I have compiled the following c program to asm to see what instructions it uses. What I have in C is:
int add(int num1, int num2) {
int num3 = num1 + num2;
return num3;
}
My thought on what the instructions "should be" (from my very limited knowledge of asm) would be:
- Load (two 4-byte int variables into memory).
- Add (two memory locations), and -
- Store the sum at a third memory location.
- Return the value and halt execution.
When compiling this, I was surprised at all the mov
operations it does:
add:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov edx, DWORD PTR [rbp-20]
mov eax, DWORD PTR [rbp-24]
add eax, edx
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
Could someone walk me through the asm code here and point out why it's using the mov
code so frequently? Here's an example of it: here.