Reputation: 155
The code below is a homework assignment, the actual assembly commands I understand. However I am unsure (and have been looking) how to determine the return type of the function and how many arguments if any the function takes. Professor really didn't go over this well (Monotone tenure princess) The assignment is to read assembly code and then write c code base on the assembly code. Again I understand the instructions, just cant figure out what the return type is or how many arguments the code takes
.file "hw5.c"
.text
.p2align 4,,15
.globl p1
.type p1, @function
p1:
.LFB0:
.cfi_startproc
addq %rsi, %rdi
addq %rdi, %rdx
movq %rdx, %rax
ret
.cfi_endproc
Upvotes: 3
Views: 3445
Reputation: 364512
See https://stackoverflow.com/tags/x86/info for links to calling convention docs. This looks like the standard SysV x86 ABI, used by everything except Windows.
This is obviously doing two adds 64bit adds to generate a result from three input registers, then putting the result in %rax
.
You can't tell whether the result is signed or unsigned, though, because the code would be the same.
Also, an optimized version would skip the mov
by using lea
as a non-destructive add with an output that wasn't one of the inputs:
lea (%rdi,%rdx), %rax
Upvotes: 2
Reputation: 3451
There are 3 arguments. They are rsi, rdi and rdx. Return value is in rax. Function sums the three arguments. Arguments can be signed or unsigned, as Addq works with both. All arguments and return value should be considered to have same type (signed or unsigned 64 bit integers).
This is a calling convention I've never seen on x64. So after converting to C the compiler won't generate the same assembly unless you specify a custom calling convention (assuming the compiler supports that).
Once you define a calling convention you can say what the order of the arguments is. Until then you don't know if rsi/rdi/rdx is first, second, or third arg.
Upvotes: 1
Reputation: 388
You should look at the calling convention for the processor/platform to understand the low level implementation.
Before calling a subroutine, the caller should save the contents of certain registers that are designated caller-saved. The caller-saved registers are EAX, ECX, EDX[x86]. Since the called subroutine is allowed to modify these registers, if the caller relies on their values after the subroutine returns, the caller must push the values in these registers onto the stack (so they can be restore after the subroutine returns. To pass parameters to the subroutine, push them onto the stack before the call. The parameters should be pushed in inverted order (i.e. last parameter first). Since the stack grows down, the first parameter will be stored at the lowest address (this inversion of parameters was historically used to allow functions to be passed a variable number of parameters). [ from x86 calling convention]
Upvotes: 2
Reputation: 155
this post, answers my question for the most part. Return value of a C function to ASM I didn't see this until I completely rethought my search word choice. I still am trying to figure out the number of arguments passed into the function. if I am correct though `
addq %rsi, %rdi
addq %rdi, %rdx
these two lines show me that two aguments are passed. one is stored in &rsi
the other stored in %rdi
Upvotes: 0