1
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rax
mov    %edx,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x4(%rbp),%edx
mov    %edx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    %edx,%eax
pop    %rbp
retq  

I am just looking for someone to confirm my thinking, what I am seeing happen is that parameter 1 is taken and put 18 below rbp and parameter two is taken and put 20 below rbp and then it seems to me that the parameters are set to each other as in x=y and y=x however at the very end the second parameter is set to rax and then added to edx which i believe is the first parameter and then returned. Is this correct or am i way off?

2

Yes, that seems correct. The equivalent C code would look something like:

int func(int *arg1, int *arg2)
{
    int temp = *arg1;
    *arg1 = *arg2;
    *arg2 = temp;
    return *arg1 + *arg2;
}

The use of stack-based storage indexed off of rbp is what we called local storage. We can give each use a name to make it easier to see what is going on. Let's call the value at rbp-0x18 local_arg1, rbp-0x20 local_arg2 and rbp-0x4 local_temp. By calling convention, rdi is the first argument to the function and rsi is the second.

Adding comments where pointer dereferencing is occurring, the disassembly is then

push   %rbp
mov    %rsp,%rbp         
mov    %rdi,local_arg1
mov    %rsi,local_arg2
mov    local_arg1,%rax
mov    (%rax),%eax        ; dereference the pointer i.e. eax = *arg1
mov    %eax,local_temp
mov    local_arg2,%rax
mov    (%rax),%edx        ; edx = *arg2
mov    local_arg1,%rax
mov    %edx,(%rax)        ; *arg1 = edx
mov    local_arg2,%rax
mov    local_temp,%edx
mov    %edx,(%rax)        ; *arg2 = edx
mov    local_arg1,%rax
mov    (%rax),%edx        ; edx = *arg1
mov    local_arg2,%rax
mov    (%rax),%eax        ; eax = *arg2
add    %edx,%eax
pop    %rbp
retq  
  • 1
    awesome how do you know they are pointers and not just ints? – GoldenWest Dec 9 '17 at 7:23
  • is it because of the (%rbp) vs %rbp stating value of ? – GoldenWest Dec 9 '17 at 7:24
  • I updated the answer to better explain that. – cimarron Dec 9 '17 at 15:58
  • How did you decompiled your c code to assembly and vise-versa? – Omkar Nath Singh Dec 16 '17 at 7:58
  • No tool, I just created the C code from my experience of what compilers generate and the disassembly is very straightforward so it's pretty easy to what is going on. Any good decompiler tool should generate something similar to what I put down though. – cimarron Dec 16 '17 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.