简体   繁体   中英

Given Assembly, translate to C

I am originally given the function prototype:

void decode1(int *xp, int *yp, int *zp)

now i am told to convert the following assembly into C code:

movl 8(%ebp), %edi //line 1 ;; gets xp
movl 12(%ebp), %edx //line 2 ;; gets yp
movl 16(%ebp),%ecx //line 3 ;; gets zp
movl (%edx), %ebx //line 4 ;; gets y
movl (%ecx), %esi //line 5 ;; gets z
movl (%edi), %eax //line 6 ;; gets x
movl %eax, (%edx) //line 7 ;; stores x into yp
movl %ebx, (%ecx) //line 8 ;; stores y into zp
movl %esi, (%edi) //line 9 ;; stores z into xp

These comments were not given to me in the problem this is what I believe they are doing but am not 100% sure.

My question is, for lines 4-6, am I able to assume that the command

movl (%edx), %ebx 
movl (%ecx), %esi
movl (%edi), %eax

just creates a local variables to y,z,x?

also, do the registers that each variable get stored in ie (edi,edx,ecx) matter or can I use any register in any order to take the pointers off of the stack?

C code:

int tx = *xp;
int ty = *yp;
int tz = *zp;
*yp = tx;
*zp = ty;
*xp = tz;

If I wasn't given the function prototype how would I tell what type of return type is used?

Congratulations, you got everything right :)

You can use any register but some need to be preserved, that is they should be saved before use and restored afterwards. In typical calling conventions you can use eax , ecx and edx , the rest need to be preserved. The assembly you showed doesn't include code to do this, but presumably it is there.

As for the return type, that's hard to deduce. Simple types are returned in the eax register, and something is always in there. We can't tell if that's intended as a return value, or just remains of a local variable. That is, if your function had return tx; it could be the same assembly code. Also, we don't know the type for eax either, it could be anything that fits in there and is expected to be returned there according to the calling convention.

Let's focus on a simpler set of instructions.

First:

movl 8(%ebp), %edi

will load into the EDI register the content of the 4 bytes that are situated on memory at 8 eight bytes beyond the address set in the EBP register. This special EBP usage is a convention followed by the compiler code generator, that per each function, saves the stack pointer ESP into the EBP registers, and then creates a stack frame for the function local variables.

Now, in the EDI register, we have the first parameter passed to the function, that is a pointer to an integer, so EDI contains now the address of that integer, but not the integer itself.

movl (%edi), %eax

will get the 4 bytes pointed by the EDI register and load them into the EAX register.

Now in EAX we have the value of the integer pointed by the xp in the first parameter.

And then:

movl %eax, (%edx)

will save this integer value into the memory pointed by the content of the EDX register which was in turn loaded from EBP+12 which is the second parameter passed to the function.

So, your first question, is this assembly code equivalent to this?

int tx = *xp;
int ty = *yp;
int tz = *zp;
*yp = tx;
*zp = ty;
*xp = tz;

is, yes , but note that there are no tx,ty,tz local variables created, but just processor registers.

And your second question, is no , you can't tell the type of return, it is, again, a convention on the register usage that you can't infer just by looking at the generated assembly code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM