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

Similar documents
CS429: Computer Organization and Architecture

Machine-level Programs Procedure

Machine-Level Programming III: Procedures

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

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

Machine- Level Representa2on: Procedure

Machine-Level Programming (2)

Procedures and the Call Stack

The Stack & Procedures

The Stack & Procedures

Machine Program: Procedure. Zhaoguo Wang

University of Washington

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

CS213. Machine-Level Programming III: Procedures

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Sungkyunkwan University

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

Systems I. Machine-Level Programming V: Procedures

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

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

Giving credit where credit is due

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

Assembly Language: Function Calls

Assembly Programming IV

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

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

%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

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

X86 Assembly -Procedure II:1

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

Machine Programming 3: Procedures

Assembly Programming IV

Data Representa/ons: IA32 + x86-64

x86-64 Programming III

Machine-level Programming (3)

Stack Discipline Jan. 19, 2018

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

Compiling C Programs Into x86-64 Assembly Programs

Function Calls and Stack

Building an Executable

Lecture 4: x86_64 Assembly Language

Areas for growth: I love feedback

Assembly Language: Function Calls" Goals of this Lecture"

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Lecture 3 CIS 341: COMPILERS

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

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

Foundations of Computer Systems

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

CS429: Computer Organization and Architecture

University*of*Washington*

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


Credits and Disclaimers

Machine Language CS 3330 Samira Khan

Machine-Level Programming III: Procedures

x86 Programming I CSE 351 Winter

Lecture 4 CIS 341: COMPILERS

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

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CSE351 Spring 2018, Midterm Exam April 27, 2018

Assembly Language: Function Calls" Goals of this Lecture"

Recitation: Attack Lab

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

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

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

How Software Executes

x86-64 Programming III & The Stack

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

Intel x86-64 and Y86-64 Instruction Set Architecture

Assembly Language: Function Calls

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

How Software Executes

Buffer Overflow Attack (AskCypert CLaaS)

Systems Programming and Computer Architecture ( )

1 Number Representation(10 points)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Computer Organization: A Programmer's Perspective

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

18-600: Recitation #4 Exploits

CSC 8400: Computer Systems. Using the Stack for Function Calls

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

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Credits and Disclaimers

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

Machine- Level Programming III: Switch Statements and IA32 Procedures

Do not turn the page until 5:10.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Question Points Score Total: 100

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

CS429: Computer Organization and Architecture

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

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 Mechanisms in Procedures Passing Control To beginning of procedure code Back to urn point Passing Data Procedure arguments Return value Memory Management Allocate during procedure execution Deallocate upon urn 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]; urn v[ t ]; CS429 Slideset 9: 1 CS429 Slideset 9: 2 x86-64 x86-64 Pushing "bottom" "bottom" Region of memory managed with stack discipline Grows toward lower addresses Increasing Addresses. Increasing Addresses Register contains lowest stack address address of top element "top" Pushing pushq Src Decrement by 8 Write operand at address given by pointer 8 "top" Grows Down CS429 Slideset 9: 3 CS429 Slideset 9: 4

x86-64 Popping Code Examples "bottom" void multstore (long x, long y, long dest ) long t = mult2(x, y) ; dest = t ; Popping popq Dest Read operand at address given by Increment by 8 pointer +8 "top" Increasing Addresses Grows Down 0000000000400540 <multstore >: 400540: push # Save 400541: mov %rdx, # Save dest 400544: q 400550 <mult2> # mult2(x, y) 40054D: mov %rax, () # Save at dest 400550: pop # Restore 400552: q # Return Write to Dest CS429 Slideset 9: 5 CS429 Slideset 9: 6 Code Examples (2) Procedure Control Flow long mult2 (long a, long b) long s = a b; urn s ; 0000000000400580 <mult2 >: 400580: mov %rdi, %rax # a 400583: imul %rsi, %rax # a b 400587: q # Return Use stack to support procedure and urn Procedure : label Push urn address on stack Jump to label Return address: Address of next instruction right after Procedure urn: Pop address from stack Jump to address CS429 Slideset 9: 7 CS429 Slideset 9: 8

Control Flow Example #1 Control Flow Example #2 Poised to execute in multstore. After in multstore. Now in mult2. 0000000000400540 <multstore >: 400544: q 400580 <mult2> 40054D: mov %rax, (). 0x130. 0x128. 0x120 0000000000400540 <multstore >: 400544: q 400580 <mult2> 40054D: mov %rax, (). 0x130. 0x128. 0x120 0x118 0x40054D 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: q %rip 0x120 0x400544 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: q %rip 0x118 0x400580 CS429 Slideset 9: 9 CS429 Slideset 9: 10 Control Flow Example #3 Control Flow Example #4 Execute through mult2 to q. After q in mult2. Back in multstore. 0000000000400540 <multstore >: 400544: q 400580 <mult2> 40054D: mov %rax, (). 0x130. 0x128. 0x120 0x118 0x40054D 0000000000400540 <multstore >: 400544: q 400580 <mult2> 40054D: mov %rax, (). 0x130. 0x128. 0x120 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: q %rip 0x118 0x400587 0000000000400580 <mult2 >: 400580: mov %rdi, %rax 400587: q %rip 0x120 0x40054D CS429 Slideset 9: 11 CS429 Slideset 9: 12

Procedure Data Flow Data Flow Examples 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 %rax Arg n Arg 8 Arg 7 Only allocate stack space when needed. 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, # Save dest 400543: q 400580 <mult2> # mult2(x, y) # t in %rax 40054C: mov %rax, () # Save at dest CS429 Slideset 9: 13 CS429 Slideset 9: 14 Data Flow Examples (continued) -Based Languages long mult2 (long a, long b) long s = a b; urn s ; 0000000000400580 <mult2 >: # a in %rdi, b in %rsi 400580: mov %rdi, %rax # a 400583: imul %rsi, %rax # a b # s in %rax 400587: q # Return 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, urn pointer discipline State for given procedure needed for a limited time: from to urn Callee urns before er does allocated in Frames State for a single procedure instantiation CS429 Slideset 9: 15 CS429 Slideset 9: 16

Call Chain Example Frames Code Structure () () ; () () ; () ; () () ; CS429 Slideset 9: 17 Procedure is recursive. In IA32, almost every procedure generated a stack frame. In x86-64, most s 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 instruction Deallocated when urning Finish code Includes pop by instruction CS429 Slideset 9: 18 Previous Frame Frame for proc top Frame pointer: %rbp (optional) pointer: Example (1) Example (2) () () ; () () ; () () ; CS429 Slideset 9: 19 CS429 Slideset 9: 20

Example (3) Example (4) () () () () ; () () () () ; CS429 Slideset 9: 21 CS429 Slideset 9: 22 Example (5) Example (6) () () () () ; () () () () ; CS429 Slideset 9: 23 CS429 Slideset 9: 24

Example (7) Example (8) () () () () ; () () ; () () ; CS429 Slideset 9: 25 CS429 Slideset 9: 26 Example (9) Example (10) () () () () ; () () ; () () ; CS429 Slideset 9: 27 CS429 Slideset 9: 28

Example (11) x86-64/linux Frame () () ; Current Frame ( Top to Bottom) Argument build: parameters for function about to 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) pointer Saved Register + Local vars Argument Build (optional) Caller Frame Return address (pushed by instruction) Arguments for this CS429 Slideset 9: 29 CS429 Slideset 9: 30 Building a Frame Example: incr 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, %rbp # save er s frame base # set ee s frame base # body of routine proc This example doesn t use explicit frames! long incr (long p, long val ) long x = p; long y = x + val ; p = y; urn x; movq %rbp, # discard ee s frame popq %rbp # reset er s frame base Reserve %rbp for this purpose if you re doing this. What does this do? incr : movq (%rdi ), %rax %rax, %rsi movq %rsi, (%rdi ) %rdi Argument p %rsi Argument val, y %rax x, urn value CS429 Slideset 9: 31 CS429 Slideset 9: 32

Example: Calling incr #1 Example: Calling incr #2 long incr () long v2 = incr(&v1, 3000) ; urn v1 + v2; incr : subq $16, movq $15213, 8( ) movl leaq $3000, %esi 8( ), %rdi incr 8( ), %rax $16, Initial Structure Rtn addr Resulting Structure Rtn addr 15213 +8 Unused long incr () long v2 = incr(&v1, 3000) ; urn v1 + v2; incr : subq $16, movq $15213, 8( ) movl leaq $3000, %esi 8( ), %rdi incr 8( ), %rax $16, Structure Rtn addr 15213 +8 Unused %rdi &v1 %rsi 3000 CS429 Slideset 9: 33 CS429 Slideset 9: 34 Example: Calling incr #3 Example: Calling incr #4 long incr () long v2 = incr(&v1, 3000) ; urn v1 + v2; incr : subq $16, movq $15213, 8( ) movl $3000, %esi leaq 8( ), %rdi incr 8( ), %rax $16, Structure Rtn addr 18213 +8 Unused %rdi &v1 %rsi 3000 long incr () long v2 = incr(&v1, 3000) ; urn v1 + v2; incr : subq $16, movq $15213, 8( ) movl $3000, %esi leaq 8( ), %rdi incr 8( ), %rax $16, Structure Rtn addr 18213 +8 Unused %rdi &v1 %rsi 3000 Updated Structure Rtn addr CS429 Slideset 9: 35 CS429 Slideset 9: 36

Example: Calling incr #5 long incr () long v2 = incr(&v1, 3000) ; urn v1 + v2; incr : subq $16, movq $15213, 8( ) movl $3000, %esi leaq 8( ), %rdi incr 8( ), %rax $16, Updated Structure Rtn addr %rax Return value Final Structure Register Saving Conventions When procedure s : is the er is the ee Can register be used for temporary storage? : movq $15213, %rdx %rdx, %rax : subq $18213, %rdx Contents of register %rdx are overwritten by This could be trouble; something should be done! Need to coordinate between er and ee. CS429 Slideset 9: 37 CS429 Slideset 9: 38 Register Saving Conventions When procedure s : is the er is the ee Can register be used for temporary storage? Conventions Caller Saved : Caller saves temporary values in its frame before the Callee Saved : Callee saves temporary values in its frame before using Callee restores them before urning to er x86-64 Linux Caller-saved Registers %rax Return value Also er-saved Can be modified by procedure %rdi,%rsi,%rdx,%rcx,%r8,%r9 Arguments Also er-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: 39 CS429 Slideset 9: 40

x86-64 Linux Callee-saved Registers, %r12, %r13, %r14 %rbp Callee-saved Callee must save and restore Callee-saved Callee must save and restore May be used as frame pointer Can mix and match Special form of ee-saved Restored to original value upon exit from procedure Callee-saved %r12 Temporaries %r13 %r14 Special %rbp Special Callee-Saved Example #1 long incr2 ( long x ) long v2 = incr ( &v1, 3000 ) ; urn x + v2; incr2 : pushq subq $16, movq %rdi, movq $15213, 8( ) movl $3000, %esi leaq 8( ), %rdi incr, %rax $16, popq Initial Structure Rtn address Resulting Structure Rtn address Saved 15213 +8 Unused CS429 Slideset 9: 41 CS429 Slideset 9: 42 Callee-Saved Example #2 Recursive Function long incr2 ( long x ) long v2 = incr ( &v1, 3000 ) ; urn x + v2; incr2 : pushq subq $16, movq %rdi, movq $15213, 8( ) movl $3000, %esi leaq 8( ), %rdi incr, %rax $16, popq Resulting Structure Rtn address Saved 15213 +8 Unused Pre-urn Structure Rtn address urn 0; urn (x & 1) pushq movq %rdi, andl $1, %ebx shrq $1, %rdi pcount r, %rax popq CS429 Slideset 9: 43 CS429 Slideset 9: 44

Recursive Function Terminal Case Recursive Function Register Save urn 0; urn (x & 1) %rdi Argument x %rax Return value pushq movq andl shrq popq %rdi, $1, %ebx $1, %rdi pcount r, %rax urn 0; urn (x & 1) Rtn address Saved pushq movq andl shrq popq %rdi, $1, %ebx $1, %rdi pcount r, %rax %rdi Argument x %rax Return value CS429 Slideset 9: 45 CS429 Slideset 9: 46 Recursive Function Call Setup Recursive Function Call urn 0; urn (x & 1) Type %rdi x >> 1 Rec. argument %rax x & 1 Caller-saved pushq movq andl shrq popq %rdi, $1, %ebx $1, %rdi pcount r, %rax urn 0; urn (x & 1) Type x & 1 Callee-saved %rax Recursive urn value pushq movq %rdi, andl $1, %ebx shrq $1, %rdi popq pcount r, %rax CS429 Slideset 9: 47 CS429 Slideset 9: 48

Recursive Function Result Recursive Function Completion urn 0; urn (x & 1) Type x & 1 Callee-saved %rax Return value pushq movq %rdi, andl $1, %ebx shrq $1, %rdi pcount r popq, %rax urn 0; urn (x & 1) Type %rax Return value pushq movq %rdi, andl $1, %ebx shrq $1, %rdi pcount r, %rax popq CS429 Slideset 9: 49 CS429 Slideset 9: 50 Observations About Recursion Handled without Special Consideration frames mean that each function has private storage Saved registers and local variables Saved urn pointer Register saving conventions prevent one function from corrupting another s data Unless the C code explicitly does so (e.g., buffer overflow) discipline follows /urn pattern If P s Q, then Q urns before P Last-In, First-Out Also works for mutual recursion P s Q; Q s P x86-64 Procedure Summary Important Points is the right data structure for procedure / urn; if P s Q, then Q urns before P Recursion (and mutual recursion) are handled by normal ing conventions Can safely store values in local stack frame and in ee-saved registers Put function arguments at top of stack Result urn 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 pointer Build (optional) CS429 Slideset 9: 51 CS429 Slideset 9: 52