Question:
Can anyone help with assembly code?
2011-02-24 01:47:55 UTC
My textbook has a C code translated to assembly:

int simple (int *xp, int y) {
.....int t = *xp + y;
.....*xp = t;
.....return t;
}

Here is the assembly code:

push %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl (%eax), %edx
addl 12(%ebp), %edx
movl %edx, (%eax)
movl %edx, %eax
movl %ebp, %esp
popl %ebp
ret

My question is why do you need

movl %edx, %eax
movl %ebp, %esp

What is the difference between those two commands?
Any help is appreciated. Thanks!
Four answers:
ejwaxx
2011-02-24 15:22:52 UTC
Well, let us step through the instructions and see what they correspond to in the C code:



push %ebp

movl %esp, %ebp



These do not carry out any of the statements in the function. Instead, they are setting up a stack frame for the function simple: this allows us to push and pop off the stack, if necessary, while still referring to local variables and function arguments by constant offsets from ebp.



movl 8(%ebp), %eax



This moves the value at memory address ebp + 8 into eax. Since ebp + 8 is the address of the first function argument, we now have xp in eax.



movl (%eax), %edx



xp is a pointer to an int, which means it contains the memory address of an int. (%eax) retrieves the value at the memory address in eax (which is xp), and %edx means that the value is moved into edx. At this point, we can say that eax contains xp and edx contains *xp (the int to which xp points).



addl 12(%ebp), %edx



12(%ebp) is the value at memory location ebp + 12, i.e. the value of y, the second function argument. We are to add y to the value in edx (*xp). This finishes implementing int t=*xp + y, and stores t in edx.



movl %edx, (%eax)



Moves t into the memory location specified in eax (xp). This implements *xp = t.



movl %edx, %eax



Moves t into register eax. This a different instruction than the one above, because the op above does not change eax, it just changes the dword in the memory block at which eax points. Since ints are returned in eax, and simple returns t, we need to move the actual value of t into eax before returning.



movl %ebp, %esp

popl %ebp



We are destroying simple's stack frame, so that the calling function's stack is preserved when we return from simple.
2011-02-24 06:15:15 UTC
Well "movl %edx, %eax" is loading the contents of the eax register into the edx register

and "movl %ebp, %esp" is loading the contents of the esp register into the ebp register.

If that doesn't answer your question, then can you please clarify what exactly you're asking?
silveira
2016-10-06 04:55:53 UTC
it truly is the output minus debug information for one compiler I even have. it truly is sixteen bit x86 code. .386p publicdll macro call public call endm $comm macro call,dist,length,count huge form comm dist call:BYTE:count huge form*length endm else $comm macro call,dist,length,count huge form comm dist call[length]:BYTE:count huge form endm endif _TEXT phase byte public use16 'CODE' _TEXT ends DGROUP team _DATA,_BSS anticipate cs:_TEXT,ds:DGROUP _DATA phase be conscious public use16 'documents' d@ label byte d@w label be conscious _DATA ends _BSS phase be conscious public use16 'BSS' b@ label byte b@w label be conscious _BSS ends _TEXT phase byte public use16 'CODE' anticipate cs:_TEXT,ds:DGROUP _main proc close to go into 2,0 call close to ptr _pass mov be conscious ptr [bp-2],ax push be conscious ptr [bp-2] push offset DGROUP:s@ call close to ptr _printf upload sp,4 xor ax,ax leave ret _main endp anticipate cs:_TEXT,ds:DGROUP _pass proc close to push bp mov bp,sp mov ax,3 pop bp ret _pass endp _TEXT ends _DATA phase be conscious public use16 'documents' s@ label byte db '%d' db 0 _DATA ends _TEXT phase byte public use16 'CODE' _TEXT ends extrn _printf:close to public _pass public _main end Shadow Wolf
2011-02-24 01:52:24 UTC
OH i remember doing assembly for a year.

It was the 2nd most awful thing in my whole life.



good luck.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...