The Hardware/So<ware Interface CSE351 Winter 2013 - - PowerPoint PPT Presentation

the hardware so ware interface
SMART_READER_LITE
LIVE PREVIEW

The Hardware/So<ware Interface CSE351 Winter 2013 - - PowerPoint PPT Presentation

University of Washington The Hardware/So<ware Interface CSE351 Winter 2013 Procedures and Stacks I University of Washington Roadmap Data & addressing


slide-1
SLIDE 1

University ¡of ¡Washington ¡

Procedures ¡and ¡Stacks ¡I ¡

The ¡Hardware/So<ware ¡Interface ¡

CSE351 ¡Winter ¡2013 ¡

slide-2
SLIDE 2

University ¡of ¡Washington ¡

Roadmap ¡

2 ¡ car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG();

get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret

Java: ¡ C: ¡ Assembly ¡ language: ¡ Machine ¡ code: ¡

0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111

Computer ¡ system: ¡ OS: ¡

Data ¡& ¡addressing ¡ Integers ¡& ¡floats ¡ Machine ¡code ¡& ¡C ¡ x86 ¡assembly ¡ programming ¡ Procedures ¡& ¡ stacks ¡ Arrays ¡& ¡structs ¡ Memory ¡& ¡caches ¡ Processes ¡ Virtual ¡memory ¡ Memory ¡allocaPon ¡ Java ¡vs. ¡C ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-3
SLIDE 3

University ¡of ¡Washington ¡

Procedures ¡and ¡Call ¡Stacks ¡

 How ¡do ¡I ¡pass ¡arguments ¡to ¡a ¡procedure? ¡  How ¡do ¡I ¡get ¡a ¡return ¡value ¡from ¡a ¡procedure? ¡  Where ¡do ¡I ¡put ¡local ¡variables? ¡  When ¡a ¡funcPon ¡returns, ¡how ¡does ¡it ¡know ¡where ¡to ¡return ¡

to? ¡ ¡

 To ¡answer ¡these ¡quesPons, ¡we ¡need ¡a ¡call ¡stack ¡… ¡

Winter ¡2013 ¡

3 ¡

Procedures ¡and ¡Stacks ¡I ¡

slide-4
SLIDE 4

University ¡of ¡Washington ¡

Memory ¡Layout ¡

Winter ¡2013 ¡

4 ¡

Procedures ¡and ¡Stacks ¡I ¡

Instructions Literals Static Data Dynamic Data (Heap) Stack

literals (e.g., “example”) static variables (including global variables (C)) variables allocated with new or malloc local variables; procedure context

2N-1

slide-5
SLIDE 5

University ¡of ¡Washington ¡

Memory ¡Layout ¡

Winter ¡2013 ¡

5 ¡

Procedures ¡and ¡Stacks ¡I ¡

Instructions Literals Static Data Dynamic Data (Heap) Stack

Managed “automatically” (by compiler) writable; not executable Managed by programmer writable; not executable Initialized when process starts writable; not executable Initialized when process starts Read-only; not executable Initialized when process starts Read-only; executable

slide-6
SLIDE 6

University ¡of ¡Washington ¡

IA32 ¡Call ¡Stack ¡

 Region ¡of ¡memory ¡managed ¡ ¡

with ¡a ¡stack ¡“discipline” ¡

 Grows ¡toward ¡lower ¡addresses ¡  Customarily ¡shown ¡“upside-­‑down” ¡  Register ¡%esp ¡contains ¡ ¡

lowest ¡stack ¡address ¡ = ¡address ¡of ¡“top” ¡element ¡ Stack ¡Pointer: ¡%esp

Stack ¡Grows ¡ Down ¡ Increasing ¡ Addresses ¡

Stack ¡“Top” ¡ Stack ¡“Bobom” ¡

6 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-7
SLIDE 7

University ¡of ¡Washington ¡

IA32 ¡Call ¡Stack: ¡Push ¡

 pushl Src ¡

Stack ¡Grows ¡ Down ¡ Increasing ¡ Addresses ¡

Stack ¡“Top” ¡ Stack ¡“Bobom” ¡ Stack ¡Pointer: ¡%esp

7 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-8
SLIDE 8

University ¡of ¡Washington ¡

IA32 ¡Call ¡Stack: ¡Push ¡

 pushl Src ¡

  • Fetch ¡value ¡from ¡Src ¡
  • Decrement ¡%esp ¡by ¡4 ¡ ¡(why ¡4?) ¡
  • Store ¡value ¡at ¡address ¡ ¡

given ¡by ¡%esp

Stack ¡Grows ¡ Down ¡ Increasing ¡ Addresses ¡

Stack ¡“Top” ¡ Stack ¡“Bobom” ¡ Stack ¡Pointer: ¡%esp

  • ­‑4 ¡

8 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-9
SLIDE 9

University ¡of ¡Washington ¡

IA32 ¡Call ¡Stack: ¡Pop ¡

Stack ¡Pointer: ¡%esp

Stack ¡Grows ¡ Down ¡ Increasing ¡ Addresses ¡

Stack ¡“Top” ¡ Stack ¡“Bobom” ¡

 popl Dest ¡

9 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-10
SLIDE 10

University ¡of ¡Washington ¡

IA32 ¡Call ¡Stack: ¡Pop ¡

Stack ¡Pointer: ¡%esp

Stack ¡Grows ¡ Down ¡ Increasing ¡ Addresses ¡

Stack ¡“Top” ¡ Stack ¡“Bobom” ¡

 popl Dest ¡

  • Load ¡value ¡from ¡address ¡%esp ¡
  • Write ¡value ¡to ¡Dest ¡
  • Increment ¡%esp ¡by ¡4 ¡

+4 ¡ 10 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-11
SLIDE 11

University ¡of ¡Washington ¡

Procedure ¡Call ¡Overview ¡

11 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

Caller ¡ Callee ¡

 Callee ¡must ¡know ¡where ¡to ¡find ¡args ¡  Callee ¡must ¡know ¡where ¡to ¡find ¡“return ¡address” ¡  Caller ¡must ¡know ¡where ¡to ¡find ¡return ¡val ¡  Caller ¡and ¡Callee ¡run ¡on ¡same ¡CPU ¡→ ¡use ¡the ¡same ¡registers ¡

  • Caller ¡might ¡need ¡to ¡save ¡registers ¡that ¡Callee ¡might ¡use ¡
  • Callee ¡might ¡need ¡to ¡save ¡registers ¡that ¡Caller ¡has ¡used ¡

¡ ¡ ¡ ¡… ¡ <set ¡up ¡args> ¡ call ¡ <clean ¡up ¡args> ¡ <find ¡return ¡val> ¡ ¡ ¡ ¡ ¡… ¡ ¡ <create ¡local ¡vars> ¡ ¡ ¡ ¡… ¡ <set ¡up ¡return ¡val> ¡ <destroy ¡local ¡vars> ¡ return ¡

slide-12
SLIDE 12

University ¡of ¡Washington ¡

Procedure ¡Call ¡Overview ¡

12 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

Caller ¡ Callee ¡

¡ ¡ ¡ ¡… ¡ <save ¡regs> ¡ <set ¡up ¡args> ¡ call ¡ <clean ¡up ¡args> ¡ <restore ¡regs> ¡ <find ¡return ¡val> ¡ ¡ ¡ ¡ ¡… ¡ ¡ <save ¡regs> ¡ <create ¡local ¡vars> ¡ ¡ ¡ ¡… ¡ <set ¡up ¡return ¡val> ¡ <destroy ¡local ¡vars> ¡ <restore ¡regs> ¡ return ¡

 The ¡convenPon ¡of ¡where ¡to ¡leave/find ¡things ¡is ¡called ¡the ¡

procedure ¡call ¡linkage ¡

  • Details ¡vary ¡between ¡systems ¡
  • We ¡will ¡see ¡the ¡convenGon ¡for ¡IA32/Linux ¡in ¡detail ¡
  • What ¡could ¡happen ¡if ¡our ¡program ¡didn’t ¡follow ¡these ¡convenGons? ¡
slide-13
SLIDE 13

University ¡of ¡Washington ¡

Procedure ¡Control ¡Flow ¡

 Use ¡stack ¡to ¡support ¡procedure ¡call ¡and ¡return ¡  Procedure ¡call: ¡call label

  • Push ¡return ¡address ¡on ¡stack ¡
  • Jump ¡to ¡label ¡

13 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-14
SLIDE 14

University ¡of ¡Washington ¡

Procedure ¡Control ¡Flow ¡

 Use ¡stack ¡to ¡support ¡procedure ¡call ¡and ¡return ¡  Procedure ¡call: ¡call label

  • Push ¡return ¡address ¡on ¡stack ¡
  • Jump ¡to ¡label ¡

 Return ¡address: ¡

  • Address ¡of ¡instrucGon ¡aOer ¡call ¡
  • Example ¡from ¡disassembly: ¡

804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax

  • Return ¡address ¡= ¡0x8048553 ¡

 Procedure ¡return: ¡ret

  • Pop ¡return ¡address ¡from ¡stack ¡
  • Jump ¡to ¡address

14 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-15
SLIDE 15

University ¡of ¡Washington ¡

%esp %eip 0x804854e

Procedure ¡Call ¡Example ¡

0x108 0x10c 0x110 123 0x108 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax %eip: program ¡counter

15 ¡

call 8048b90

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-16
SLIDE 16

University ¡of ¡Washington ¡

%esp %eip %esp %eip 0x804854e 0x108 0x108 0x10c 0x110 0x104 0x804854e 123

Procedure ¡Call ¡Example ¡

0x108 0x10c 0x110 123 0x108 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax %eip: program ¡counter

16 ¡

call 8048b90

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-17
SLIDE 17

University ¡of ¡Washington ¡

%esp %eip %esp %eip 0x804854e 0x108 0x108 0x10c 0x110 0x104 0x804854e 123

Procedure ¡Call ¡Example ¡

0x108 0x10c 0x110 123 0x108 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax %eip: program ¡counter

17 ¡

call 8048b90

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

0x8048553

slide-18
SLIDE 18

University ¡of ¡Washington ¡

%esp %eip %esp %eip 0x804854e 0x108 0x108 0x10c 0x110 0x104 0x804854e 0x8048553 123

Procedure ¡Call ¡Example ¡

0x108 0x10c 0x110 123 0x108 call 8048b90 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x8048553 0x104 %eip: program ¡counter

18 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-19
SLIDE 19

University ¡of ¡Washington ¡

%esp %eip %esp %eip 0x8048553 0x108 0x108 0x10c 0x110 0x104 0x804854e 0x8048553 123

Procedure ¡Call ¡Example ¡

0x108 0x10c 0x110 123 0x108 call 8048b90 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax 0x8048b90 0x104 %eip: program ¡counter

19 ¡

+ 0x000063d

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-20
SLIDE 20

University ¡of ¡Washington ¡

%esp %eip 0x104 0x8048591 0x104 0x108 0x10c 0x110 0x8048553 123

Procedure ¡Return ¡Example ¡

8048591: c3 ret %eip: program ¡counter

20 ¡

ret

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-21
SLIDE 21

University ¡of ¡Washington ¡

%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x104 0x108 0x10c 0x110 0x8048553 123

Procedure ¡Return ¡Example ¡

0x108 0x10c 0x110 123 8048591: c3 ret 0x8048553 %eip: program ¡counter

21 ¡

ret

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-22
SLIDE 22

University ¡of ¡Washington ¡

%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x104 0x108 0x10c 0x110 0x8048553 123

Procedure ¡Return ¡Example ¡

0x108 0x10c 0x110 123 ret 8048591: c3 ret 0x8048553 0x8048553 %eip: program ¡counter

22 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-23
SLIDE 23

University ¡of ¡Washington ¡

%esp %eip 0x104 %esp %eip 0x8048591 0x8048591 0x104 0x104 0x108 0x10c 0x110 0x8048553 123

Procedure ¡Return ¡Example ¡

0x108 0x10c 0x110 123 ret 8048591: c3 ret 0x108 0x8048553 0x8048553 %eip: program ¡counter

23 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-24
SLIDE 24

University ¡of ¡Washington ¡

Stack-­‑Based ¡Languages ¡

 Languages ¡that ¡support ¡recursion ¡

  • e.g., ¡C, ¡Pascal, ¡Java ¡
  • Code ¡must ¡be ¡re-­‑entrant ¡
  • MulGple ¡simultaneous ¡instanGaGons ¡of ¡single ¡procedure ¡
  • Need ¡some ¡place ¡to ¡store ¡state ¡of ¡each ¡instanGaGon ¡
  • Arguments ¡
  • Local ¡variables ¡
  • Return ¡pointer ¡

 Stack ¡discipline ¡

  • State ¡for ¡a ¡given ¡procedure ¡needed ¡for ¡a ¡limited ¡Gme ¡
  • StarGng ¡from ¡when ¡it ¡is ¡called ¡to ¡when ¡it ¡returns ¡
  • Callee ¡always ¡returns ¡before ¡caller ¡does ¡

 Stack ¡allocated ¡in ¡frames ¡

  • State ¡for ¡a ¡single ¡procedure ¡instanGaGon ¡

24 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-25
SLIDE 25

University ¡of ¡Washington ¡

Call ¡Chain ¡Example ¡

yoo(…) {

  • who();
  • }

who(…) {

  • • •

amI();

  • • •

amI();

  • • •

} amI(…) {

  • amI();
  • }

yoo who amI amI amI Example ¡ Call ¡Chain ¡ amI Procedure ¡ ¡amI ¡ ¡is ¡recursive ¡ (calls ¡itself) ¡

25 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-26
SLIDE 26

University ¡of ¡Washington ¡

Frame ¡Pointer: ¡%ebp ¡

Stack ¡Frames ¡

 Contents ¡

  • Local ¡variables ¡
  • FuncGon ¡arguments ¡
  • Return ¡informaGon ¡
  • Temporary ¡space ¡

 Management ¡

  • Space ¡allocated ¡when ¡procedure ¡is ¡entered ¡
  • “Set-­‑up” ¡code ¡
  • Space ¡deallocated ¡upon ¡return ¡
  • “Finish” ¡code ¡

Stack ¡Pointer: ¡%esp ¡

Previous ¡ Frame ¡ Stack ¡“Top” ¡

26 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

Frame ¡ for ¡ current ¡ proc

slide-27
SLIDE 27

University ¡of ¡Washington ¡

Example ¡

yoo(…) {

  • who();
  • }

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

27 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-28
SLIDE 28

University ¡of ¡Washington ¡

who(…) {

  • • •

amI();

  • • •

amI();

  • • •

}

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who

28 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-29
SLIDE 29

University ¡of ¡Washington ¡

amI(…) {

  • amI();
  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI

29 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-30
SLIDE 30

University ¡of ¡Washington ¡

amI(…) {

  • amI();
  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI amI

30 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-31
SLIDE 31

University ¡of ¡Washington ¡

amI(…) {

  • amI();
  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI amI amI

31 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-32
SLIDE 32

University ¡of ¡Washington ¡

amI(…) {

  • amI();
  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI amI

32 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-33
SLIDE 33

University ¡of ¡Washington ¡

amI(…) {

  • amI();
  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI

33 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-34
SLIDE 34

University ¡of ¡Washington ¡

who(…) {

  • • •

amI();

  • • •

amI();

  • • •

}

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who

34 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-35
SLIDE 35

University ¡of ¡Washington ¡

amI(…) {

  • }

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who amI

35 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-36
SLIDE 36

University ¡of ¡Washington ¡

who(…) {

  • • •

amI();

  • • •

amI();

  • • •

}

Example ¡

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

who

36 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-37
SLIDE 37

University ¡of ¡Washington ¡

Example ¡

yoo(…) {

  • who();
  • }

yoo who amI amI amI amI yoo %ebp ¡ %esp ¡

Stack ¡

37 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-38
SLIDE 38

University ¡of ¡Washington ¡

IA32/Linux ¡Stack ¡Frame ¡

 Current ¡Stack ¡Frame ¡(“Top” ¡to ¡Bobom) ¡

  • “Argument ¡build” ¡area ¡

(parameters ¡for ¡funcGon ¡ ¡ about ¡to ¡be ¡called) ¡

  • Local ¡variables ¡

(if ¡can’t ¡be ¡kept ¡in ¡registers) ¡

  • Saved ¡register ¡context ¡ ¡

(when ¡reusing ¡registers) ¡

  • Old ¡frame ¡pointer ¡(for ¡caller) ¡

 Caller’s ¡Stack ¡Frame ¡

  • Return ¡address ¡
  • Pushed ¡by ¡call ¡instrucGon ¡
  • Arguments ¡for ¡this ¡call ¡

Return ¡Addr ¡ Saved ¡ Registers ¡ + ¡ Local ¡ Variables ¡ Argument ¡ Build ¡ Old ¡%ebp ¡ Arguments ¡ Caller ¡ Frame ¡ Frame ¡pointer ¡ %ebp ¡ Stack ¡pointer ¡ %esp ¡

38 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-39
SLIDE 39

University ¡of ¡Washington ¡

RevisiPng ¡swap ¡

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); }

39 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-40
SLIDE 40

University ¡of ¡Washington ¡

RevisiPng ¡swap ¡

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } call_swap:

  • • •

pushl $zip2 # Global Var pushl $zip1 # Global Var call swap

  • • •

Calling ¡swap ¡from ¡call_swap

40 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-41
SLIDE 41

University ¡of ¡Washington ¡

RevisiPng ¡swap ¡

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } call_swap:

  • • •

pushl $zip2 # Global Var pushl $zip1 # Global Var call swap

  • • •

&zip2 &zip1 Rtn ¡adr ¡ %esp

ResulPng ¡ Stack ¡

  • ¡
  • ¡
  • Calling ¡swap ¡from ¡call_swap

41 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-42
SLIDE 42

University ¡of ¡Washington ¡

RevisiPng ¡swap ¡

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body ¡ Set ¡ Up ¡ Finish ¡

42 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-43
SLIDE 43

University ¡of ¡Washington ¡

swap ¡Setup ¡#1 ¡

swap: pushl %ebp movl %esp,%ebp pushl %ebx

ResulPng ¡Stack? ¡

&zip2 &zip1 Rtn ¡adr ¡ %esp

Entering ¡Stack ¡

  • ¡
  • ¡
  • %ebp

43 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-44
SLIDE 44

University ¡of ¡Washington ¡

swap ¡Setup ¡#1 ¡

swap: pushl %ebp movl %esp,%ebp pushl %ebx

ResulPng ¡Stack ¡

&zip2 &zip1 Rtn ¡adr ¡ %esp

Entering ¡Stack ¡

  • ¡
  • ¡
  • %ebp

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

44 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-45
SLIDE 45

University ¡of ¡Washington ¡

swap ¡Setup ¡#2 ¡

swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn ¡adr ¡ %esp

Entering ¡Stack ¡

  • ¡
  • ¡
  • %ebp

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

45 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-46
SLIDE 46

University ¡of ¡Washington ¡

swap ¡Setup ¡#3 ¡

swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn ¡adr ¡ %esp

Entering ¡Stack ¡

  • ¡
  • ¡
  • %ebp

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • ResulPng ¡Stack

¡

46 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

%esp Old ¡%ebx ¡

slide-47
SLIDE 47

University ¡of ¡Washington ¡

12 ¡ 8 ¡ 4 ¡

swap ¡Body ¡

&zip2 &zip1 Rtn ¡adr ¡ %esp

Entering ¡Stack ¡

  • ¡
  • ¡
  • %ebp

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

Old ¡%ebx ¡ movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp . . . Offset ¡rela8ve ¡ ¡ to ¡new ¡%ebp

47 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-48
SLIDE 48

University ¡of ¡Washington ¡

swap ¡Finish ¡#1 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

ResulPng ¡Stack? ¡

48 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-49
SLIDE 49

University ¡of ¡Washington ¡

swap ¡Finish ¡#1 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

Old ¡%ebx ¡

ObservaPon: ¡Saved ¡and ¡restored ¡ ¡ register ¡%ebx

49 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-50
SLIDE 50

University ¡of ¡Washington ¡

swap ¡Finish ¡#2 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

50 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-51
SLIDE 51

University ¡of ¡Washington ¡

swap ¡Finish ¡#3 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

ResulPng ¡Stack ¡

yp xp Rtn ¡adr ¡ %ebp

  • ¡
  • ¡
  • %esp

51 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-52
SLIDE 52

University ¡of ¡Washington ¡

swap ¡Finish ¡#4 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

52 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-53
SLIDE 53

University ¡of ¡Washington ¡

Disassembled ¡swap ¡

080483a4 <swap>: 80483a4: 55 push %ebp 80483a5: 89 e5 mov %esp,%ebp 80483a7: 53 push %ebx 80483a8: 8b 55 08 mov 0x8(%ebp),%edx 80483ab: 8b 4d 0c mov 0xc(%ebp),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: 89 02 mov %eax,(%edx) 80483b4: 89 19 mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret 8048409: e8 96 ff ff ff call 80483a4 <swap> 804840e: 8b 45 f8 mov 0xfffffff8(%ebp),%eax

Calling ¡Code ¡

53 ¡

mov %ebp,%esp pop %ebp 0x0804840e + 0xffffff96 = 0x080483a4

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-54
SLIDE 54

University ¡of ¡Washington ¡

swap ¡Finish ¡#4 ¡

yp xp Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp

  • ¡
  • ¡
  • %esp

swap’s ¡Stack ¡

Old ¡%ebx ¡ movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp %ebp

  • ¡
  • ¡
  • %esp

ResulPng ¡Stack ¡

 ObservaPon ¡

  • Saved ¡& ¡restored ¡register ¡%ebx ¡
  • Didn’t ¡do ¡so ¡for ¡%eax, ¡%ecx, ¡or ¡%edx ¡

54 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-55
SLIDE 55

University ¡of ¡Washington ¡

Register ¡Saving ¡ConvenPons ¡

 When ¡procedure ¡yoo ¡calls ¡who: ¡

  • yoo ¡is ¡the ¡caller ¡
  • who ¡is ¡the ¡callee ¡

 Can ¡a ¡register ¡be ¡used ¡for ¡temporary ¡storage? ¡

  • Contents ¡of ¡register ¡%edx ¡overwri_en ¡by ¡who ¡

yoo:

  • • •

movl $12345, %edx call who addl %edx, %eax

  • • •

ret who:

  • • •

movl 8(%ebp), %edx addl $98195, %edx

  • • •

ret

55 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-56
SLIDE 56

University ¡of ¡Washington ¡

Register ¡Saving ¡ConvenPons ¡

 When ¡procedure ¡yoo ¡calls ¡who: ¡

  • yoo ¡is ¡the ¡caller ¡
  • who ¡is ¡the ¡callee ¡

 Can ¡a ¡register ¡be ¡used ¡for ¡temporary ¡storage? ¡  ConvenPons ¡

  • “Caller ¡Save” ¡
  • Caller ¡saves ¡temporary ¡values ¡in ¡its ¡frame ¡before ¡calling ¡
  • “Callee ¡Save” ¡
  • Callee ¡saves ¡temporary ¡values ¡in ¡its ¡frame ¡before ¡using ¡

56 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-57
SLIDE 57

University ¡of ¡Washington ¡

IA32/Linux ¡Register ¡Usage ¡

 %eax, %edx, %ecx

  • Caller ¡saves ¡prior ¡to ¡call ¡if ¡

values ¡are ¡used ¡later ¡

 %eax

  • also ¡used ¡to ¡return ¡

integer ¡value ¡

 %ebx, %esi, %edi

  • Callee ¡saves ¡if ¡wants ¡to ¡

use ¡them ¡

 %esp, %ebp

  • special ¡form ¡of ¡callee ¡save ¡– ¡restored ¡to ¡original ¡values ¡upon ¡exit ¡from ¡

procedure ¡

%eax %edx %ecx %ebx %esi %edi %esp %ebp

Caller-­‑Save ¡ Temporaries ¡ Callee-­‑Save ¡ Temporaries ¡ Special ¡

57 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡

slide-58
SLIDE 58

University ¡of ¡Washington ¡

Example: ¡Pointers ¡to ¡Local ¡Variables ¡

Winter ¡2013 ¡

58 ¡

Procedures ¡and ¡Stacks ¡I ¡

void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1,accum); } } int sfact(int x) { int val = 1; s_helper(x, &val); return val; }

Top-­‑Level ¡Call ¡ Recursive ¡Procedure ¡

 Pass ¡pointer ¡to ¡update ¡locaPon ¡

slide-59
SLIDE 59

University ¡of ¡Washington ¡

  • Temp. ¡

Space ¡ %esp

CreaPng ¡& ¡IniPalizing ¡Pointer ¡

int sfact(int x) { int val = 1; s_helper(x, &val); return val; } _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1

Variable ¡val ¡must ¡be ¡stored ¡on ¡stack ¡

  • Because: ¡Need ¡to ¡create ¡pointer ¡to ¡it ¡

Compute ¡pointer ¡as ¡-4(%ebp) ¡

Push ¡on ¡stack ¡as ¡second ¡argument ¡

IniPal ¡part ¡of ¡sfact ¡

x Rtn ¡adr ¡ Old ¡%ebp ¡ 4 8

  • 4

val = 1 Unused ¡

  • 12
  • 8
  • 16

_sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1

Winter ¡2013 ¡

59 ¡

Procedures ¡and ¡Stacks ¡I ¡

%esp %esp %ebp

slide-60
SLIDE 60

University ¡of ¡Washington ¡

Passing ¡Pointer ¡

int sfact(int x) { int val = 1; s_helper(x, &val); return val; } leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val

  • • •

# Finish

Calling ¡s_helper ¡from ¡sfact ¡

x Rtn ¡adr ¡ Old ¡%ebp ¡ %ebp 4 8 val = 1

  • 4

Unused ¡

  • 12
  • 8
  • 16

%esp x &val Stack ¡at ¡Pme ¡of ¡call: ¡ leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val

  • • •

# Finish leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val

  • • •

# Finish val=x!

Winter ¡2013 ¡

60 ¡

Procedures ¡and ¡Stacks ¡I ¡

Variable ¡val ¡must ¡be ¡stored ¡on ¡stack ¡

  • Because: ¡Need ¡to ¡create ¡pointer ¡to ¡it ¡

Compute ¡pointer ¡as ¡-4(%ebp) ¡

Push ¡on ¡stack ¡as ¡second ¡argument ¡

slide-61
SLIDE 61

University ¡of ¡Washington ¡

IA ¡32 ¡Procedure ¡Summary ¡

 Important ¡points: ¡

  • IA32 ¡procedures ¡are ¡a ¡combinaGon ¡of ¡instruc5ons ¡

and ¡conven5ons ¡

  • ConvenGons ¡prevent ¡funcGons ¡from ¡

disrupGng ¡each ¡other ¡

  • Stack ¡is ¡the ¡right ¡data ¡structure ¡for ¡procedure ¡

call ¡/ ¡return ¡

  • If ¡P ¡calls ¡Q, ¡then ¡Q ¡returns ¡before ¡P ¡

 Recursion ¡handled ¡by ¡normal ¡calling ¡

convenPons ¡

  • Can ¡safely ¡store ¡values ¡in ¡local ¡stack ¡frame ¡and ¡in ¡

callee-­‑saved ¡registers ¡

  • Put ¡funcGon ¡arguments ¡at ¡top ¡of ¡stack ¡
  • Result ¡returned ¡in ¡%eax

Return ¡Addr ¡ Saved ¡ Registers ¡ + ¡ Local ¡ Variables ¡ Argument ¡ Build ¡ Old ¡%ebp ¡ Arguments ¡ Caller ¡ Frame ¡ %ebp ¡ %esp ¡

61 ¡

Winter ¡2013 ¡ Procedures ¡and ¡Stacks ¡I ¡