1.10 Accessing passed arguments
1.10 Accessing passed arguments
Now we figured out that thecallerfunction is passing arguments to thecalleevia the stack. But how does
thecalleeaccess them?
Listing 1.86: simple example
#include <stdio.h>
int f (int a, int b, int c)
{
return a*b+c;
};
int main()
{
printf ("%d\n", f(1, 2, 3));
return 0;
};
1.10.1 x86
MSVC
Here is what we get after compilation (MSVC 2010 Express):
Listing 1.87: MSVC 2010 Express
_TEXT SEGMENT
_a$ = 8 ; size = 4
_b$ = 12 ; size = 4
_c$ = 16 ; size = 4
_f PROC
push ebp
mov ebp, esp
mov eax, DWORD PTR _a$[ebp]
imul eax, DWORD PTR _b$[ebp]
add eax, DWORD PTR _c$[ebp]
pop ebp
ret 0
_f ENDP
_main PROC
push ebp
mov ebp, esp
push 3 ; 3rd argument
push 2 ; 2nd argument
push 1 ; 1st argument
call _f
add esp, 12
push eax
push OFFSET $SG2463 ; '%d', 0aH, 00H
call _printf
add esp, 8
; return 0
xor eax, eax
pop ebp
ret 0
_main ENDP
What we see is that themain()function pushes 3 numbers onto the stack and callsf(int,int,int).
Argument access insidef()is organized with the help of macros like:
_a$ = 8, in the same way as local variables, but with positive offsets (addressed withplus). So, we are
addressing theouterside of thestack frameby adding the_a$macro to the value in theEBPregister.
Then the value ofais stored intoEAX. AfterIMULinstruction execution, the value inEAXis aproductof the
value inEAXand the content of_b.