+ All Categories
Home > Documents > The Hardware/Software Interface CSE351 Winter 2013

The Hardware/Software Interface CSE351 Winter 2013

Date post: 06-Jan-2016
Category:
Upload: bikita
View: 16 times
Download: 0 times
Share this document with a friend
Description:
The Hardware/Software Interface CSE351 Winter 2013. Procedures and Stacks I. Data & addressing Integers & floats Machine code & C x86 assembly programming Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C. Roadmap. C:. Java:. - PowerPoint PPT Presentation
Popular Tags:
62
University of Washington Procedures and Stacks I The Hardware/Software Interface CSE351 Winter 2013
Transcript
Page 1: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

Procedures and Stacks I

The Hardware/Software InterfaceCSE351 Winter 2013

Page 2: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

2Procedures and Stacks I

Roadmap

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:

01110100000110001000110100000100000000101000100111000010110000011111101000011111

Computer system:

OS:

Data & addressingIntegers & floatsMachine code & Cx86 assembly programmingProcedures & stacksArrays & structsMemory & cachesProcessesVirtual memoryMemory allocationJava vs. C

Winter 2013

Page 3: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

3Procedures and Stacks I

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 function returns, how does it know where to return

to?

To answer these questions, we need a call stack …

Winter 2013

Page 4: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

4Procedures and Stacks I

Memory Layout

Winter 2013

Instructions

Literals

Static Data

Dynamic Data(Heap)

Stack

literals (e.g., “example”)

static variables(including global variables (C))

variables allocated withnew or malloc

local variables;procedure context

0

2N-1

Page 5: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

5Procedures and Stacks I

Memory Layout

Winter 2013

Instructions

Literals

Static Data

Dynamic Data(Heap)

Stack Managed “automatically”(by compiler)writable; not executable

Managed by programmerwritable; not executable

Initialized when process startswritable; not executable

Initialized when process startsRead-only; not executable

Initialized when process startsRead-only; executable

Page 6: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

6Procedures and Stacks I

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 GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom”

Winter 2013

Page 7: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

7Procedures and Stacks I

IA32 Call Stack: Push

pushl Src

Stack GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom”

Stack Pointer: %esp

Winter 2013

Page 8: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

8Procedures and Stacks I

IA32 Call Stack: Push

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

given by %esp

Stack GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom”

Stack Pointer: %esp-4

Winter 2013

Page 9: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

9Procedures and Stacks I

IA32 Call Stack: Pop

Stack Pointer: %esp

Stack GrowsDown

IncreasingAddresses

Stack “Top”

Stack “Bottom” popl Dest

Winter 2013

Page 10: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

10Procedures and Stacks I

IA32 Call Stack: Pop

Stack Pointer: %espStack Grows

Down

IncreasingAddresses

Stack “Top”

Stack “Bottom” popl Dest

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

+4

Winter 2013

Page 11: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

11Procedures and Stacks I

Procedure Call Overview

Winter 2013

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

Page 12: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

12Procedures and Stacks I

Procedure Call Overview

Winter 2013

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 convention of where to leave/find things is called the procedure call linkage Details vary between systems We will see the convention for IA32/Linux in detail What could happen if our program didn’t follow these conventions?

Page 13: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

13Procedures and Stacks I

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label

Push return address on stack Jump to label

Winter 2013

Page 14: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

14Procedures and Stacks I

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 instruction after 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

Winter 2013

Page 15: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

15Procedures and Stacks I

%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

call 8048b90

Winter 2013

Page 16: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

16Procedures and Stacks I

%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

call 8048b90

Winter 2013

Page 17: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

17Procedures and Stacks I

%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

call 8048b90

Winter 2013

0x8048553

Page 18: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

18Procedures and Stacks I

%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 counterWinter 2013

Page 19: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

19Procedures and Stacks I

%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

+ 0x000063d

Winter 2013

Page 20: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

20Procedures and Stacks I

%esp

%eip

0x104

0x8048591

0x104

0x108

0x10c

0x110

0x8048553

123

Procedure Return Example8048591: c3 ret

%eip: program counter

ret

Winter 2013

Page 21: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

21Procedures and Stacks I

%esp

%eip

0x104

%esp

%eip 0x80485910x8048591

0x1040x104

0x108

0x10c

0x110

0x8048553

123

Procedure Return Example

0x108

0x10c

0x110

123

8048591: c3 ret

0x8048553

%eip: program counter

ret

Winter 2013

Page 22: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

22Procedures and Stacks I

%esp

%eip

0x104

%esp

%eip 0x80485910x8048591

0x1040x104

0x108

0x10c

0x110

0x8048553

123

Procedure Return Example

0x108

0x10c

0x110

123

ret

8048591: c3 ret

0x8048553

0x8048553

%eip: program counterWinter 2013

Page 23: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

23Procedures and Stacks I

%esp

%eip

0x104

%esp

%eip 0x80485910x8048591

0x1040x104

0x108

0x10c

0x110

0x8048553

123

Procedure Return Example

0x108

0x10c

0x110

123

ret

8048591: c3 ret

0x108

0x8048553

0x8048553

%eip: program counterWinter 2013

Page 24: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

24Procedures and Stacks I

Stack-Based Languages Languages that support recursion

e.g., C, Pascal, Java Code must be re-entrant

Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation

Arguments Local variables Return pointer

Stack discipline State for a given procedure needed for a limited time

Starting from when it is called to when it returns Callee always returns before caller does

Stack allocated in frames State for a single procedure instantiation

Winter 2013

Page 25: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

25Procedures and Stacks I

Call Chain Example

yoo(…){

••who();••}

who(…){

• • •amI();• • •amI();• • •}

amI(…){

••amI();••}

yoo

who

amI

amI

amI

ExampleCall Chain

amI

Procedure amI is recursive(calls itself)

Winter 2013

Page 26: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

26Procedures and Stacks I

Frame Pointer: %ebp

Stack Frames Contents

Local variables Function arguments Return information Temporary space

Management Space allocated when procedure is entered

“Set-up” code Space deallocated upon return

“Finish” code

Stack Pointer: %esp

PreviousFrame

Stack “Top”

Winter 2013

Frame for

currentproc

Page 27: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

27Procedures and Stacks I

Example

yoo(…){

••who();••}

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

Winter 2013

Page 28: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

28Procedures and Stacks I

who(…){

• • •amI();• • •amI();• • •}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

Winter 2013

Page 29: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

29Procedures and Stacks I

amI(…){

••amI();••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

Winter 2013

Page 30: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

30Procedures and Stacks I

amI(…){

••amI();••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

amI

Winter 2013

Page 31: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

31Procedures and Stacks I

amI(…){

••amI();••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

amI

amI

Winter 2013

Page 32: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

32Procedures and Stacks I

amI(…){

••amI();••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

amI

Winter 2013

Page 33: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

33Procedures and Stacks I

amI(…){

••amI();••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

Winter 2013

Page 34: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

34Procedures and Stacks I

who(…){

• • •amI();• • •amI();• • •}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

Winter 2013

Page 35: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

35Procedures and Stacks I

amI(…){

•••••}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

amI

Winter 2013

Page 36: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

36Procedures and Stacks I

who(…){

• • •amI();• • •amI();• • •}

Example

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

who

Winter 2013

Page 37: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

37Procedures and Stacks I

Example

yoo(…){

••who();••}

yoo

who

amI

amI

amI

amI

yoo

%ebp

%esp

Stack

Winter 2013

Page 38: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

38Procedures and Stacks I

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom)

“Argument build” area(parameters for function 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 instruction Arguments for this call

Return Addr

SavedRegisters

+Local

Variables

ArgumentBuild

Old %ebp

Arguments

CallerFrame

Frame pointer%ebp

Stack pointer%esp

Winter 2013

Page 39: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

39Procedures and Stacks I

Revisiting 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);}

Winter 2013

Page 40: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

40Procedures and Stacks I

Revisiting 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 Varpushl $zip1 # Global Varcall swap• • •

Calling swap from call_swap

Winter 2013

Page 41: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

41Procedures and Stacks I

Revisiting 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 Varpushl $zip1 # Global Varcall swap• • •

&zip2

&zip1

Rtn adr %esp

ResultingStack•

••

Calling swap from call_swap

Winter 2013

Page 42: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

42Procedures and Stacks I

Revisiting swap

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:pushl %ebpmovl %esp,%ebppushl %ebx

movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

Body

SetUp

Finish

Winter 2013

Page 43: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

43Procedures and Stacks I

swap Setup #1

swap:pushl %ebpmovl %esp,%ebppushl %ebx

Resulting Stack?

&zip2

&zip1

Rtn adr %esp

Entering Stack

•••

%ebp

Winter 2013

Page 44: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

44Procedures and Stacks I

swap Setup #1

swap:pushl %ebpmovl %esp,%ebppushl %ebx

Resulting Stack

&zip2

&zip1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp

%ebp•••

%esp

Winter 2013

Page 45: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

45Procedures and Stacks I

swap Setup #2

swap:pushl %ebpmovl %esp,%ebppushl %ebx

&zip2

&zip1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

Resulting Stack

Winter 2013

Page 46: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

46Procedures and Stacks I

swap Setup #3

swap:pushl %ebpmovl %esp,%ebppushl %ebx

&zip2

&zip1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

Resulting Stack

Winter 2013

%espOld %ebx

Page 47: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

47Procedures and Stacks I

1284

swap Body

&zip2

&zip1

Rtn adr %esp

Entering Stack

•••

%ebp

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

Resulting Stack

Old %ebx

movl 12(%ebp),%ecx # get ypmovl 8(%ebp),%edx # get xp. . .

Offset relative to new %ebp

Winter 2013

Page 48: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

48Procedures and Stacks I

swap Finish #1

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

Resulting Stack?

Winter 2013

Page 49: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

49Procedures and Stacks I

swap Finish #1

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

Resulting Stack

Old %ebx

Observation: Saved and restored register %ebx

Winter 2013

Page 50: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

50Procedures and Stacks I

swap Finish #2

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

Resulting Stack

Winter 2013

Page 51: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

51Procedures and Stacks I

swap Finish #3

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

Resulting Stack

yp

xp

Rtn adr

%ebp•••

%esp

Winter 2013

Page 52: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

52Procedures and Stacks I

swap Finish #4

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

yp

xp

%ebp•••

%esp

Resulting Stack

Winter 2013

Page 53: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

53Procedures and Stacks I

Disassembled swap080483a4 <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

mov %ebp,%esppop %ebp

Winter 2013

Page 54: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

54Procedures and Stacks I

swap Finish #4

yp

xp

Rtn adr

Old %ebp %ebp

•••

%esp

swap’s Stack

Old %ebx

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

yp

xp

%ebp•••

%esp

Resulting Stack

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

Winter 2013

Page 55: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

55Procedures and Stacks I

Register Saving Conventions 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 overwritten by who

yoo:• • •movl $12345, %edxcall whoaddl %edx, %eax

• • •ret

who:• • •movl 8(%ebp), %edxaddl $98195, %edx

• • •ret

Winter 2013

Page 56: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

56Procedures and Stacks I

Register Saving Conventions When procedure yoo calls who:

yoo is the caller who is the callee

Can a register be used for temporary storage? Conventions

“Caller Save” Caller saves temporary values in its frame before calling

“Callee Save” Callee saves temporary values in its frame before using

Winter 2013

Page 57: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

57Procedures and Stacks I

IA32/Linux Register Usage %eax, %edx, %ecx

Caller saves prior to call ifvalues 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-SaveTemporaries

Callee-SaveTemporaries

Special

Winter 2013

Page 58: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

58Procedures and Stacks I

Example: Pointers to Local Variables

Winter 2013

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 CallRecursive Procedure

Pass pointer to update location

Page 59: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

59Procedures and Stacks I

Temp.Space

%esp

Creating & Initializing Pointer

int sfact(int x){ int val = 1; s_helper(x, &val); return val;}

_sfact:pushl %ebp # Save %ebpmovl %esp,%ebp # Set %ebpsubl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = xmovl $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

Initial part of sfact

x

Rtn adr

Old %ebp 0

4

8

-4 val = 1

Unused-12

-8

-16

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

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

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

Winter 2013

%esp

%esp%ebp

Page 60: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

60Procedures and Stacks I

Passing Pointer

int sfact(int x){ int val = 1; s_helper(x, &val); return val;}

leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish

Calling s_helper from sfact

x

Rtn adr

Old %ebp %ebp 0

4

8

val = 1 -4

Unused-12

-8

-16 %esp

x

&val

Stack at time of call:

leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish

leal -4(%ebp),%eax # Compute &valpushl %eax # Push on stackpushl %edx # Push xcall s_helper # callmovl -4(%ebp),%eax # Return val• • • # Finish

val=x!

Winter 2013

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

Page 61: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

61Procedures and Stacks I

IA 32 Procedure Summary Important points:

IA32 procedures are a combination of instructionsand conventions

Conventions prevent functions fromdisrupting each other

Stack is the right data structure for procedurecall / return

If P calls Q, then Q returns before P Recursion handled by normal calling

conventions Can safely store values in local stack frame and in

callee-saved registers Put function arguments at top of stack Result returned in %eax

Return Addr

SavedRegisters

+Local

Variables

ArgumentBuild

Old %ebp

Arguments

CallerFrame

%ebp

%esp

Winter 2013

Page 62: The Hardware/Software Interface CSE351 Winter 2013

University of Washington

62Procedures and Stacks IWinter 2013


Recommended