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.

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.
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
X86 Programming CS 740 Sept. 12, 2007 Topics Basics Accessing and Moving Data Arithmetic operations Control Flow Procedures Data Structures.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
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
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.
Introduction to x86 Assembly or “How does someone hack my laptop?”
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.
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: 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 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
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
Reading Condition Codes (Cont.)
C function call conventions and the stack
CSCE 212 Computer Architecture
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
Machine-Level Programming 1 Introduction
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 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine Level Representation of Programs (IV)
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)
“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:

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 Switch statements  Jump tables: jmp *.L62(,%edx,4)  Decision trees (not shown) when we have sparse cases Jump table.section.rodata.align 4.L62:.long.L61 # x = 0.long.L56 # x = 1.long.L57 # x = 2.long.L58 # x = 3.long.L61 # x = 4.long.L60 # x = 5.long.L60 # x = 6 1

University of Washington Procedures/Stacks Stacks Procedures Parameter passing 2

University of Washington Memory Layout Instructions Literals Static Data Dynamic Data (Heap) Stack Execution lifetime; immutable Execution lifetime; mutable Programmer controlled lifetime; mutable “Automatic” lifetime; mutable Read-only; executable Read-only; not executable writable; not executable Note: executability of data areas is system dependent...

University of Washington Memory Layout Instructions Literals Static Data Dynamic Data (Heap) Stack literals (e.g., “example”) static variables (including global variables (C)) new'ed variables local variables 0 2 N -1

University of Washington IA32 Stack Region of memory managed with a stack discipline Grows toward lower addresses Customarily shown “upside-down” Register %esp contains lowest stack address = address of “top” element Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” 5

University of Washington IA32 Stack: Push pushl Src Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer: %esp -4 6

University of Washington IA32 Stack: Push 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 7

University of Washington IA32 Stack: Pop Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” popl Dest +4 8

University of Washington IA32 Stack: Pop Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” popl Dest  Read operand at address %esp  Increment %esp by 4  Write operand to Dest +4 9

University of Washington Procedure Control Flow Use stack to support procedure call and return Procedure call: call label  Push return address on stack  Jump to label 10

University of Washington 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 instruction beyond call  Example from disassembly e:e8 3d call 8048b :50 pushl %eax  Return address = 0x Procedure return: ret  Pop address from stack  Jump to address 11

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

University of Washington %esp %eip %esp %eip 0x x108 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: program counter x000063d

University of Washington %esp %eip 0x104 %esp %eip 0x x104 0x108 0x10c 0x110 0x Procedure Return Example 0x108 0x10c 0x ret :c3 ret 0x108 0x %eip: program counter 14

University of Washington Stack-Based Languages Languages that support recursion  e.g., C, Pascal, Java  Code must be re-entrant  Multiple simultaneous instantiations of single procedure  Need some place to store state of each instantiation  Arguments  Local variables  Return pointer Stack discipline  State for a given procedure needed for a limited time  Starting from when it is called to when it returns  Callee always returns before caller does Stack allocated in frames  State for a single procedure instantiation 15

University of Washington Call Chain Example yoo(…) { who(); } who(…) { amI(); amI(); } amI(…) { amI(); } yoo who amI Example Call Chain amI Procedure amI is recursive (calls itself) 16

University of Washington Frame for proc Frame Pointer: %ebp Stack Frames Contents  Local variables  Return information  Temporary space Management  Space allocated when procedure is entered  “Set-up” code  Space deallocated upon return  “Finish” code Stack Pointer: %esp Previous Frame Stack “Top” 17

University of Washington Example yoo(…) { who(); } yoo who amI yoo %ebp %esp Stack 18

University of Washington who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who 19

University of Washington amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI 20

University of Washington amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI 21

University of Washington amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI 22

University of Washington amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI 23

University of Washington amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI 24

University of Washington who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who 25

University of Washington amI(…) { } Example yoo who amI yoo %ebp %esp Stack who amI 26

University of Washington who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who 27

University of Washington Example yoo(…) { who(); } yoo who amI yoo %ebp %esp Stack 28

University of Washington IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom)  Old frame pointer  Local variables If can’t be just kept in registers  Saved register context When reusing registers  “Argument build area” Parameters for function about to be called Caller Stack Frame  Return address Pushed by call instruction  Arguments for this call Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame Frame pointer %ebp Stack pointer %esp 29

University of Washington Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 98195; void call_swap() { swap(&zip1, &zip2); } call_swap: pushl $zip2# Global Var pushl $zip1# Global Var call swap &zip2 &zip1 Rtn adr %esp Resulting Stack Calling swap from call_swap 30

University of Washington Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish 31

University of Washington swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp 32

University of Washington swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp 33

University of Washington swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack 34

University of Washington swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp 35

University of Washington swap Setup #1 &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack Old %ebx movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp... Offset relative to new %ebp 36

University of Washington swap Finish #1 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack Old %ebx Observation: Saved and restored register %ebx 37

University of Washington swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Old %ebx 38

University of Washington swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack 39

University of Washington swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp 40

University of Washington swap Finish #3 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Resulting Stack yp xp Rtn adr %ebp %esp 41

University of Washington swap Finish #4 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr %ebp %esp 42

University of Washington swap Finish #4 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp %ebp %esp Resulting Stack Observation  Saved & restored register %ebx  Didn’t do so for %eax, %ecx, or %edx 43

University of Washington Disassembled swap a4 : 80483a4: 55 push %ebp 80483a5: 89 e5 mov %esp,%ebp 80483a7: 53 push %ebx 80483a8: 8b mov 0x8(%ebp),%edx 80483ab: 8b 4d 0c mov 0xc(%ebp),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: mov %eax,(%edx) 80483b4: mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret : e8 96 ff ff ff call 80483a e: 8b 45 f8 mov 0xfffffff8(%ebp),%eax Calling Code 44 mov %ebp,%esp pop %ebp 0x e + 0xffffff96 = 0x080483a4

University of Washington 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 $98195, %edx ret 45

University of Washington 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 Save”  Caller saves temporary in its frame before calling  “Callee Save”  Callee saves temporary in its frame before using 46

University of Washington IA32/Linux Register Usage %eax, %edx, %ecx  Caller saves prior to call if values are used later %eax  also used to return integer value %ebx, %esi, %edi  Callee saves if wants to use them %esp, %ebp  special %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special 47

University of Washington int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; } 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  %ebx used, but saved at beginning & restored at end  %eax used without first saving  expect caller to save  pushed onto stack as parameter for next call  used for return value 48

University of Washington Pointer Code void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1,accum); } int sfact(int x) { int val = 1; s_helper(x, &val); return val; } Top-Level CallRecursive Procedure Pass pointer to update location 49

University of Washington Temp. Space %esp Creating & Initializing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 Variable val must be stored on stack  Because: Need to create pointer to it Compute pointer as -4(%ebp) Push on stack as second argument Initial part of sfact x Rtn adr Old %ebp %ebp val = 1 Unused _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 50

University of Washington Temp. Space %esp Creating & Initializing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 Variable val must be stored on stack  Because: Need to create pointer to it Compute pointer as -4(%ebp) Push on stack as second argument Initial part of sfact x Rtn adr Old %ebp %ebp val = 1 Unused _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 51

University of Washington Passing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish Calling s_helper from sfact x Rtn adr Old %ebp %ebp val = 1 -4 Unused %esp x &val Stack at time of call leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish val=x! 52

University of Washington Passing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish Calling s_helper from sfact x Rtn adr Old %ebp %ebp val = 1 -4 Unused %esp x &val Stack at time of call leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish val=x! 53

University of Washington IA 32 Procedure Summary 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  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 Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame %ebp %esp 54