Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.

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

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Copyright 2014 – Noah Mendelsohn UM Macro Assembler Functions Noah Mendelsohn Tufts University Web:
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
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.
Machine-Level Programming III: Procedures Jan 30, 2003
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
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.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
X86_64 programming Tutorial #1 CPSC 261. X86_64 An extension of the IA32 (often called x86 – originated in the Intel 8086 processor) instruction set to.
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.
Spring 2016Assembly Review Roadmap 1 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);
Reading Condition Codes (Cont.)
Credits and Disclaimers
Computer Science 210 Computer Organization
CSCE 212 Computer Architecture
Instructor: Your TA(s)
Recitation: Attack Lab
143A: Principles of Operating Systems Lecture 4: Calling conventions
Procedures & Executables CSE 351 Spring 2017
The Stack & Procedures CSE 351 Spring 2017
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Recitation: Attack Lab
Procedures & The Stack Announcements Lab 1 graded HW 2 out
Switch & Procedures & The Stack I CSE 351 Winter 2017
Machine-Level Programming III: Switch Statements and IA32 Procedures
Machine-Level Programming 4 Procedures
Assembly Programming IV CSE 351 Spring 2017
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Procedures & The Stack II CSE 351 Autumn 2016
Procedures & The Stack I CSE 351 Autumn 2016
Arrays CSE 351 Winter
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Procedures & The Stack II CSE 351 Winter 2017
Assembly Programming IV CSE 410 Winter 2017
Procedures & Executables CSE 351 Autumn 2017
Recitation: Attack Lab
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures Sept 18, 2001
The Stack & Procedures CSE 351 Winter 2018
Machine Level Representation of Programs (IV)
The Stack & Procedures CSE 410 Winter 2017
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.
Oct 15, 2018 Instructor: Your TA(s) 1.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Where is all the knowledge we lost with information? T. S. Eliot
“Way easier than when we were students”
Loops, Switches, Intro to Stack & Procedures CSE 351 Winter 2019
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Credits and Disclaimers
Credits and Disclaimers
Visual Studio x64 C Compiler function entrance code
Computer Architecture and System Programming Laboratory
Instructor: Your TA(s)
Presentation transcript:

Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017

Mechanisms in Procedures • y = Q(x); print(y) } Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value Memory management Allocate during procedure execution Deallocate upon return Mechanisms all implemented with machine instructions x86-64 implementation of a procedure uses only those mechanisms required int Q(int i) { int t = 3*i; int v[10]; • return v[t]; }

Today Procedures Stack Structure Calling Conventions Passing control Ithaca College Today Procedures Stack Structure Calling Conventions Passing control Passing data Managing local data Illustrations of Recursion & Pointers

Procedure Data Flow Registers Stack First 6 arguments Return value Ithaca College Procedure Data Flow Registers Stack First 6 arguments Return value Arg 7 • • • Arg 8 Arg n %rdi %rsi %rdx %rcx %r8 %r9 %rax Only allocate stack space when needed

Data Flow Examples void multstore (long x, long y, long *dest) { long t = mult2(x, y); *dest = t; } Data Flow Examples 0000000000400540 <multstore>: # x in %rdi, y in %rsi, dest in %rdx • • • 400541: mov %rdx,%rbx # Save dest 400544: callq 400550 <mult2> # mult2(x,y) # t in %rax 400549: mov %rax,(%rbx) # Save at dest long mult2 (long a, long b) { long s = a * b; return s; } 0000000000400550 <mult2>: # a in %rdi, b in %rsi 400550: mov %rdi,%rax # a 400553: imul %rsi,%rax # a * b # s in %rax 400557: retq # Return

Today Procedures Stack Structure Calling Conventions Passing control Ithaca College Today Procedures Stack Structure Calling Conventions Passing control Passing data Managing local data Illustration of Recursion

Stack-Based Languages Ithaca College Stack-Based Languages Languages that support recursion e.g., C, 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 Example Call Chain yoo(…) { • who(); } yoo who(…) { Ithaca College Call Chain Example Example Call Chain yoo(…) { • who(); } yoo who(…) { • • • amI(); } who amI(…) { • amI(); } amI amI amI amI Procedure amI() is recursive

Stack Frames Contents Management Stack “Top” Previous Frame Ithaca College Stack Frames Previous Frame Frame for proc Contents Return information Local storage (if needed) Temporary space (if needed) Management Space allocated when enter procedure “Set-up” code Includes push by call instruction Deallocated when return “Finish” code Includes pop by ret instruction Frame Pointer: %rbp (Optional) x Stack Pointer: %rsp Stack “Top”

Example Stack yoo yoo yoo(…) %rbp { • yoo who(); who %rsp } amI amI Ithaca College Example Stack yoo yoo yoo(…) { • who(); } %rbp %rsp yoo who amI amI amI amI

Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } Ithaca College Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo who %rbp %rsp amI amI amI amI

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI amI %rbp %rsp amI amI

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI(…) { • amI(); } amI amI amI %rbp %rsp amI

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI(…) { • amI(); } amI amI amI(…) { • amI(); } amI amI %rbp %rsp

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI(…) { • amI(); } amI amI amI %rbp %rsp amI

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI amI %rbp %rsp amI amI

Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } Ithaca College Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo who %rbp %rsp amI amI amI amI

Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • Ithaca College Example Stack yoo who amI yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo amI(…) { • amI(); } who amI amI %rbp %rsp amI amI

Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } Ithaca College Example Stack yoo who yoo(…) { • who(); } yoo who(…) { • • • amI(); } yoo who %rbp %rsp amI amI amI amI

Example Stack yoo yoo %rbp yoo(…) yoo { • who %rsp who(); } amI amI Ithaca College Example Stack yoo yoo %rbp %rsp yoo(…) { • who(); } yoo who amI amI amI amI

x86-64/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Ithaca College x86-64/Linux Stack Frame Current Stack Frame (“Top” to Bottom) “Argument build:” Parameters for function about to call Local variables If can’t keep in registers Saved register context Old frame pointer (optional) Caller Stack Frame Return address Pushed by call instruction Arguments for this call Caller Frame Arguments 7+ Frame pointer %rbp Return Addr Old %rbp (Optional) Saved Registers + Local Variables Argument Build (Optional) Stack pointer %rsp

Example: incr long incr(long *p, long val) { long x = *p; Ithaca College Example: incr long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x; } incr: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret Register Use(s) %rdi Argument p %rsi Argument val, y %rax x, Return value

Example: Calling incr #1 Ithaca College Example: Calling incr #1 Initial Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } . . . Rtn address %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movq $3000, %rsi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Resulting Stack Structure . . . The subq instruction is creating room on the stack for local variables v1 and v2 Rtn address 15213 %rsp+8 Unused %rsp

Example: Calling incr #2 Ithaca College Example: Calling incr #2 Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } . . . Rtn address 15213 %rsp+8 Unused %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %rsi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rdi &v1 %rsi 3000 Put the two parameters into %rdi and %rsi. Why use the leaq instruction?

Example: Calling incr #3 Ithaca College Example: Calling incr #3 Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } . . . Rtn address 18213 %rsp+8 Unused %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %rsi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rdi &v1 %rsi 3000

Example: Calling incr #4 Ithaca College Example: Calling incr #4 Stack Structure . . . long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } Rtn address 18213 %rsp+8 Unused %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %rsi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rax Return value Updated Stack Structure Note that %rsp is pointing at the return address for the ret instruction to use . . . Rtn address %rsp

Example: Calling incr #5 Ithaca College Example: Calling incr #5 Updated Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } . . . Rtn address %rsp call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %rsi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret Register Use(s) %rax Return value Final Stack Structure . . . %rsp

Register Saving Conventions Ithaca College 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 %rdx overwritten by who This could be trouble ➙ something should be done! Need some coordination yoo: • • • movq $15213, %rdx call who addq %rdx, %rax ret who: • • • subq $18213, %rdx ret

Register Saving Conventions Ithaca College Register Saving Conventions When procedure yoo calls who: yoo is the caller who is the callee Can register be used for temporary storage? Conventions “Caller Saved” Caller saves temporary values in its frame before the call “Callee Saved” Callee saves temporary values in its frame before using Callee restores them before returning to caller

x86-64 Linux Register Usage #1 Ithaca College x86-64 Linux Register Usage #1 %rax Return value Also caller-saved Can be modified by subroutine %rdi, ..., %r9 Arguments %r10, %r11 Caller-saved Return value %rax %rdi %rsi %rdx Arguments %rcx %r8 %r9 %r10 Caller-saved temporaries %r11

x86-64 Linux Register Usage #2 Ithaca College x86-64 Linux Register Usage #2 %rbx, %r12, %r13, %r14 Callee-saved Callee must save & restore %rbp May be used as frame pointer Can mix & match %rsp Special form of callee save Restored to original value upon exit from subroutine %rbx %r12 Callee-saved Temporaries %r13 %r14 %rbp Special %rsp

Callee-Saved Example #1 Ithaca College Callee-Saved Example #1 Initial Stack Structure long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2; } . . . Rtn address %rsp call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Resulting Stack Structure . . . Rtn address Saved %rbx 15213 %rsp+8 Unused %rsp

Callee-Saved Example #2 Ithaca College Callee-Saved Example #2 Resulting Stack Structure long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2; } . . . Rtn address Saved %rbx 15213 %rsp+8 call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret Unused %rsp Pre-return Stack Structure . . . Rtn address %rsp