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.

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/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
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.
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.
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
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
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.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
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.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
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.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
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.
1 Assembly Language: Function Calls Jennifer Rexford.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
A job ad at a game programming company
Reading Condition Codes (Cont.)
Credits and Disclaimers
C function call conventions and the stack
CSCE 212 Computer Architecture
Recitation 2 – 2/11/02 Outline Stacks & Procedures
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.
Procedures & The Stack Announcements Lab 1 graded HW 2 out
Machine-Level Programming 1 Introduction
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.
Procedures & The Stack II 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
Condition Codes Single Bit Registers
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)
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.
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.
Credits and Disclaimers
Presentation transcript:

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

University of Washington IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom)  “Argument build” area (parameters for function about to be called)  Local variables (if can’t be kept in registers)  Saved register context (when reusing registers)  Old frame pointer (for caller) Caller’s 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 2

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); } 3

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 Calling swap from call_swap 4

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 5

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 6

University of Washington swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack? &zip2 &zip1 Rtn adr %esp Entering Stack %ebp 7

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 8

University of Washington swap Setup #2 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 9

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

University of Washington swap Body &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 11

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 Resulting Stack? 12

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 13

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 14

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 15

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 16

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 17 mov %ebp,%esp pop %ebp

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 18

University of Washington Register Saving Conventions When procedure yoo calls who :  yoo is the caller  who is the callee Can a register be used for temporary storage?  Contents of register %edx overwritten by who yoo: movl $12345, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $98195, %edx ret 19

University of Washington Register Saving Conventions When procedure yoo calls who :  yoo is the caller  who is the callee Can a register be used for temporary storage? Conventions  “Caller Save”  Caller saves temporary values in its frame before calling  “Callee Save”  Callee saves temporary values in its frame before using 20

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 form of callee save – restored to original values upon exit from procedure %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special 21

University of Washington Example: Pointers to Local Variables 22 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

University of Washington 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 Initial part of sfact x Rtn adr _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 movl %esp,%ebp subl $16,%esp movl 8(%ebp),%edx movl $1,-4(%ebp) 23 %esp

University of Washington 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 Initial part of sfact x Rtn adr _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 24 %esp

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 How do we call s_helper? Initial part of sfact x Rtn adr Old %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 25 %ebp

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! 26 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

University of Washington IA 32 Procedure Summary Important points:  IA32 procedures are a combination of instructions and conventions  Conventions prevent functions from disrupting each other  Stack is the right data structure for procedure call / return  If P calls Q, then Q returns before P Recursion handled by normal calling conventions  Can safely store values in local stack frame and in callee-saved registers  Put function arguments at top of stack  Result returned in %eax Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame %ebp %esp 27

University of Washington x86-64 Procedure Calling Convention Doubling of registers makes us less dependent on stack  Store argument in registers  Store temporary variables in registers What do we do if we have too many arguments or too many temporary variables? 28

University of Washington %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp x bit Registers: Usage Conventions 29 %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 Callee saved Caller saved Callee saved Stack pointer Caller Saved Return value Argument #4 Argument #1 Argument #3 Argument #2 Argument #6 Argument #5

University of Washington Revisiting swap, IA32 vs. x86-64 versions 30 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 swap (64-bit long ints): movq(%rdi), %rdx movq(%rsi), %rax movq%rax, (%rdi) movq%rdx, (%rsi) ret Arguments passed in registers  First ( xp ) in %rdi, second ( yp ) in %rsi  64-bit pointers No stack operations required (except ret ) Avoiding stack  Can hold all local information in registers

University of Washington X86-64 procedure call highlights Arguments (up to first 6) in registers  Faster to get these values from registers than from stack in memory Local variables also in registers (if there is room) callq instruction stores 64-bit return address on stack  Address pushed onto stack, decrementing %rsp by 8 No frame pointer  All references to stack frame made relative to %rsp; eliminates need to update %ebp/%rbp, which is now available for general-purpose use Functions can access memory up to 128 bytes beyond %rsp: the “red zone”  Can store some temps on stack without altering %rsp Registers still designated “caller-saved” or “callee-saved” 31

University of Washington x86-64 Stack Frames Often (ideally), x86-64 functions need no stack frame at all  Just a return address is pushed onto the stack when a function call is made A function does need a stack frame when it:  Has too many local variables to hold in registers  Has local variables that are arrays or structs  Uses the address-of operator (&) to compute the address of a local variable  Calls another function that takes more than six arguments  Needs to save the state of callee-save registers before modifying them 32

University of Washington Example 33 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: subq $32,%rsp movq $1,16(%rsp) movl $2,24(%rsp) movw $3,28(%rsp) movb $4,31(%rsp) Return address to caller of call_proc %rsp NB: Details may vary depending on compiler.

University of Washington Example 34 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: subq $32,%rsp movq $1,16(%rsp) movl $2,24(%rsp) movw $3,28(%rsp) movb $4,31(%rsp) Return address to caller of call_proc %rsp x3x4x2 x1

University of Washington Example 35 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: leaq 24(%rsp),%rcx leaq 16(%rsp),%rsi leaq 31(%rsp),%rax movq %rax,8(%rsp) movl $4,(%rsp) leaq 28(%rsp),%r9 movl $3,%r8d movl $2,%edx movq $1,%rdi call proc Arg 8 Arg 7 %rsp x3x4x2 x1 Return address to caller of call_proc Arguments passed in (in order): rdi, rsi, rdx, rcx, r8, r9

University of Washington Example 36 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: leaq 24(%rsp),%rcx leaq 16(%rsp),%rsi leaq 31(%rsp),%rax movq %rax,8(%rsp) movl $4,(%rsp) leaq 28(%rsp),%r9 movl $3,%r8d movl $2,%edx movq $1,%rdi call proc Arg 8 Arg 7 %rsp x3x4x2 x1 Return address to caller of call_proc Return address to line after call to proc

University of Washington Example 37 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: movswl 28(%rsp),%eax movsbl 31(%rsp),%edx subl %edx,%eax cltq movslq 24(%rsp),%rdx addq 16(%rsp),%rdx imulq %rdx,%rax addq $32,%rsp ret Arg 8 Arg 7 x3x4x2 x1 Return address to caller of call_proc %rsp

University of Washington Example 38 long int call_proc() { long x1 = 1; int x2 = 2; short x3 = 3; char x4 = 4; proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4); return (x1+x2)*(x3-x4); } call_proc: movswl 28(%rsp),%eax movsbl 31(%rsp),%edx subl %edx,%eax cltq movslq 24(%rsp),%rdx addq 16(%rsp),%rdx imulq %rdx,%rax addq $32,%rsp ret Return address to caller of call_proc %rsp

University of Washington x86-64 Procedure Summary Heavy use of registers (faster than using stack in memory)  Parameter passing  More temporaries since more registers Minimal use of stack  Sometimes none  When needed, allocate/deallocate entire frame at once  No more frame pointer: address relative to stack pointer More room for compiler optimizations  Prefer to store data in registers rather than memory  Minimize modifications to stack pointer 39