ModdersCentral

Full Version: Basic
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Stack Frame
This is a template on how a function should look like
Code:
push    ebp            ; must save old ebp
mov    ebp, esp        ; point ebp to this frame
sub    esp, 12                ; make space for locals (3 ints 3x4=12)
...
mov    esp, ebp        ; clean up locals
pop    ebp            ; restore old ebp
ret
for the next example we need to know where the local variables and parameters are placed in memory.
we will use the table below as a guide
Code:
               +---------+
        ebp-12 |    a    |
               +---------+
        ebp-8  |    b    |
               +---------+
        ebp-4  |    c    |
               +---------+
        ebp    | old ebp |
               +---------+
        ebp+4  | retaddr |
               +---------+
        ebp+8  |    x    |
               +---------+
        ebp+12 |    y    |
               +---------+
this is what the function looks like in C
Code:
int example(int x, int y) {
 int a, b, c;//make space for locals (3 ints 3x4=12)
 b = 7;
 return x * b + y;//the return value is set in the eax register
}
this is the assembly example
Code:
example:
push    ebp            ; must save old ebp
mov    ebp, esp        ; point ebp to this frame
sub    esp, 12                ; make space for locals (3 ints 3x4=12)
;----------------------------
mov    dword [ebp-8], 7        ;b = 7
mov    eax, [ebp+8]            ;eax = x
imul    eax, [ebp-8]            ; x * b
add    eax, [ebp+12]        ; x * b + y
;----------------------------
mov    esp, ebp        ; clean up locals
pop    ebp            ; restore old ebp
ret
www.000webhost.com