CS429: Computer Organization and Architecture

Similar documents
Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Machine-level Programs Procedure

Machine-Level Programming III: Procedures

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Machine- Level Representa2on: Procedure

Machine-Level Programming (2)

Procedures and the Call Stack

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The Stack & Procedures

The Stack & Procedures

Machine Program: Procedure. Zhaoguo Wang

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly Programming IV

University of Washington

Sungkyunkwan University

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Building an Executable

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Assembly Language: Function Calls

CS213. Machine-Level Programming III: Procedures

Systems I. Machine-Level Programming V: Procedures

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Assembly Programming IV

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

x86-64 Programming III

Foundations of Computer Systems

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Machine Programming 3: Procedures

CS429: Computer Organization and Architecture

Giving credit where credit is due

Machine Language CS 3330 Samira Khan

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Lecture 4: x86_64 Assembly Language

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

X86 Assembly -Procedure II:1

Machine/Assembler Language Putting It All Together

Data Representa/ons: IA32 + x86-64

Areas for growth: I love feedback

Lecture 3 CIS 341: COMPILERS

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

1 Number Representation(10 points)

An Experience Like No Other. Stack Discipline Aug. 30, 2006

CSE 351 Midterm - Winter 2017

Stack Discipline Jan. 19, 2018

Systems Programming and Computer Architecture ( )

CSE351 Spring 2018, Midterm Exam April 27, 2018

Machine-level Programming (3)

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

CS429: Computer Organization and Architecture

Function Calls and Stack


CSCI 2021: x86-64 Control Flow

Recitation: Attack Lab

Machine-Level Programming II: Control

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Intel x86-64 and Y86-64 Instruction Set Architecture

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

x86-64 Programming III & The Stack

Buffer Overflow Attack (AskCypert CLaaS)

Machine-Level Programming II: Control

x86 Programming I CSE 351 Winter

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

18-600: Recitation #4 Exploits

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

How Software Executes

CSE 351 Midterm - Winter 2015

Do not turn the page until 5:10.

C++ Named Return Value Optimization

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Compiling C Programs Into x86-64 Assembly Programs

Lecture 4 CIS 341: COMPILERS

CS429: Computer Organization and Architecture

CSE 351 Midterm - Winter 2015 Solutions

C to Machine Code x86 basics: Registers Data movement instructions Memory addressing modes Arithmetic instructions

Topics of this Slideset. CS429: Computer Organization and Architecture. Instruction Set Architecture. Why Y86? Instruction Set Architecture

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Machine Level Programming: Control

University*of*Washington*

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

x86 Programming II CSE 351 Winter

Machine- Level Programming III: Switch Statements and IA32 Procedures

Computer Organization: A Programmer's Perspective

CSE 351 Midterm Exam

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Compiling C Programs Into X86-64 Assembly Programs

Corrections made in this version not seen in first lecture:

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

x86-64 Programming II

Transcription:

CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 CS429 Slideset 9: 1

Mechanisms in Procedures 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. P() y = Q(x) ; printf (y) ; int Q( int i ) int t = 3 i ; int v [10]; return v[ t ]; CS429 Slideset 9: 2

x86-64 Stack Stack "bottom" Region of memory managed with stack discipline Grows toward lower addresses Register %rsp contains lowest stack address address of top element Increasing Addresses. %rsp Stack "top" CS429 Slideset 9: 3

x86-64 Stack Pushing Stack "bottom" Increasing Addresses Pushing pushq Src Decrement %rsp by 8 Write operand at address given by %rsp Stack pointer %rsp 8 Stack "top" Stack Grows Down CS429 Slideset 9: 4

x86-64 Stack Popping Stack "bottom" Increasing Addresses Popping popq Dest Read operand at address given by %rsp Stack pointer %rsp +8 Stack "top" Stack Grows Down Increment %rsp by 8 Write to Dest CS429 Slideset 9: 5

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) 40054D: mov %rax, (%rbx) # Save at dest 400550: pop %rbx # Restore %rbx 400552: retq # Return CS429 Slideset 9: 6

Code Examples (2) long mult2 (long a, long b) long s = a b; return s ; 0000000000400580 <mult2 >: 400580: mov %rdi, %rax # a 400583: imul %rsi, %rax # a b 400587: retq # Return CS429 Slideset 9: 7

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 next instruction right after call Procedure return: ret Pop address from stack Jump to address CS429 Slideset 9: 8

Control Flow Example #1 Poised to execute call in multstore. 0000000000400540 <multstore >: 400544: callq 400580 <mult2> 40054D: mov %rax, (%rbx). 0x130. 0x128. 0x120 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: retq %rsp %rip 0x120 0x400544 CS429 Slideset 9: 9

Control Flow Example #2 After call in multstore. Now in mult2. 0000000000400540 <multstore >: 400544: callq 400580 <mult2> 40054D: mov %rax, (%rbx). 0x130. 0x128. 0x120 0x118 0x40054D 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: retq %rsp %rip 0x118 0x400580 CS429 Slideset 9: 10

Control Flow Example #3 Execute through mult2 to retq. 0000000000400540 <multstore >: 400544: callq 400580 <mult2> 40054D: mov %rax, (%rbx). 0x130. 0x128. 0x120 0x118 0x40054D 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: retq %rsp %rip 0x118 0x400587 CS429 Slideset 9: 11

Control Flow Example #4 After retq in mult2. Back in multstore. 0000000000400540 <multstore >: 400544: callq 400580 <mult2> 40054D: mov %rax, (%rbx). 0x130. 0x128. 0x120 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: retq %rsp %rip 0x120 0x40054D CS429 Slideset 9: 12

Procedure Data Flow Registers: First 6 arguments %rdi %rsi %rdx %rcx %r8 %r9 Mnemonic to remember the order: Diane s silk dress cost $89. Args past 6 are pushed on the stack in reverse order. Return value Stack Arg n Arg 8 Arg 7 %rsp Only allocate stack space when needed. %rax CS429 Slideset 9: 13

Data Flow Examples void multstore (long x, long y, long dest ) long t = mult2(x, y) ; dest = t ; 0000000000400540 <multstore >: # x in %rdi, y in %rsi, dest in %rdx 400540: mov %rdx,%rbx # Save dest 400543: callq 400580 <mult2> # mult2(x, y) # t in %rax 40054C: mov %rax, (%rbx) # Save at dest CS429 Slideset 9: 14

Data Flow Examples (continued) long mult2 (long a, long b) long s = a b; return s ; 0000000000400580 <mult2 >: # a in %rdi, b in %rsi 400580: mov %rdi, %rax # a 400583: imul %rsi, %rax # a b # s in %rax 400587: retq # Return CS429 Slideset 9: 15

Stack-Based Languages Languages that support recursion e.g., C, Pascal, Java Code must be reentrant : multiple simultaneous instantiations of single procedure Need some place to store state of each instantation: arguments, local variables, return pointer Stack discipline State for given procedure needed for a limited time: from call to return Callee returns before caller does Stack allocated in Frames State for a single procedure instantiation CS429 Slideset 9: 16

Call Chain Example Code Structure yoo ()... who() ;... who ()... ami() ;... ami() ;... ami ()... ami() ;... Procedure ami is recursive. yoo who ami ami ami ami CS429 Slideset 9: 17

Stack Frames In IA32, almost every procedure call generated a stack frame. In x86-64, most calls do not generate an explicit frame. 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 returning Finish code Includes pop by ret instruction CS429 Slideset 9: 18 Previous Frame Frame for proc Stack top Frame pointer: %rbp (optional) Stack pointer: %rsp

Example (1) yoo / Executing here : / yoo () who() ; %rbp %rsp Stack yoo CS429 Slideset 9: 19

Example (2) yoo () who() ; yoo who Stack / Executing here : / who () ami() ; %rbp %rsp yoo who CS429 Slideset 9: 20

Example (3) yoo () who () yoo who ami Stack yoo / Executing here : / ami () ami() ; %rbp %rsp who ami CS429 Slideset 9: 21

Example (4) yoo () yoo who Stack who () / Executing here : / ami () ami() ; ami ami %rbp %rsp yoo who ami ami CS429 Slideset 9: 22

Example (5) yoo () who () / Executing here : / ami () ami() ; yoo who ami ami ami %rbp %rsp Stack yoo who ami ami ami CS429 Slideset 9: 23

Example (6) yoo () yoo who Stack who () / Executing here : / ami () ami() ; ami ami %rbp %rsp yoo who ami ami CS429 Slideset 9: 24

Example (7) yoo () who () yoo who ami Stack yoo / Executing here : / ami () ami() ; %rbp %rsp who ami CS429 Slideset 9: 25

Example (8) yoo () who() ; yoo who Stack / Executing here : / who () ami() ; %rbp %rsp yoo who CS429 Slideset 9: 26

Example (9) yoo () who () yoo who ami Stack yoo / Executing here : / ami () ami() ; %rbp %rsp who ami CS429 Slideset 9: 27

Example (10) yoo () who() ; yoo who Stack / Executing here : / who () ami() ; %rbp %rsp yoo who CS429 Slideset 9: 28

Example (11) yoo / Executing here : / yoo () who() ; %rbp %rsp Stack yoo CS429 Slideset 9: 29

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 frame Arguments 7+ Return addr Frame pointer Old %rbp %rbp (optional) Saved Register + Local vars Argument Stack pointer Build %rsp (optional) Caller Stack Frame Return address (pushed by call instruction) Arguments for this call CS429 Slideset 9: 30

Building a Stack Frame IA32 routines almost always constructed an explicit stack frame; x86-64 routines usually don t. If you do build a stack frame: proc : pushq %rbp movq %rsp, %rbp # save caller s frame base # set callee s frame base # body of routine proc movq %rbp, %rsp popq %rbp ret # discard callee s frame # reset caller s frame base Reserve %rbp for this purpose if you re doing this. What does this do? CS429 Slideset 9: 31

Example: incr This example doesn t use explicit frames! 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 CS429 Slideset 9: 32

Example: Calling incr #1 long call incr () long v1 = 15213; Initial Stack Structure long v2 = incr(&v1, 3000) ; return v1 + v2; Rtn addr %rsp call incr : subq $16, %rsp movq $15213, 8(%rsp ) Resulting Stack Structure movl leaq call addq addq ret $3000, %esi 8(%rsp ), %rdi incr 8(%rsp ), %rax $16, %rsp Rtn addr 15213 %rsp+8 Unused %rsp CS429 Slideset 9: 33

Example: Calling incr #2 long call incr () long v1 = 15213; long v2 = incr(&v1, 3000) ; return v1 + v2; Stack Structure call incr : subq $16, %rsp movq $15213, 8(%rsp ) movl $3000, %esi leaq 8(%rsp ), %rdi call incr addq 8(%rsp ), %rax addq $16, %rsp ret Rtn addr 15213 %rsp+8 Unused %rsp Register Use(s) %rdi &v1 %rsi 3000 CS429 Slideset 9: 34

Example: Calling incr #3 long call incr () long v1 = 15213; long v2 = incr(&v1, 3000) ; return v1 + v2; call incr : subq $16, %rsp movq $15213, 8(%rsp ) movl $3000, %esi leaq 8(%rsp ), %rdi Stack Structure Rtn addr 18213 %rsp+8 Unused %rsp call addq addq ret incr 8(%rsp ), %rax $16, %rsp Register Use(s) %rdi &v1 %rsi 3000 CS429 Slideset 9: 35

Example: Calling incr #4 long call incr () long v1 = 15213; long v2 = incr(&v1, 3000) ; return v1 + v2; call incr : subq $16, %rsp movq $15213, 8(%rsp ) movl $3000, %esi leaq 8(%rsp ), %rdi call incr addq addq ret 8(%rsp ), %rax $16, %rsp Stack Structure Rtn addr 18213 %rsp+8 Unused %rsp Register Use(s) %rdi &v1 %rsi 3000 Updated Stack Structure Rtn addr %rsp CS429 Slideset 9: 36

Example: Calling incr #5 long call incr () long v1 = 15213; long v2 = incr(&v1, 3000) ; return v1 + v2; call incr : subq $16, %rsp movq $15213, 8(%rsp ) movl $3000, %esi leaq 8(%rsp ), %rdi call incr addq 8(%rsp ), %rax addq $16, %rsp ret Updated Stack Structure Rtn addr %rsp Register Use(s) %rax Return value Final Stack Structure %rsp CS429 Slideset 9: 37

Register Saving Conventions When procedure yoo calls who: yoo is the caller who is the callee Can register be used for temporary storage? yoo : movq $15213, %rdx call who addq %rdx, %rax ret who: subq $18213, %rdx ret Contents of register %rdx are overwritten by who This could be trouble; something should be done! Need to coordinate between caller and callee. CS429 Slideset 9: 38

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 CS429 Slideset 9: 39

x86-64 Linux Caller-saved Registers %rax Return value Also caller-saved Can be modified by procedure %rdi,%rsi,%rdx,%rcx,%r8,%r9 Arguments Also caller-saved Can be modified by procedure %r10, %r11 Caller-saved Can be modified by procedure Return value %rax Argument 1 %rdi Argument 2 %rsi Argument 3 %rdx Argument 4 %rcx Argument 5 %r8 Argument 6 %r9 Caller-saved %r10 temporaries %r11 CS429 Slideset 9: 40

x86-64 Linux Callee-saved Registers %rbx, %r12, %r13, %r14 Callee-saved Callee must save and restore %rbp %rsp Callee-saved Callee must save and restore May be used as frame pointer Can mix and match Special form of callee-saved Restored to original value upon exit from procedure %rbx Callee-saved %r12 Temporaries %r13 %r14 Special %rbp Special %rsp CS429 Slideset 9: 41

Callee-Saved Example #1 long call incr2 ( long x ) long v1 = 15213; long v2 = incr ( &v1, 3000 ) ; return x + v2; 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 CS429 Slideset 9: 42 Initial Stack Structure Rtn address %rsp Resulting Stack Structure Rtn address Saved %rbx 15213 %rsp+8 Unused %rsp

Callee-Saved Example #2 long call incr2 ( long x ) long v1 = 15213; long v2 = incr ( &v1, 3000 ) ; return x + v2; 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 CS429 Slideset 9: 43 Resulting Stack Structure Rtn address Saved %rbx 15213 %rsp+8 Unused %rsp Pre-return Stack Structure Rtn address %rsp

Recursive Function / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq $1, %rdi call pcount r addq %rbx, %rax popq %rbx.l6 : ret CS429 Slideset 9: 44

Recursive Function Terminal Case / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; Register Use(s) %rdi Argument x %rax Return value pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq movq andl shrq call addq popq.l6 : ret %rbx %rdi, %rbx $1, %ebx $1, %rdi pcount r %rbx, %rax %rbx CS429 Slideset 9: 45

Recursive Function Register Save / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; Rtn address Saved %rbx %rsp CS429 Slideset 9: 46 pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq movq andl shrq call addq popq.l6 : ret %rbx %rdi, %rbx $1, %ebx $1, %rdi pcount r %rbx, %rax %rbx Register Use(s) %rdi Argument x %rax Return value

Recursive Function Call Setup / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; Register Use(s) Type %rdi x >> 1 Rec. argument %rax x & 1 Caller-saved pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq %rbx movq andl shrq call addq popq.l6 : ret %rdi, %rbx $1, %ebx $1, %rdi pcount r %rbx, %rax %rbx CS429 Slideset 9: 47

Recursive Function Call / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq $1, %rdi call pcount r Register Use(s) Type %rbx x & 1 Callee-saved %rax Recursive call return value addq popq.l6 : ret %rbx, %rax %rbx CS429 Slideset 9: 48

Recursive Function Result / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; Register Use(s) Type %rbx x & 1 Callee-saved %rax Return value pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq $1, %rdi call pcount r addq popq.l6 : ret %rbx, %rax %rbx CS429 Slideset 9: 49

Recursive Function Completion / Recursive popcount / long pcount r (unsigned long x) if (x == 0) return 0; else return (x & 1) + pcount r (x >> 1) ; Register Use(s) Type %rax Return value pcount r : movl $0, %eax testq %rdi, %rdi je.l6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq $1, %rdi call pcount r addq %rbx, %rax popq.l6 : ret %rbx %rsp CS429 Slideset 9: 50

Observations About Recursion Handled without Special Consideration Stack frames mean that each function call has private storage Saved registers and local variables Saved return pointer Register saving conventions prevent one function call from corrupting another s data Unless the C code explicitly does so (e.g., buffer overflow) Stack discipline follows call/return pattern If P calls Q, then Q returns before P Last-In, First-Out Also works for mutual recursion P calls Q; Q calls P CS429 Slideset 9: 51

x86-64 Procedure Summary Important Points Stack is the right data structure for procedure call / return; if P calls Q, then Q returns before P Recursion (and mutual recursion) are 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 return in %rax Pointers are addresses of values (on stack or global) Caller frame Arguments 7+ Return addr Frame pointer Old %rbp %rbp (optional) Saved Register + Local vars Argument Stack pointer Build %rsp (optional) CS429 Slideset 9: 52