Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1183
Appendix C: Notes on C
<main>:
1: push ebp
2: mov ebp,esp
3: sub esp,0x18
4: mov eax,0x0
5: mov DWORD PTR [ebp-4],0x3
6: mov DWORD PTR [ebp-8],0x4
7: mov eax,DWORD PTR [ebp-8]
8: mov DWORD PTR [esp+4],eax
9: mov eax,DWORD PTR [ebp-4]
10: mov DWORD PTR [esp],eax
11: call <add>
12: mov DWORD PTR [ebp-12],eax
13: mov eax,DWORD PTR [ebp-12]
14: mov DWORD PTR [esp+4],eax
15: mov DWORD PTR [esp],0x0
16: call <printf>
17: mov DWORD PTR [esp],0x0
18: call <exit>
<add>:
19: push ebp
20: mov ebp,esp
21: mov eax,DWORD PTR [ebp+12]
22: add eax,DWORD PTR [ebp+8]
23: pop ebp
24: ret
mainbegins with the standard operations described previously to save the frame pointer that, on IA-32
systems, is held in theebpregister. The value is pushed onto thelowest position in the stack, and this
causes the stack pointer to be moved down automatically by 4 bytes — simply because 4 bytes are needed
to represent a pointer on IA-32 systems. The value of the stack pointer is then stored in the frame pointer
register using themovstatement.mov a, bcopies the value in registerbto registera. Line 2 therefore
causes the current value of the stack pointer to be copied into the frame pointer.
Line 3 subtracts 0x18 bytes from the stack pointer and moves it down, thus increasing the size of the stack
by0x18= 24. Line 4 initializeseax, a general-purpose register, with the value 0.
The local variables must now be placed on the stack.As the C code indicates, there are two variables,
aandb,formain. They are both integer variables and therefore each needs 4 bytes of memory. Because
the first 4 bytes of the stack hold the old value of the frame pointer, the compiler reserves the two 4-byte
areas below for the variables.
To assign the initial values to the reserved memory space, the compiler makes use of the pointer de-
referencing option of the processor. TheDWORD PTR [ebp-4]statement in line 5 instructs the compiler
to reference the position in memory to which the value ‘‘frame pointers minus 4‘‘ points. The value 3 is
written to this position usingmov. The compiler proceeds in the same way with the second local variable,
which is lower in the stack and is given the value 4.