This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic
#1
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
Disclaimer: Don't be misguided looking at my usergroup and assume whatever I say is official. When I speak in community I am just a member with personal opinions.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
www.000webhost.com