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

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

Machine-level Programs Procedure

CS429: Computer Organization and Architecture

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Machine-Level Programming (2)

Machine- Level Representa2on: Procedure

CS213. Machine-Level Programming III: Procedures

The Stack & Procedures

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

Procedures and the Call Stack

Machine Program: Procedure. Zhaoguo Wang

Systems I. Machine-Level Programming V: Procedures

University of Washington

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

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

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

Assembly Programming IV

The Stack & Procedures

Giving credit where credit is due

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

Compiling C Programs Into x86-64 Assembly Programs

Assembly Language: Function Calls

Sungkyunkwan University

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Systems Programming and Computer Architecture ( )

Machine Programming 3: Procedures

Machine-Level Programming III: Procedures

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

Machine-level Programming (3)

x86-64 Programming III

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

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly Programming IV

CSE 351 Midterm Exam Spring 2016 May 2, 2015

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

Do not turn the page until 5:10.

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

x86-64 Programming III & The Stack

1 Number Representation(10 points)

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

Function Calls and Stack

x86 Programming II CSE 351 Winter

CSE 351 Midterm - Winter 2015

Intel x86-64 and Y86-64 Instruction Set Architecture

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

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

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

Areas for growth: I love feedback

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

CSE 351 Midterm - Winter 2015 Solutions

Lecture 3 CIS 341: COMPILERS

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

Data Representa/ons: IA32 + x86-64

Computer Organization: A Programmer's Perspective

CSCI 2021: x86-64 Control Flow

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

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

Machine Language CS 3330 Samira Khan

CS429: Computer Organization and Architecture

Question 1: Number Representation

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

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

Buffer Overflow Attack (AskCypert CLaaS)

Machine Level Programming: Basics

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

Recitation: Attack Lab

Machine Level Programming: Basics

Lecture 4: x86_64 Assembly Language

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

x86 Programming I CSE 351 Winter

CSE 351 Midterm Exam

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

x86 64 Programming II

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

x86 64 Programming I CSE 351 Autumn 2017 Instructor: Justin Hsia

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

Question Points Score Total: 100

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

18-600: Recitation #4 Exploits

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Compiling C Programs Into X86-64 Assembly Programs

Changelog. Brief Assembly Refresher. a logistics note. last time

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

x86-64 Programming II

Corrections made in this version not seen in first lecture:

University*of*Washington*

Introduction to Compilers and Language Design Copyright (C) 2017 Douglas Thain. All rights reserved.

Machine/Assembler Language Putting It All Together

CSE 351 Midterm - Winter 2017

CSE 401/M501 Compilers

Setting & Examining Condition Codes. int gt(long x, long y) { return x > y; } int has_nonzero_masked(long x, long mask) { return!!

Transcription:

Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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 All implemented with machine instructions int P(...) {.. y = Q(x); print(y);. } int Q(int i) { } int t = 3*i; int v[10];.. urn v[t]; 2

x86-64 Stack Region of memory managed with stack discipline Last-In, First-Out (LIFO) Push & Pop Grows toward lower addresses Register contains lowest stack address Address of top element Stack Pointer Stack Bottom Increasing Addresses Stack Grows Down Stack Top 3

x86-64 Stack: Push pushq Src Fetch operand at Src Decrement by 8 Write operand at address given by Stack Bottom Increasing Addresses Stack Pointer -8 Stack Grows Down Stack Top 4

x86-64 Stack: Pop popq Dest Read value at address given by Increment by 8 Store value at Dest (must be a register) Stack Bottom Increasing Addresses Stack Pointer +8 Stack Grows Down Stack Top 5

x86-64 Stack: Example Stack operation examples pushq %rax popq %rdx 0x210 0x210 0x210 0x208 0x208 0x208 0x200 123 0x200 123 0x200 123 0x1f8 213 0x1f8 213 %rax 213 %rax 213 %rax 213 %rdx 555 %rdx 555 %rdx 555 213 0x200 0x108 0x1f8 0x104 0x200 6

Procedure Control Flow Use stack to support procedure call and urn Procedure call: call label Push urn address on stack Address of the next instruction right after call Jump to label Procedure urn: Pop address from stack Jump to address 7

Example 0000000000400546 <main>: 400546: 48 8d 64 24 f8 lea -0x8(), 40054b: bf 0a 00 00 00 mov $0xa,%edi 400550: e8 13 00 00 00 callq 400568 <fact> 400555: 48 89 c7 mov %rax,%rdi 400558: e8 21 00 00 00 callq 40057e <print> 40055d: b8 00 00 00 00 mov $0x0,%eax 400562: 48 8d 64 24 08 lea 0x8(), 400567: c3 q 0000000000400568 <fact>: 400568: b8 01 00 00 00 mov $0x1,%eax 40056d: eb 08 jmp 400577 <fact+0xf> 40056f: 48 0f af c7 imul %rdi,%rax 400573: 48 83 ef 01 sub $0x1,%rdi 400577: 48 83 ff 01 cmp $0x1,%rdi 40057b: 7f f2 jg 40056f <fact+0x7> 40057d: c3 q 8

Procedure Call Example 400550: e8 13 00 00 00 callq 400568 <fact> 400555: 48 89 c7 mov %rax,%rdi callq 400568 0x00400555 +0x00000013 =0x00400568 0x120 0x118 0x110 123 0x120 0x118 0x110 123 0x108 0x400555 0x110 0x108 %rip 0x400550 %rip 0x804854e 0x400568 %rip is program counter 9

Procedure Return Example 40057d: c3 q 0x120 0x118 0x110 0x108 0x120 0x118 123 0x110 0x400555 0x108 123 0x400555 0x108 0x104 0x110 %rip 0x40057d %rip 0x8048591 0x400555 %rip is program counter 10

Passing Arguments First 6 arguments: Diane s silk dress costs $89 %rdi %rsi %rdx %rcx Remaining arguments: Push the rest on the stack in reverse order Only allocate stack space when needed %r8 %r9 Return value %rax Arg n Arg 8 Arg 7 Increasing Addresses 11

Stack-based Languages Languages that support recursion (e.g. C, C++, Pascal, Java) Code must be Reentrant Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments, local variables, urn address Stack discipline State for given procedure needed for limited time From when called to when urn Callee urns before caller does Stack allocated in frames State for single procedure instantiation 12

Stack Frame Contents Return information Arguments Local variables & temp space Frame Pointer (Optional) %rbp Management Set-up code: space allocated Stack Pointer when enter procedure Finish code: deallocate when urn Stack pointer indicates stack top Optional frame pointer %rbp indicates start of current frame Previous Frame Frame for proc Stack Top 13

Stack Frames: Example (1) Code Structure yoo( ) { who(); } who( ) { (); (); } Procedure recursive ( ) { (); } Call Chain yoo who 14

Stack Frames: Example (2) yoo( ) { who(); } Call Chain yoo Frame Pointer %rbp Stack Pointer yoo 15

Stack Frames: Example (3) who( ) { (); (); Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who } 16

Stack Frames: Example (4) ( ) { (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 17

Stack Frames: Example (5) ( ) { (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 18

Stack Frames: Example (6) ( ) { (); } Call Chain yoo who yoo who Frame Pointer %rbp Stack Pointer 19

Stack Frames: Example (7) ( ) { (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 20

Stack Frames: Example (8) ( ) { (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 21

Stack Frames: Example (9) who( ) { (); (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 22

Stack Frames: Example (10) ( ) { } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 23

Stack Frames: Example (11) who( ) { (); (); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo who 24

Stack Frames: Example (12) yoo( ) { who(); } Call Chain yoo who Frame Pointer %rbp Stack Pointer yoo 25

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 stack frame Return address Pushed by call instruction Arguments for this call Caller Frame Frame Pointer (optional) %rbp Stack Pointer Arguments 7+ Return Addr Old %rbp Saved Registers + Local Variables Argument Build 26

Revisiting Swap long v1 = 1111; long v2 = 2222; void swap (long *xp, long *yp) { long t = *xp; *xp = *yp; *yp = t; } int main (void) { swap (&v1, &v2);... } v2: v1:.quad 2222....quad 1111... swap: movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) main:... movq movq call... $v2, %rsi $v1, %rdi swap 27

Register Saving Problem 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 who: subq $91125, %rdx Contents of register %rdx overwritten by who() 28

Register Saving Conventions Caller saved registers Caller saves temporary values in its frame before the call Contents of these registers can be modified as a result of procedure call x86-64: %rax, %rdi, %rsi, %rdx, %rcx, %r8, %r9, %r10, %r11 Callee saved registers Callee saves temporary values in its frame before using Callee restores them before urning to caller The contents of these registers are preserved across a procedure call x86-64: %rbx, %r12, %r13, %r14, %r15, %rbp 29

x86-64/linux Register Usage (1) %rax Return value Also caller-saved Can be modified by procedure %rdi,, %r9 Arguments Also caller-saved Can be modified by procedure %r10, %r11 Caller-saved Can be modified by procedure Return value Arguments Caller-saved temporaries %rax %rdi %rsi %rdx %rcx %r8 %r9 %r10 %r11 30

x86-64/linux Register Usage (2) %rbx, %r12, %r13, %r14, %r15 Callee-saved Callee must save & restore %rbp Callee-saved Callee must save & restore May be used as frame pointer Special from of callee save Restored to original value upon exit from procedure Callee-saved Temporaries Special %rbx %r12 %r13 %r14 %r15 %rbp 31

Recursive Factorial: rfact Registers %rax used without first saving %rbx used, but save at beginning & restore at end long rfact(long x) { long rval; if (x <= 1) urn 1; rval = rfact(x-1); urn rval * x; } rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx 32

Example: rfact(3) RetAddr Registers %rdi 3 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx? 33

Example: rfact(3) RetAddr? Registers %rdi 3 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx? 34

Example: rfact(3) RetAddr? Registers %rdi 3 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 35

Example: rfact(3) RetAddr? Registers %rdi 2 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 36

Example: rfact(3) RetAddr? A Registers %rdi 2 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 37

Example: rfact(3) RetAddr? A Registers %rdi 2 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 38

Example: rfact(3) RetAddr? A 3 Registers %rdi 2 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 39

Example: rfact(3) RetAddr? A 3 Registers %rdi 2 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 40

Example: rfact(3) RetAddr? A 3 Registers %rdi 1 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 41

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 42

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax? Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 43

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 1 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 44

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 1 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 45

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 2 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 2 46

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 2 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 47

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 2 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 48

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 6 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx 3 49

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 6 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx? 50

Example: rfact(3) RetAddr? A 3 A Registers %rdi 1 %rax 6 Lower Address %rip rfact: cmpq $1, %rdi jle.l3 pushq %rbx movq %rdi, %rbx leaq -1(%rdi), %rdi call rfact A: imulq %rbx, %rax jmp.l2.l3: movl $1, %eax.l2: popq %rbx %rbx? 51

Observations about Recursion Handled without special consideration Stack frames mean each function call has private storage Saved registers + local variables Saved urn address 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 / urn pattern If P calls Q, then Q urns before P Last-In, First-Out Also works for mutual recursion P calls Q; Q calls P 52

Summary Stack is the right data structure for procedure call / urn Private storage for each instance of procedure call Recursion handled by normal calling conventions Mechanisms Call,, push, pop, etc. instructions Registers for passing arguments and urn value Stack memory Policies Register usage (caller / callee save, %rbp & ) Stack frame organization 53