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.
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.
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.
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.
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: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
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!!
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
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);
Section 5: Procedures & Stacks
Reading Condition Codes (Cont.)
Credits and Disclaimers
C function call conventions and the stack
CSCE 212 Computer Architecture
Instructor: Your TA(s)
Recitation: Attack Lab
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
In this lecture Global variables Local variables System stack
Procedures & The Stack Announcements Lab 1 graded HW 2 out
Switch & Procedures & The Stack I CSE 351 Winter 2017
x86-64 Programming III & The Stack CSE 351 Winter 2018
Machine-Level Programming 4 Procedures
Assembly Programming IV CSE 351 Spring 2017
x86-64 Programming III & The Stack CSE 351 Winter 2018
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Procedures & The Stack I CSE 351 Autumn 2016
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
Recitation: Attack Lab
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
x86-64 Programming III & The Stack CSE 351 Autumn 2017
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)
Get To Know Your Compiler
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Carnegie Mellon Ithaca College
“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.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Carnegie Mellon Ithaca College
Credits and Disclaimers
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 Illustration of Recursion

x86-64 Stack Stack “Bottom” Ithaca College x86-64 Stack Stack Pointer: %rsp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Region of memory managed with stack discipline Grows toward lower addresses Register %rsp contains lowest stack address address of “top” element

x86-64 Stack: Push Stack “Bottom” pushq Src Stack Pointer: %rsp Ithaca College x86-64 Stack: Push Stack “Bottom” pushq Src Fetch operand at Src Decrement %rsp by 8 Write operand at address given by %rsp Increasing Addresses Stack Grows Down Stack Pointer: %rsp Stack “Top” -8

x86-64 Stack: Pop Stack “Bottom” popq Dest Stack Pointer: %rsp Ithaca College x86-64 Stack: Pop Stack “Bottom” popq Dest Read value at address given by %rsp Increment %rsp by 8 Store value at Dest (must be register) Increasing Addresses Stack Grows Down +8 Stack Pointer: %rsp Stack “Top”

pushl and popl instructions Effect Description pushq S R[%rsp] R[%rsp]–8; M[R[%rsp]]S Push on runtime stack popq D DM[R[%rsp]]; R[%rsp]R[%rsp]+8 Pop from runtime stack Notes: 1. both instructions take a single operand 2. Stack is a place in memory allocated to a process 3. Stack grows from smaller addresses to larger addresses 4. %rsp holds the address of the current top of stack 5. When pushq a value, first increment %rsp by 8, then write the value at the new top of stack address. Effect of a pushq %rbp instruction: subq $8, %rsp movq %rbp, (%rsp) Effect of a popq %rax instruction: movq (%rsp), %rax addq $8, %rsp

Initially pushq %rax popq %rdx 0x123 0x108 %rax %rdx %rsp 0x123 0x100 %rax %rdx %rsp 0x123 0x108 %rax %rdx %rsp • Stack “top” Stack “bottom” Stack “bottom” Stack “bottom” • • Increasing address Stack Grows Down 0x108 0x108 0x108 0x123 0x123 0x100 Stack “top” Stack “top” 1. By convention we draw the stack with the top towards the bottom 2. stack grows toward lower addresses

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

Code Examples void multstore (long x, long y, long *dest) { long t = mult2(x, y); *dest = t; } 0000000000400540 <multstore>: 400540: push %rbx # Save %rbx 400541: mov %rdx,%rbx # Save dest 400544: callq 400550 <mult2> # mult2(x,y) 400549: mov %rax,(%rbx) # Save at dest 40054c: pop %rbx # Restore %rbx 40054d: retq # Return long mult2 (long a, long b) { long s = a * b; return s; } 0000000000400550 <mult2>: 400550: mov %rdi,%rax # a 400553: imul %rsi,%rax # a * b 400557: retq # Return

Procedure Control Flow Ithaca College 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 the next instruction right 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 address from stack Jump to address

Control Flow Example #1 • 0x130 0000000000400540 <multstore>: • 400544: callq 400550 <mult2> 400549: mov %rax,(%rbx) 0x128 0x120 %rsp 0x120 %rip 0x400544 0000000000400550 <mult2>: 400550: mov %rdi,%rax • 400557: retq

Control Flow Example #2 • %rsp 0x120 0x128 0x130 0x118 %rip 0000000000400540 <multstore>: • 400544: callq 400550 <mult2> 400549: mov %rax,(%rbx) 0x400549 0x118 0x400550 0000000000400550 <mult2>: 400550: mov %rdi,%rax • 400557: retq

Control Flow Example #3 • %rsp 0x120 0x128 0x130 0x118 %rip 0000000000400540 <multstore>: • 400544: callq 400550 <mult2> 400549: mov %rax,(%rbx) 0x400549 0x118 0x400557 0000000000400550 <mult2>: 400550: mov %rdi,%rax • 400557: retq

Control Flow Example #4 • 0x130 0000000000400540 <multstore>: • 400544: callq 400550 <mult2> 400549: mov %rax,(%rbx) 0x128 0x120 %rsp 0x120 %rip 0x400549 0000000000400550 <mult2>: 400550: mov %rdi,%rax • 400557: retq