i´ve written a simple code to understand assemblercode. It´s the following:
int sum(int a, int b){
int res = a+b;
}
And in the main-function I invoke the sum-function. So, and I get the assemblercode(I only take the part of sum function here)4
push ebp,
mov ebp, esp
sub esp, 16
mov eax, DWORD PTR[ebp + 12]
mov edx, DWORD PTR[ebp + 8]
add eax, edx
mov DWORD PTR [ebp-4], eax
mov eax, DWORD PTR [ebp-4]
leave
and now to my questions. I have two questions about that: First, is there a reason why the values of the sum parameter, for example sum(5,4), are strored in ebp+12 and ebp+8 and the result in ebp-4 ? Why we do that? Is it always the same or are that randomly choosen?
Second, the part in which we have:
mov DWORD PTR [ebp-4], eax
mov eax, DWORD PTR [ebp-4]
why we do the result first in ebp-4 and then in eax again before we leave the function? Is there also a reason?