I'm trying to understand this simple C program:
int square(int num) {
return num * num;
}
When it's in Assembly code:
square(int):
push rbp ;push rbp register onto stack
mov rbp, rsp ;move contents of rbp register into rsp register
mov DWORD PTR [rbp-4], edi ;not sure what happens here
mov eax, DWORD PTR [rbp-4] ;not sure what happens here
imul eax, DWORD PTR [rbp-4] ;multiply eax and DWORD PTR [rbp-4] (?)
pop rbp ;pop original register out of stack
ret ;return
- What is happening in the 3rd and 4th line?
- Why did two more register (edi and eax) have to be used instead of rsp?
- What is actually happening with DWORD PTR [rbp-4]?
[rbp-4]
With optimizations enabled that should reduce tomov eax, edi; imul eax, eax; ret
. – Jesteredi
register is stored as local variable in the stack frame and copied back toeax
register. – Aki Suihkonen