Understanding C program in Assembly

Viewed 4244

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 
  1. What is happening in the 3rd and 4th line?
  2. Why did two more register (edi and eax) have to be used instead of rsp?
  3. What is actually happening with DWORD PTR [rbp-4]?
1 Answers
Related