0

I use Ubuntu 20.04 on Intel Celeron I reverse assembled this C code.

extern int addintandint(int i,int j)
{
    return i + j;
}

like this

$ gcc -c addintandint.c
$ objdump -d addintandint.o > dump.txt

and result is this


addintandint.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <addintandint>:
   0:   f3 0f 1e fa             endbr64 
   4:   55                      push   %rbp
   5:   48 89 e5                mov    %rsp,%rbp
   8:   89 7d fc                mov    %edi,-0x4(%rbp)
   b:   89 75 f8                mov    %esi,-0x8(%rbp)
   e:   8b 55 fc                mov    -0x4(%rbp),%edx
  11:   8b 45 f8                mov    -0x8(%rbp),%eax
  14:   01 d0                   add    %edx,%eax
  16:   5d                      pop    %rbp
  17:   c3                      retq 

I can understand by add %edx,%eax we add i and j but rest of data manipulation i cannot understand.

For example,%edx and %eax I cannot follow input data stream.

and where %edx gone after add? Someone can teach me?

  • 1
    Use gcc -O2 to get readable assembly output. Otherwise gcc generates such bad code that it’s hard to see what it’s doing. – prl Jan 23 '21 at 13:19

1 Answers1

1
  1. push stack base pointer to save the previous stack.
push   %rbp
  1. set stack base pointer by stack top pointer, so it is now a frame pointer. stack initialization was finished.
mov    %rsp,%rbp
  1. copy edi and esi registers to stack. (They were set by a caller. They are the function argument variables).
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
  1. read stack that was copied by step 3. note that eax register is used for storing the return value of this function.

Note that this store/reload only happens because of compiling without optimization (-O0), which is kind of a debug mode. A normal optimized build would be much simpler.

mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%eax
  1. add operation. the result will be set in the eax register.
add    %edx,%eax
  1. recover stack
pop    %rbp
  1. jump back to caller
retq 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
yumetodo
  • 1,147
  • 7
  • 19
  • 2
    This is the x86-64 System V ABI's calling convention. It's not called "cdecl". If you need a name for it, it's "sysv", as in GCC's `__attribute__((sysv_abi))` to indicate that calling convention. – Peter Cordes Jan 23 '21 at 15:37
  • @Peter Cordes Thank you for your edit and comment! – yumetodo Jan 24 '21 at 04:26