1 Carnegie Mellon Stacks 15-213: Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.

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.
15/18-213: Introduction to Computer Systems
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
RECITATION - 09/20/2010 BY SSESHADR Buflab. Agenda Reminders  Bomblab should be finished up  Exam 1 is on Tuesday 09/28/2010 Stack Discipline Buflab.
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.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
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
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.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
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.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
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.
1 Assembly Language: Function Calls Jennifer Rexford.
Carnegie Mellon Midterm Review : Introduction to Computer Systems October 15, 2012 Instructor:
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.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
A job ad at a game programming company
CS 177 Computer Security Lecture 9
Assembly function call convention
Reading Condition Codes (Cont.)
C function call conventions and the stack
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
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
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:
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The Runtime Environment
Machine-Level Programming: Introduction
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Chapter 3 Machine-Level Representation of Programs
X86 Assembly Review.
“Way easier than when we were students”
Presentation transcript:

1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F

2 Carnegie Mellon Today: Stacks News Stack discipline review  Quick review of registers and assembly  Stack frames  Function calls  x86 (IA32) and x86-64 Example

3 Carnegie Mellon News bomblab is due tomorrow night  Don’t use your late days yet  “If you wait till the last minute, it only takes a minute!” buflab is coming out tomorrow night  All about stacks Pro-tip: we love stack questions on exams

4 Carnegie Mellon Quick review of registers (IA32) Caller saved: %eax, %ecx, %edx  You must save these before a function call if you need them Callee saved: %ebx, %edi, %esi  You must save these before any work if you need them Base pointer: %ebp  Points to the “bottom” of a stack frame Stack pointer: %esp  Points to the “top” of a stack frame Instruction pointer: %eip  Generally don’t need to worry about this one

5 Carnegie Mellon IA32 stack This is a memory region that grows down Confusingly, refer to the bottom of the stack as the “top” %esp refers to the lowest stack address 0xffffffff %esp Stack “bottom” Stack “top” Stack growth

6 Carnegie Mellon push ing and pop ping It may be helpful to remember this correspondence (IA32)  Note: This is probably not how it actually works %esp “points” to the top value on the stack pushl src popl dest subl $4,%esp movl src,(%esp) movl (%esp),dest addl $4,%esp

7 Carnegie Mellon Quick example... 0x110 0x10c 0x1080x x110 0x10c 0x1080x123 0x1040x x110 0x10c 0x1080x123 0x1040x213 pushl %eaxpopl %edx %eax0x213 %edx0x555 %esp0x108 %eax0x213 %edx0x555 %esp0x104 %eax0x213 %edx0x213 %esp0x108 %esp

8 Carnegie Mellon Stack frames Every function call is given a stack frame What does a C function need?  Local variables (scalars, arrays, structs)  Scalars: if the compiler couldn’t allocate enough registers  Space to save callee saved registers  Space to put computations  A way to give arguments and call other functions  A way to grab arguments Use the stack! %ebp Old %ebp Saved registers + local variables Argument build for function call %esp

9 Carnegie Mellon Function calls Use the stack for function calls Function call  call label Push “return address” on stack, jump to label Return address  Address of the instruction immediately after the call  Example from disassembly:  e: e8 3d call 8048b90  : 50 pushl %eax  Return address is 0x Returning from a function call  ret Pop return address [(%esp)] into %eip, keep running  Remember that the function’s actual return value must be in %eax

10 Carnegie Mellon What does this look like?... 0x110 0x10c 0x1080x x110 0x10c 0x1080x123 0x1040x %eip0x804854e %esp0x108 %esp  e: e8 3d call 8048b90  : 50 pushl %eax call %eip0x8048b90 %esp0x104

11 Carnegie Mellon Returning... 0x110 0x10c 0x1080x123 0x1040x %eip0x %esp0x104  : c3 ret %eip0x %esp0x x110 0x10c 0x1080x123 0x1040x ret %esp

12 Carnegie Mellon Function calls and stack frames %ebp Old %ebp Saved registers + local variables Argument build %esp Suppose you have sum grabs arguments by reaching up the caller’s stack frame! If we scale up this example, we see that arguments should be pushed in reverse order int main(void) { int x = 3; return sum(x, 0); } Arguments Return addr main sum

13 Carnegie Mellon Example (from a 213 exam I took) Given the following function prototypes, and initial lines of IA32 assembly for each function, fill in the stack frame diagram with registers to be saved any arguments to the function foo the return address Any registers stored on the stack by the asm fragment The location on the stack pointed to by %esp and %ebp after execution of the sub instruction (Bonus: How does foo access its arguments after the sub ?) int foo(int a, int b, int c, int d); push %ebp mov %esp,%ebp push %ebx sub $0x10,$esp

14 Carnegie Mellon IA32 vs x86-64 Remember in 64-bit this stuff is even easier  No more frame pointer (you are free to use %ebp/%rbp)  Many arguments are passed in registers  More registers = less stack space needed Overall a lot less stack usage  Good for performance - see memory hierarchy You are expected to know how the stack works for 64-bit  Even if no labs exercise these skills

15 Carnegie Mellon Questions? (stacks, bomblab, what is buflab) (come to office hours if you need help)