+ All Categories
Home > Science > The stack

The stack

Date post: 15-Aug-2015
Category:
Upload: howard-chang
View: 19 times
Download: 2 times
Share this document with a friend
Popular Tags:
22
the Stack
Transcript
Page 1: The stack

the Stack

Page 2: The stack

sample code#include <stdio.h>

void foo(int w, int x, int y) {

int z = 0;

z = x + y ;

}

int main () {

foo(12,15,18) ;

}

Page 3: The stack

Stack

ESP:top of the stack

EBP:base pointerEIP: next instruction

Page 4: The stack

before calling foo

• push EAX, ECX and EDX

• push arguments of foo(12, 15, 18) in to stack

• 18 -> 15 -> 12

• call function -> push return address ( EIP )

something

EBP

ESP

Page 5: The stack

cont.

• push EAX, ECX and EDX

• push arguments of foo(12, 15, 18) in to stack

• 18 -> 15 -> 12

• call function -> push return address ( EIP )

something

EAX, ECX & EDX

EBP

ESP

Page 6: The stack

cont.

• push EAX, ECX and EDX

• push arguments of foo(12, 15, 18) in to stack

• 18 -> 15 -> 12

• call function -> push return address ( EIP )

something

EAX, ECX & EDX

args 3 =18

args 2 =15

args 1 =12

EBP

ESP

Page 7: The stack

cont.

• push EAX, ECX and EDX

• push arguments of foo(12, 15, 18) in to stack

• 18 -> 15 -> 12

• call function -> push return address ( EIP )

something

EAX, ECX & EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP

ESP

Page 8: The stack

After calling foo• push ebp

• mov ebp, esp

• allocate space for local variables and buffer

• sub esp, 4 ; 4 bytes for int

• push EBX, ESI and EDI

• loading local variables

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP

ESP

Page 9: The stack

cont.• push ebp

• mov ebp, esp

• allocate space for local variables and buffer

• sub esp, 4 ; 4 bytes for int

• push EBX, ESI and EDI

• loading local variables

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

EBP

ESP

Page 10: The stack

cont.• push ebp

• mov ebp, esp

• allocate space for local variables and buffer

• sub esp, 4 ; 4 bytes for int

• push EBX, ESI and EDI

• loading local variables

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)EBP,ESP

Page 11: The stack

cont.• push ebp

• mov ebp, esp

• allocate space for local variables and buffer

• sub esp, 4 ; 4 bytes for int

• push EBX, ESI and EDI

• loading local variables

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

XESP

EBP

Page 12: The stack

cont.• push ebp

• mov ebp, esp

• allocate space for local variables and buffer

• sub esp, 4 ; 4 bytes for int

• push EBX, ESI and EDI

• loading local variables

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

X

EBX, ESI and EDIESP

EBP

Page 13: The stack

loading local variables

int z = 0;

mov dword ptr [ebp-4], 0

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

local variable z = 0

EBX, ESI and EDIESP

EBP

Page 14: The stack

preform function task

Page 15: The stack

before returning

• pop edi esi ebx

• mov esp, ebp

• pop ebp

• ret

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

local variable z

EBX, ESI and EDI

EBP

ESP

Page 16: The stack

cont.

• pop edi esi ebx

• mov esp, ebp

• pop ebp

• ret

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

local variable z

EBP

ESP

Page 17: The stack

cont.

• pop edi esi ebx

• mov esp, ebp

• pop ebp

• ret

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)

EBP(main)

local variable z

EBPESP

Page 18: The stack

cont.

• pop edi esi ebx

• mov esp, ebp

• pop ebp

• ret

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

return address (foo)ESP

EBP

Page 19: The stack

cont.

• pop edi esi ebx

• mov esp, ebp

• pop ebp

• ret

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

EBP

ESP

Page 20: The stack

after returning

• add esp, 12

• pop EAX, ECX and EDX

something

EAX, ECX and EDX

args 3 =18

args 2 =15

args 1 =12

EBP

ESP

Page 21: The stack

after returning

• add esp, 12

• pop EAX, ECX and EDX

something

EAX, ECX and EDX

EBP

ESP

Page 22: The stack

cont.

• add esp, 12

• pop EAX, ECX and EDXsomething

EBP

ESP


Recommended