+ All Categories
Home > Documents > MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf ·...

MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf ·...

Date post: 28-Feb-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
46
Midterm Exam Midterm Exam Midterm Exam Midterm Exam d l 16 18 Midterm Exam Results Class A (Average 150/230) 12 14 Class A (Average = 150/230) Class B (Average = 137/230) 8 10 4 6 0 2 1 CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected]) 020 2140 4160 6180 81100 101120 121140 141160 161180 181200 201230
Transcript
Page 1: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Midterm ExamMidterm ExamMidterm ExamMidterm Exam

d l

16

18

Midterm Exam Results

Class A (Average 150/230)

12

14

Class A (Average = 150/230)Class B (Average = 137/230)

8

10

4

6

0

2

1CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

0‐20 21‐40 41‐60 61‐80 81‐100 101‐120 121‐140 141‐160 161‐180 181‐200 201‐230

Page 2: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

NoticeNoticeNoticeNotice Midterm exam results

• Check out your score in GLS• Your exam papers and answer sheets are available in p p

#400629.• If you have any claims, please visit my office during

5/10 ~ 5/14.

First programming assignment results• They will be posted on the course homepage soon.y p p g• Any claims to 김형준 조교 ([email protected],

#400629)

2CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 3: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

NoticeNoticeNoticeNotice Project assignment #2j g

• Due date is 5/9 11:59PM• Send your code and document to y

[email protected] and [email protected]

• Any questions to 김형준 조교 ([email protected])

Guest lecturesGuest lectures• Class on 5/3 by Prof. Hwansoo Han• Class on 5/6 by Prof Joonwon LeeClass on 5/6 by Prof. Joonwon Lee

3CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 4: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

A bl IIIA bl IIIAssembly III:Procedures

Assembly III:ProceduresProceduresProcedures

Jin-Soo Kim ([email protected])Jin Soo Kim ([email protected])Computer Systems Laboratory

Sungkyunkwan Universityhtt // l kk dhttp://csl.skku.edu

Page 5: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32 Stack (1)IA-32 Stack (1)IA 32 Stack (1)IA 32 Stack (1) Characteristics Stack “Bottom”

• Region of memory managed with stack

Stack Bottom

discipline• Grows toward lower

dd

IncreasingAddresses

addresses• Register %esp

i di t l t t kindicates lowest stack address– address of top element

StackPointer

Stack GrowsDown

address of top element Pointer%esp

5CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Stack “Top”

Page 6: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32 Stack (2)IA-32 Stack (2)IA 32 Stack (2)IA 32 Stack (2) Pushing Stack “Bottom”g

• pushl Src• Fetch operand at Src Increasingp• Decrement %esp by 4• Write operand at

IncreasingAddresses

te ope a d ataddress given by %esp

St k GStack GrowsDown

StackPointerPointer%esp -4

6CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Stack “Top”

Page 7: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32 Stack (3)IA-32 Stack (3)IA 32 Stack (3)IA 32 Stack (3) Popping Stack “Bottom”pp g

• popl Dest• Read operand at Increasingp

address given by %esp• Increment %esp by 4

IncreasingAddresses

• Write to Dest

St k GStack GrowsDown

StackPointerPointer%esp +4

7CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Stack “Top”

Page 8: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32 Stack (4)IA-32 Stack (4)IA 32 Stack (4)IA 32 Stack (4) Stack operation examplesp p

pushl %eax popl %edx

0x10c

0x110

0x10c

0x110

0x10c

0x110

0x104

0x108

0x10c

0x104 213

1230x108

0x10c

123 0x108

0x10c

123

213

%esp

%eax

%edx

%esp

%eax

%edx

%esp

%eax

%edx 555

0x108

555

213

555

213

0x108 0x104

213

0x104

213

0x108

8CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

%esp%esp%esp 0x1080x108 0x104 0x1040x108

Page 9: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Procedure Control FlowProcedure Control FlowProcedure Control FlowProcedure Control Flow Use stack to support procedure call and pp p

return Procedure callProcedure call

• call label– Push return address on stack– Jump to label

• Return address value– Address of instruction beyond call

Procedure return• ret

– Pop address from stack

9CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

– Jump to address

Page 10: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Procedure Call ExampleProcedure Call ExampleProcedure Call ExampleProcedure Call Example804854e: e8 3d 06 00 00 call 8048b90 <main>

call 8048b90

8048553: 50 pushl %eax

0x1100x110

0x08048553+0x0000063d

0x108

0x10c

1230x108

0x10c

123

=0x08048b90

%esp %esp 0x108

0x104 0x8048553

0x108 0x104%esp

%eip

%esp

%eip 0x804854e

0x108

0x804854e

0x108

0x8048b90

0x104

10CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

%eip is program counter

Page 11: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Procedure Return ExampleProcedure Return ExampleProcedure Return ExampleProcedure Return Example8048591: c3 ret

ret

0x110 0x110

0 104

0x108

0x10c

0 8048553

123 0x108

0x10c

123

0 8048553

%esp

0x104

%esp 0x1040x104

0x8048553

0x108

0x8048553

p

%eip

p

%eip 0x80485910x8048591 0x8048553

11CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

%eip is program counter

Page 12: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack-based LanguagesStack-based LanguagesStack based LanguagesStack based Languages Languages that support recursion

• e.g., C, Pascal, Java, etc.• Code must be “Reentrant”

– Multiple simultaneous instantiations of single procedure

• Need some place to store state of each instantiationArguments local variables return pointer– Arguments, local variables, return pointer

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

– From when called to when return

• Callee returns before caller does

Stack allocated in frames• State for single procedure instantiation

12CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

g p

Page 13: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (1)Stack Frames (1)Stack Frames (1)Stack Frames (1)Call ChainCode Structure

yoo(…){

yoo

••who();•

who(…){

• • •

who

•}

amI();• • •amI();

amI( )

amI

amI

amI

• • •}

amI(…){

••

amI

amIamI();••

• Procedure amIrecursive

13CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

}recursive

Page 14: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (2)Stack Frames (2)Stack Frames (2)Stack Frames (2) Contents

• Return information• Arguments• Local variables & temp space

yoo

whoLocal variables & temp space

Management• Space allocated when enter

d

amI

procedure– “set-up” code

• Deallocated when return

FramePointer%ebp

– “finish” code

PointersStack pointe %esp indicates

StackPointer

proc%ebp

• Stack pointer %esp indicates stack top

• Frame pointer %ebp indicates t t f t f

Pointer%esp Stack

“Top”

14CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

start of current frame

Page 15: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (3)Stack Frames (3)Stack Frames (3)Stack Frames (3)•Frame••Pointer

%ebpCall Chainyoo(…)

yoo

StackPointer%

yoo{

••h ()

yoo

%espwho();••

}}

15CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 16: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (4)Stack Frames (4)Stack Frames (4)Stack Frames (4)•••

FrameCall Chain

who(…)yoo

who

FramePointer%ebp

yoo{

•amI();

yoo

StackPointer%

whowho•amI();•

%esp}

16CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 17: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (5)Stack Frames (5)•

Stack Frames (5)Stack Frames (5)

••

Call ChainamI(…)

yoo

whoFrame

yoo{

••I()

yoo

who

amI

FramePointer%ebp

who

amI

amI();••

}Stack

Pointer%esp

amIamI}

%esp

17CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 18: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (6)Stack Frames (6)Stack Frames (6)Stack Frames (6)•••

Call ChainamI(…)

yoo

who

yoo{

••I()

yoo

who

amIFrame

who

amI

amI();••

} amIFramePointer%ebp

amI}

amI amIStack

Pointer%esp

18CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 19: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (7)Stack Frames (7)Stack Frames (7)Stack Frames (7)•••

Call ChainamI(…)

yoo

who

yoo{

••I()

yoo

who

amI

who

amI

amI();••

} amI

Frame

amI}

amI amIPointer%ebpamI

amI

19CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Stack Pointer %esp

Page 20: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (8)Stack Frames (8)Stack Frames (8)Stack Frames (8)•••

Call ChainamI(…)

yoo

who

yoo{

••I()

yoo

who

amI

who

amI

amI();••

} Frame amIamI}

amI amI

FramePointer%ebp

amIStack

Pointer%esp

20CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 21: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (9)Stack Frames (9)Stack Frames (9)Stack Frames (9)•••

Call ChainamI(…)

yoo

who

yoo{

••I()

yoo

Frame who

amI

who

amI

amI();••

}

FramePointer%ebp

amIamI}

amI

StackPointer%esp

amI

%esp

21CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 22: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (10)Stack Frames (10)Stack Frames (10)Stack Frames (10)•••

Call Chainwho(…)

yooFrame

who

yoo{

•amI();

yooFramePointer%ebp

whowho

amI

•amI();•

}

StackPointeramI}

amI

%esp

amI

22CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 23: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (11)Stack Frames (11)Stack Frames (11)Stack Frames (11)•••

Call ChainamI(…)

yoo

who

yoo{

••

yoo

Frame who

amI

who

amI

••

}

FramePointer%ebp

amI amIamI

amI

StackPointer%esp

amI

%esp

23CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 24: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (12)Stack Frames (12)Stack Frames (12)Stack Frames (12)•••

Call Chainwho(…)

yooFrame

who

yoo{

•amI();

yooFramePointer%ebp

whowho

amI

•amI();•

}

StackPointeramIamI}

amI

%esp

amI

24CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 25: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Stack Frames (13)Stack Frames (13)Stack Frames (13)Stack Frames (13)•Frame••Pointer

%ebpCall Chainyoo(…)

yoo

StackPointer%

yoo{

••h ()

yoo

%espwho

amI

who();••

} amIamI}

amI

amI

25CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 26: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32/Linux Stack FrameIA-32/Linux Stack FrameIA 32/Linux Stack FrameIA 32/Linux Stack Frame Current stack frame (“Top”

to Bottom)• Parameters for function about

t llCallerFrameto call

– “Argument build”

• Local variables Frame Pointer Return Addr

ArgumentsFrame

Local variables– If can’t keep in registers

• Saved register context

(%ebp)

Saved

Old %ebp

g• Old frame pointer

Caller stack frame

Registers+

Local

• Return address– Pushed by call instruction

f h ll Stack Pointer

Variables

Argument

26CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

• Arguments for this call Stack Pointer(%esp)

Build

Page 27: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Revisiting swap (1)Revisiting swap (1)e s t g swap ( )e s t g swap ( )

int zip1 = 15213;Calling swap from call_swap

int zip1 = 15213;int zip2 = 91125;

void call swap()

call_swap:• • •pushl $zip2 # Global Varvoid call_swap()

{swap(&zip1, &zip2);

}

pushl $zip2 # Global Varpushl $zip1 # Global Varcall swap• • •

void swap(int *xp, int *yp)

} • • •

Resulting•{int t0 = *xp;int t1 = *yp;

gStack•

*xp = t1;*yp = t0;

}

&zip2

&zip1

Rtn adr %esp

27CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Rtn adr %esp

Page 28: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Revisiting swap (2)Revisiting swap (2)e s t g swap ( )e s t g swap ( )

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

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

Setupint t1 = *yp;*xp = t1;*yp = t0;

}

pushl %ebx

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

Bodymovl %eax,(%edx)movl %ebx,(%ecx)

movl -4(%ebp),%ebx( p),movl %ebp,%esppopl %ebpret

Finish

28CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 29: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Swap Setup (1)Swap Setup (1)Swap Setup ( )Swap Setup ( )ResultingSt k

EnteringSt k StackStack

%ebp %ebp•••

•••

&zip2

&zip1

yp

xpp

Rtn adr %esp

p

Rtn adrOld %ebp %esp

swap:pushl %ebpmovl %esp,%ebp

29CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

pushl %ebx

Page 30: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Swap Setup (2)Swap Setup (2)Swap Setup ( )Swap Setup ( )ResultingSt k

EnteringSt k StackStack

%ebp•••

•••

&zip2

&zip1

yp

xpp

Rtn adr %esp

p

Rtn adrOld %ebp %esp

swap:pushl %ebpmovl %esp,%ebp

%ebp

30CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

pushl %ebx

Page 31: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Swap Setup (3)Swap Setup (3)Swap Setup (3)Swap Setup (3)ResultingSt k

EnteringSt k StackStack

%ebp•••

•••

&zip2

&zip1

yp

xpp

Rtn adr %esp

p

Rtn adrOld %ebp %ebp

swap:pushl %ebpmovl %esp,%ebp

%espOld %ebx

31CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

pushl %ebx

Page 32: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Effect of swap SetupEffect of swap Setupect o swap Setupect o swap SetupResultingSt k

EnteringSt k StackStack

%ebp•••

•••

&zip2

&zip1

yp

xp8

12

p

Rtn adr %esp

p

Rtn adrOld %ebp %ebp0

4

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

%espOld %ebx

Body

32CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

. . .

Page 33: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

swap Finish (1)swap Finish (1)swap s ( )swap s ( )swap’s Stack

••

••

yp

12

Offset

yp

12

Offset

yp

xp

Rtn adr4

8

yp

xp

Rtn adr4

8

Old %ebp %ebp

%espOld %ebx0

-4

Old %ebp %ebp

%espOld %ebx0

-4

movl –4(%ebp),%ebxmovl %ebp,%esppopl %ebp

Observation• Saved & restored register %ebx

33CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

p p pret

Saved & restored register %ebx

Page 34: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

swap Finish (2)swap Finish (2)swap s ( )swap s ( )swap’s Stack

••

••

yp

12

Offset

yp

12

Offset

yp

xp

Rtn adr4

8

yp

xp

Rtn adr4

8

Old %ebp %ebp

%espOld %ebx0

-4

Old %ebp %ebp

%espOld %ebx0

-4

movl –4(%ebp),%ebxmovl %ebp,%esppopl %ebp

34CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

p p pret

Page 35: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

swap Finish (3)swap Finish (3)swap s (3)swap s (3)swap’s Stack

%ebp

••

%ebp

••

yp

12

Offset

yp

12

Offset

yp

xp

4

8

Rtn adr

yp

xp

Rtn adr4

8

Old %ebp%espOld %ebx

0

-4

Old %ebp %ebp

%espOld %ebx0

-4

movl –4(%ebp),%ebxmovl %ebp,%esppopl %ebp

35CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

p p pret

Page 36: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

swap Finish (4)swap Finish (4)swap s ( )swap s ( )swap’s Stack

%ebp% bExiting Stack

••

%ebp

••

%ebp

yp

12

Offset

yp

12

Offset

Rtn adr

yp

xp %esp

4

8

yp

xp

4

8

12

Rtn adrOld %ebp

Old %ebx0

-4

Old %ebp%espOld %ebx

0

-4

movl –4(%ebp),%ebxmovl %ebp,%esppopl %ebp

Observation• Saved & restored register %ebx

36CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

p p pret• Didn’t do so for %eax, %ecx, or %edx

Page 37: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Register Saving Conventions (1)Register Saving Conventions (1)g g ( )g g ( )

When procedure yoo() calls who():p y () ()• yoo is the caller, who is the callee

Can register be used for temporary storage?Can register be used for temporary storage?

yoo:• • •

who:• • •• • •

movl $15213, %edxcall whoaddl %edx, %eax

• • •movl 8(%ebp), %edxaddl $91125, %edx• • •addl %edx, %eax

• • •ret

ret

• Contents of register %edx overwritten by who

37CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 38: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Register Saving Conventions (2)Register Saving Conventions (2)g g ( )g g ( )

Conventions• “Caller save”

– Caller saves temporary in its frame before calling

• “Callee save”– Callee saves temporary in its frame before using

38CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 39: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

IA-32/Linux Register UsageIA-32/Linux Register UsageIA 32/Linux Register UsageIA 32/Linux Register Usage Integer registersg g

• Two have special uses:– %ebp, %esp

%eax

%edxCaller‐Save

• Three managed as callee-save:– %ebx, %esi, %edi

%edx

%ecx

% b

Temporaries

– Old values saved on stack prior to using

• Three managed as caller save:

%ebx

%esiCallee‐SaveTemporaries

• Three managed as caller-save:– %eax, %edx, %ecx– Do what you please, but expect

%edi

%espSpecialy p , p

any callee to do so, as well

• Register %eax also stores d l

%ebpSpecial

39CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

returned value

Page 40: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

Recursive Factorial: rfactRecursive Factorial: rfactecu s e acto a : actecu s e acto a : act Registers rfact:

pushl %ebpg

• %eax used without first saving

pushl %ebpmovl %esp,%ebppushl %ebxmovl 8(%ebp),%ebx

• %ebx used, but save at beginning & d

cmpl $1,%ebxjle .L78leal -1(%ebx),%eaxpushl %eax& restore at end

int rfact(int x)

pushl %eaxcall rfactimull %ebx,%eaxjmp .L79

{int rval;if (x <= 1)

.align 4.L78:

movl $1,%eaxL79:return 1;

rval = rfact(x-1);return rval * x;

.L79:movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebp

40CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

} ret

Page 41: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

rfact Stack Setuprfact Stack Setupact Stac Setupact Stac Setup

Entering Stack%ebppre %ebp pre %ebp

xCaller

%ebppre %ebppre %ebx

x8

Callerpre %ebppre %ebx

Rtn adr %esp Rtn adr4

%ebp0 Callee

Old %ebp%espOld %ebx-4

Callee

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

41CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Page 42: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

rfact Bodyrfact Bodyact odyact ody Registers int rfact(int x)

• %ebx: stored value of x• %eax

{int rval;if (x <= 1)

return 1;– Temporary value of x-1– Returned value from rfact(x-1)

R t d l f thi ll

return 1;rval = rfact(x-1);return rval * x;

}– Returned value from this call

movl 8(%ebp),%ebx # ebx = xcmpl $1,%ebx # Compare x : 1

8 #jle .L78 # If <= goto Termleal -1(%ebx),%eax # eax = x-1pushl %eax # Push x-1call rfact # rfact(x-1)Recursion call rfact # rfact(x 1)imull %ebx,%eax # rval * xjmp .L79 # Goto done

.L78: # Term:l $1 % # l 1

42CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

movl $1,%eax # return val = 1.L79: # Done:

Page 43: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

rfact Recursionrfact Recursionact ecu s oact ecu s oleal -1(%ebx),%eax

x

pushl %eaxx

Rtn adr call rfact

Rtn adrOld %ebp %ebp

Old %ebp %ebp

Old %ebx %espx

Rtn adrOld %ebx

%espx-1

Rtn adrOld %ebp %ebp

Old %ebx

x-1%eax

%eax

x%ebx

x-1x-1

%espRtn adrx 1%eax

x%ebx x-1%eax

x%ebx

43CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

x%ebx

Page 44: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

rfact Resultrfact Resultact esu tact esu t

imull %ebx,%eaxReturn from Call

x

Rtn adr

imull %ebx,%eax

x

Rtn adr

Return from Call

Rtn adrOld %ebp %ebp

Old %ebx

Rtn adrOld %ebp %ebp

Old %ebx

%espx-1%espx-1

(x-1)!x!%eax

x%ebx

(x-1)!%eax

x%ebx

(x-1)!

x%ebxx%ebx

Assume that rfact(x-1)returns (x-1)! in register

44CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

( ) g%eax

Page 45: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

rfact Completionrfact Completionact Co p et oact Co p et o

pre %ebpmovl –4(%ebp),%ebx

l % b %

x8

pre %ebppre %ebx

movl %ebp,%esppopl %ebpret

Rtn adrOld %ebp %ebp0

4

8

pre %ebppre %ebx %ebppre %ebp

Old %ebx%esp

-4

x-1-8

x

Rtn adrOld %ebp %ebp0

4

8

x

Rt d %

pre %ebx

x!%eax

x%ebx

Old %ebp %ebp0 %esp

Old %ebx

Rtn adr %esp

x%ebxx!%eax

Old %ebx%ebx

Old %ebx

x!%eax

45CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

Old %ebx%ebx

Page 46: MidtermExamMidterm Exam - AndroBenchcsl.skku.edu/uploads/CSE2003S10/11-procedure.pdf · 2011-02-14 · Notice Midterm exam results •Check out your score in GLS •Your exam pppapers

SummarySummarySummarySummary The stack makes recursion work

• Private storage for each instance of procedure call– Instantiations don’t clobber each other

Add i f l l t b l ti t t k– Addressing of locals + arguments can be relative to stack positions

• Can be managed by stack disciplineg y p– Procedures return in inverse order of calls

Procedures = Instructions + Conventions• Call / Ret instructions• Register usage conventions

ll ll– Caller / Callee save– %ebp and %esp

• Stack frame organization conventions

46CSE2003: System Programming | Spring 2010| Jin-Soo Kim ([email protected])

• Stack frame organization conventions


Recommended