Confused how to read assembly instruction - c

I have some questions dealing with reading assembly instruction. I know the basic instruction syntax, but it gets confusing when some of the register comes with some address in front of it, or the question uses any of the strange instruction. Is there any easy to understand set of reference to the assembly instructions?
For example, I have a question to read these instruction:
0x401050 <what>: push %ebp
0x401051 <what+1>: mov %esp,%ebp
0x401053 <what+3>: sub $0xc,%esp
0x401056 <what+6>: mov 0x8(%ebp),%eax
0x401059 <what+9>: add $0x4,%eax
0x40105c <what+12>: mov %eax,0xfffffffc(%ebp)
0x40105f <what+15>: mov 0x8(%ebp),%eax
0x401062 <what+18>: imul 0xc(%ebp),%eax
0x401066 <what+22>: mov %eax,0xfffffff8(%ebp)
0x401069 <what+25>: mov 0xc(%ebp),%edx
0x40106c <what+28>: mov 0x8(%ebp),%eax
0x40106f <what+31>: sub %edx,%eax
0x401071 <what+33>: mov %eax,0xfffffff4(%ebp)
0x401074 <what+36>: mov 0xfffffff8(%ebp),%eax
0x401077 <what+39>: add 0xfffffffc(%ebp),%eax
0x40107a <what+42>: add 0xfffffff4(%ebp),%eax
0x40107d <what+45>: leave
0x40107e <what+46>: ret
0x401089 <main>: push %ebp
0x40108a <main+1>: mov %esp,%ebp
0x40108c <main+3>: sub $0x18,%esp
0x40108f <main+6>: and $0xfffffff0,%esp
0x401092 <main+9>: mov $0x0,%eax
0x401097 <main+14>: mov %eax,0xfffffff0(%ebp)
0x40109a <main+17>: mov 0xfffffff0(%ebp),%eax
0x40109d <main+20>: call 0x401420 <_alloca>
0x4010a2 <main+25>: call 0x4014b0 <__main>
0x4010a7 <main+30>: mov 0xc(%ebp),%eax
0x4010aa <main+33>: add $0x4,%eax
0x4010ad <main+36>: mov (%eax),%eax
0x4010af <main+38>: mov %eax,(%esp)
0x4010b2 <main+41>: call 0x4014d0 <atoi>
0x4010b7 <main+46>: mov %eax,0xfffffffc(%ebp)
0x4010ba <main+49>: mov 0xc(%ebp),%eax
0x4010bd <main+52>: add $0x8,%eax
0x4010c0 <main+55>: mov (%eax),%eax
0x4010c2 <main+57>: mov %eax,(%esp)
0x4010c5 <main+60>: call 0x4014d0 <atoi>
0x4010ca <main+65>: mov %eax,0xfffffff8(%ebp)
0x4010cd <main+68>: mov 0xfffffff8(%ebp),%eax
0x4010d0 <main+71>: inc %eax
0x4010d1 <main+72>: mov %eax,0x4(%esp)
0x4010d5 <main+76>: mov 0xfffffffc(%ebp),%eax
0x4010d8 <main+79>: add $0x2,%eax
0x4010db <main+82>: mov %eax,(%esp)
0x4010de <main+85>: call 0x401050 <what>
0x4010e3 <main+90>: mov %eax,0xfffffff4(%ebp)
0x4010e6 <main+93>: mov 0xfffffff4(%ebp),%eax
0x4010e9 <main+96>: mov %eax,0x4(%esp)
0x4010ed <main+100>: movl $0x40107f,(%esp)
0x4010f4 <main+107>: call 0x4014c0 <printf>
0x4010f9 <main+112>: mov $0x0,%eax
0x4010fe <main+117>: leave
0x4010ff <main+118>: ret
The questions are:
This program is invoked from the command line as shown:
./program 5 7
The procedure named what is called from main, with the following argument(s):
1) 6, 8
2) 7, 8
3) 5, 7
4) 7, 5
The procedure named what is called from main, and returns to main with the value:
1) 11
2) 24
3) 42
4) 66
Could anyone guide me how the instructions in this example works? Any suggestion or help would be appreciated. Thanks.

You should try and loading that binary into IDA. IDA gives you a better view of the structure and its easier to understand what it is you are trying to ask (aside from reading instructions).
Edit: https://www.hex-rays.com/products/ida/
This is for binaries, if it fits your needs, there is a free version of this software on the above page as well. You can use the demo version to do your work as well. I have used IDA Pro (demo) for viewing and editing iOS App Binaries.
To the comments below, you are the worst thing about Stack Exchange - therefore this is the last time I try to help. Reddit it is.

Related

im having a PANIC when running some assembly in bochs

this image showcase the error im getting when running the command "bochs" in the bootDIr directory where my assembly file , the image ,the binary and the script are at , the tutorial i watched was using ubuntu while im using kali,i dont think that was relevant but i would love your help
the goal is to show the "Hello" String when booting (+some messages generated by the emulator)
here is the nasm code:
[BITS 16]
[ORG 0x7c00]
start :
xor ax,ax
mov ds,ax
mov es,ax
mov ss, ax
mov sp, 0x7c00
PrintMessage :
mov ah,0x13
mov al, 1
mov bx,0xa
xor dx,dx
mov bp, Message
mov cx, MessageLen
int 0x10
End :
hlt
jmp End
Message : db "Hello"
MessageLen : equ $-Message
times (0x1be -($-$$)) db 0
db 80h
db 0,2,0
db 0f0h
db 0ffh,0ffh,0ffh
dd 1
dd (20*16*63 - 1 )
times (16*3) db 0
db 0x55
db 0xaa

check positive number assembly8086

iam trying to write an assembly code 8086 i have two array X AND Y size 10 and i want to enter a number and check if its positive y will be 1 if negative it will be 0
but my code just displayed what i enter a number but i want it like this example .
example:
X : 2 -5 -7 8 -4 9 11 -12 90 -50
Y : 1 0 0 1 1 0 1 1 0 1 0
I wrote:
.MODEL SMALL
.DATA
x db 10 dup ("?")
y db 10 dup ("?")
.CODE
MOV AH,1
INT 21H
mov si,offset x
mov di,offset y
mov [si],al
cmp si,0
ja l1
jmp l2
l1 : mov al , 1
mov [di],al
jmp l3
l2 : mov ah , 0
mov y[di] , al
jmp l3
l3 :
MOV AH, 2
mov dl,al
INT 21H
cmp si,10
je l4
inc si
inc di
l4 :
.EXIT
END
The list of problems is long. Check out these comments and try to see why the error exists.
<<<< add label for a 10-time repeat
MOV AH,1
INT 21H
mov si,offset x <<<< move higher up for 1-time execution
mov di,offset y <<<< move higher up for 1-time execution
mov [si],al
cmp si,0 <<<< the byte to compare is in AL
ja l1 <<<< requires a SIGNED conditional, use JGE
jmp l2 <<<< reverse condition and drop this jump
l1 : mov al , 1
mov [di],al
jmp l3
l2 : mov ah , 0 <<<< should be AL
mov y[di] , al <<<< DI already refers to the y-array, use just [DI]
jmp l3 <<<< redundant
l3 :
MOV AH, 2
mov dl,al
INT 21H
cmp si,10 <<<< 10 is wrong
je l4
inc si <<<< move above checking iterator
inc di <<<< move above checking iterator
<<<< needs jumping back in order to repeat
l4 :
Next code, just like yours, inputs a single character from the keyboard and treats its ASCII code as the byte you want to store in the x array. For negative bytes you should input extended ASCII's. Use the Alt followed by a number up to 255.
.CODE
xor bx, bx
again:
mov ah, 01h ; DOS.GetCharacter
int 21h ; -> AL
mov x[bx], al
mov ah, 255
shl ax, 1
not ah
mov y[bx], ah ; 1 for positive, 0 for negative
inc bx
cmp bx, 10
jb again
.EXIT

Assembly Language x86 - Registers Set and Arithmetic and Loop

I am trying to solve this problem about loops. I am using a push and pop method instead of using a separate register to store data.
.model small
.stack
.code
m proc
mov ax,0b800h
mov es,ax
mov di,7d0h
mov ah,7 ; normal attribute
mov al,'A'
mov cx,5
x: stosw
push ax ;mov dl,al ; dl='A'
push di
mov al,'1'
stosw
pop di
add di,158
pop ax ;mov al,dl
inc al
loop x
mov ah,4ch
int 21h
m endp
end m
I am unable to loop the mov al, '1'.
The output should be like this:
A1
B2
C3
D4
E5
Can anyone show the correct code? Thank you.
Consider the ASCII codes involved:
Letter Digit Difference
A1 65 49 16
B2 66 50 16
C3 67 51 16
D4 68 52 16
E5 69 53 16
See how the difference is always 16 ? That's what next solution exploits:
...
mov ax, 0700h + 'A' ; WhiteOnBlack 'A'
x: stosw ; Stores one letter from {A, B, C, D, E}
sub al, 'A' - '1' ; Convert from letter to digit
stosw ; Stores one digit from {1, 2, 3, 4, 5}
add al, 'A' - '1' + 1 ; Restore AL and at the same time increment
add di, 160 - 4 ; Move down on the screen
cmp al, 'E'
jbe x
...
You don't always need to use CX and the LOOP instruction to work with a loop. Anyway the LOOP instruction is to be avoided for speed reasons!

x86 real mode: move data from one array to another

Here is my code:
data segment
gio db 1,2,3,4,5,6
ricxvi db 1
jami db 0
x db ?
ends
stack segment
db 128 dup(0)
ends
code segment
start:
MOV AX,DATA
MOV DS,AX
mov cx, 6
lea si, gio
mov ah, 0
n1:
mov al, [si]
cmp ricxvi, 3
je n2
jmp n3
n2:
add jami, al
mov ricxvi, 1
jmp n4
n3:
add ricxvi, 1
push ax
n4:
add si, 1
add di, 1
loop n1
mov ricxvi, 1
mov ax, 0
mov cx, 6
lea si, gio
n5:
cmp ricxvi, 3
je n6
jmp n7
n6:
mov ricxvi, 1
add si, 1
loop n5
n7:
pop [si]
add si, 1
loop n5
mov ax, 4c00h
int 21h
ends
end start
I have a array named gio And I'm trying to reverse this array but leave every 3th element on its position. Meaning I want to get the output like this 5,4,3,2,1,6 but As I inspect variables, in array I have 5,4,2,1,B8. I have noticed that when program first hits pop [si] whole array changes, exploring variables shows me that its 5, NULL, 3, 4, 5, 6 should it not be 5,2,3,4,5,6? I'm using emu8086. Question may sound silly as I'm new with assembly. Thanks.
There are three errors :
You are poping two bytes into [si] but you only need one byte. The solution is to pop two bytes into a register and move one byte into [si].
Under label n6 you got loop n5, but when cx becomes zero the loop doesn't jump and the block n7 is executed when it shouldn't.
You forgot to increase the "3" counter under label n7.
Here are the fixes :
n6:
mov ricxvi, 1
add si, 1
loop n5
jmp finale ;◄■■ SKIP NEXT BLOCK WHEN CX=0.
n7:
add ricxvi, 1 ;◄■■ INCREASE THE "3" COUNTER.
pop ax ;◄■■ POP TWO BYTES.
mov [si], al ;◄■■ USE ONE BYTE ONLY.
add si, 1
loop n5
finale: ;◄■■ END LABEL.
mov ax, 4c00h
int 21h

Assembly Language (Order Taker)

I'm trying hard to study Assembly language.
I really need help to store different 10 items in an array
I want to build a program that will accept items from 10 choices and it will store it.
Those 10 items have different value.
Example
Item 1 = 10$
Item 2 = 4$
So that when the User tries to choose Item1 and Item2 it will show the
sum of both items.
I'll be gladly be happy if someone can share his/her own code that can store 10 items with the sum of all items. Thanks
Here's my code:
_start:
mov eax,3 ;number bytes to be summed
mov ebx,0 ;EBX will store the sum
mov ecx, x ;ECX will point to the current element to be summed
top: add ebx, [ecx]
add ecx,1 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
done:
add ebx, '0'
mov [sum], ebx ;done, store result in "sum"
display:
mov edx,1 ;message length
mov ecx, sum ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
global x
x:
db 2
db 4
db 3
sum:
db 0
My code here does have problem.
x:
db 2
db 4
db 3
sum:
db 0
Since you've defined all of your variables to be of byte type, you must process them as bytes! You're reading and writing dwords in your program.
This could have been OK:
top: add bl, [ecx]
add ecx, 1 ;move pointer to next element
dec eax ;decrement counter
jnz top ;if counter not 0, then loop again
done:
add bl, '0'
mov [sum], bl ;done, store result in "sum"

Resources