Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Calling sequence ESP.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
IA32 Stack Discipline From Last Time
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
A job ad at a game programming company
Assembly function call convention
Reading Condition Codes (Cont.)
C function call conventions and the stack
CSCE 212 Computer Architecture
The Stack & Procedures CSE 351 Spring 2017
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Chapter 3 Machine-Level Representation of Programs
asum.ys A Y86 Programming Example
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures Sept 18, 2001
Today Control flow if/while/do while/for/switch
Machine-Level Programming: Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
“Way easier than when we were students”
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Presentation transcript:

Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables

IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”

IA32 Stack Pushing Pushing – pushl Src – Fetch operand at Src – Decrement %esp by 4 – Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer %esp -4

IA32 Stack Popping Popping – popl Dest – Read operand at address given by %esp – Increment %esp by 4 – Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +4

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 value – Address of instruction beyond call Procedure return: – ret Pop address from stack; Jump to address

%esp %eip %esp %eip 0x108 0x10c 0x110 0x104 0x804854e 0x Procedure Call Example 0x108 0x10c 0x x108 call 8048b e:e8 3d call 8048b :50 pushl %eax 0x8048b90 0x104 %eip is program counter

%esp %eip 0x104 %esp %eip 0x x104 0x108 0x10c 0x110 0x Procedure Return Example 0x108 0x10c 0x ret :c3 ret 0x108 %eip is program counter 0x

Stack-Based Languages Languages that Support Recursion – e.g., C, Pascal, Java – Code must be “Reentrant” Multiple simultaneous instantiations of single procedure – Need some place to store state of each instantiation Arguments, Local variables, Return pointer Stack Discipline – 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

Call Chain Example Code Structure yoo(…) { who(); } who(…) { amI(); amI(); } amI(…) { amI(); } yoo who amI Call Chain Procedure amI recursive amI

%esp yoo who proc %ebp Stack “Top” Stack Frames Contents – Local variables – Return information – Temporary space Management – Space allocated when enter procedure “Set-up” code – Deallocated when return “Finish” code Pointers – Stack pointer %esp indicates stack top – Frame pointer %ebp indicates start of current frame amI

%esp yoo Stack Operation yoo Call Chain yoo(…) { who(); } %ebp

%esp yoo who %ebp Stack Operation yoo who Call Chain who(…) { amI(); amI(); }

%esp yoo who amI %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); }

yoo who amI %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI %esp

yoo who amI Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI %ebp %esp

yoo who amI Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI %ebp %esp

yoo who amI Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI %ebp %esp

yoo who Stack Operation yoo who Call Chain who(…) { amI(); amI(); } amI %ebp %esp

yoo who amI Stack Operation yoo who Call Chain amI(…) { } amI %ebp %esp

yoo who Stack Operation yoo who Call Chain who(…) { amI(); amI(); } amI %ebp %esp

yoo(…) { who(); } yoo Stack Operation yoo who Call Chain amI %ebp %esp

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) – Parameters for function about to call “Argument build” – Local variables If can’t keep in registers – Saved register context – Old frame pointer Caller Stack Frame – Return address Pushed by call instruction – Arguments for this call Stack Pointer ( %esp ) Frame Pointer ( %ebp ) Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame

Register Saving Conventions When procedure yoo calls who : – yoo is the caller, who is the callee Can Register be Used for Temporary Storage? – Contents of register %edx overwritten by who yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $91125, %edx ret

IA32/Linux Register Usage Integer Registers – Two have special uses %ebp, %esp – Three managed as callee- save %ebx, %esi, %edi Old values saved on stack prior to using – Three managed as caller- save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well – Register %eax also stores returned value %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special

int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; }.globl rfact.type rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx jle.L78 leal -1(%ebx),%eax pushl %eax call rfact imull %ebx,%eax jmp.L79.align 4.L78: movl $1,%eax.L79: movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Recursive Factorial Registers – %eax used without first saving – %ebx used, but save at beginning & restore at end

rfact: pushl %ebp movl %esp,%ebp pushl %ebx rfact: pushl %ebp movl %esp,%ebp pushl %ebx Rfact Stack Setup Entering Stack x Rtn adr 4 8 Caller %ebp 0 %esp Old %ebx -4 Callee x Rtn adr Caller %esp %ebp pre %ebp pre %ebx pre %ebp pre %ebx Old %ebp rfact: pushl %ebp movl %esp,%ebp pushl %ebx

Rfact Body Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call movl 8(%ebp),%ebx# ebx = x cmpl $1,%ebx# Compare x : 1 jle.L78# If <= goto Term leal -1(%ebx),%eax# eax = x-1 pushl %eax# Push x-1 call rfact# rfact(x-1) imull %ebx,%eax# rval * x jmp.L79# Goto done.L78:# Term: movl $1,%eax# return val = 1.L79:# Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Recursion

Rfact Recursion x Rtn adr Old %ebp %ebp Old %ebx pushl %eax %esp x-1 %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp %eax x %ebx x-1 leal -1(%ebx),%eax x Rtn adr Old %ebp %ebp Old %ebx x-1 %eax x %ebx %esp Rtn adr call rfact

(x-1)! Rfact Result x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 imull %ebx,%eax x! %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 (x-1)! %eax x %ebx Return from Call (x-1)! Assume that rfact(x-1) returns (x-1)! in register %eax

Rfact Completion x Rtn adr Old %ebp %ebp Old %ebx %esp -4 x! %eax x %ebx x-1 -8 pre %ebp pre %ebx x Rtn adr Old %ebp %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret x Rtn adr %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx

Summary The Stack Makes Recursion Work – Private storage for each instance of procedure call Instantiations don’t clobber each other Addressing of locals + arguments can be relative to stack positions – Can be managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combination of Instructions + Conventions – Call / Ret instructions – Register usage conventions Caller / Callee save %ebp and %esp – Stack frame organization conventions