I am reading about x64 Assembly through this online textbook. It takes the following C function:
int adder2(int a) {
return a + 2;
}
And shows how it is compiled into x64 Assembly:
push %rbp
mov %rsp,%rbp
mov %edi,-0x4(%rbp)
mov -0x4(%rbp),%eax
add $0x2,%eax
pop %rbp
retq
I understand that push %rbp
pushes the adder2 function onto the call stack, pop %rbp
pops it from the call stack, and retq
returns control to the function that had called adder2. I also get that add $0x2, %eax
adds 2 to a, and the result is what will be returned.
However, why are there three lines that call the mov command? There is only one variable (a), so why are there so many registers? There is no clear line-by-line connection between the C and Assembly code.