Understanding C program in Assembly -


i'm trying understand 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 rsp register    mov dword ptr [rbp-4], edi ;not sure happens here    mov eax, dword ptr [rbp-4] ;not sure happens here   imul eax, dword ptr [rbp-4] ;multiply eax , dword ptr [rbp-4] (?)   pop rbp ;pop original register out of stack   ret ;return  
  1. what happening in 3rd , 4th line?
  2. why did 2 more register (edi , eax) have used instead of rsp?
  3. what happening dword ptr [rbp-4]?

mov dword ptr [rbp-4], edi ;not sure happens here 

the x86_64 system v abi passes function arguments via registers - first integer argument passed in rdi/edi register. line copies argument num local (offset -4 bytes frame pointer value stored in rbp).

mov eax, dword ptr [rbp-4] ;not sure happens here 

this copies value in local eax register.

imul eax, dword ptr [rbp-4] ;multiply eax , dword ptr [rbp-4] (?) 

and multiplies value in eax local, , stores result eax (which happens register in function return value stored).

as others pointed out in comments, compiling optimization eliminate local, , write directly edi eax.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -