Machine Program: Procedure. Zhaoguo Wang

Size: px
Start display at page:

Download "Machine Program: Procedure. Zhaoguo Wang"

Transcription

1 Machine Program: Procedure Zhaoguo Wang

2 Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z;

3 Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control 2. Passing Arguments & return value int Q(int i) { int t, z; return z;

4 Requirements of procedure calls? P() { y = Q(x); y++; int Q(int i) { int t, z; return z; 1. Passing control 2. Passing Arguments & return value 3. Allocate / deallocate local variables

5 Carnegie Mellon How to transfer control for procedure calls? void main(){ f() L1: void f(){ g() L2: void g(){ h() L3:

6 How to transfer control for procedure calls? void main(){ f() L1: Jump to f() Remember where to come back Carnegie Mellon void f(){ g() L2: L1 void g(){ h() L3:

7 How to transfer control for procedure calls? void main(){ f() L1: Jump to f() Remember where to come back Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to g() Remember where to come back L2 L1

8 How to transfer control for procedure calls? void main(){ f() L1: Jump to f() Remember where to come back Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to g() Remember where to come back Jump to h() Remember where to come back L3 L2 L1

9 How to transfer control for procedure calls? void main(){ f() L1: Jump to f() Remember where to come back Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to g() Remember where to come back Jump to L3 Forget L3 L3 L2 L1

10 How to transfer control for procedure calls? void main(){ f() L1: Jump to f() Remember where to come back Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to L2 Forget L2 Jump to L3 Forget L3 L3 L2 L1

11 How to transfer control for procedure calls? void main(){ f() L1: Jump to L1 Forget L1 Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to L2 Forget L2 Jump to L3 Forget L3 L3 L2 L1

12 How to transfer control for procedure calls? void main(){ f() L1: Jump to L1 Forget L1 Carnegie Mellon void f(){ g() L2: void g(){ h() L3: Jump to L2 Forget L2 Jump to L3 Forget L3 L3 L2 L1 Stack

13 Stack Grows Down 0x x x x x x x x x x x x x x x x x x Memory BOTTOM (Initial ESP Value) TOP CPU PC: IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

14 Stack push Instruction pushq src Decrement %rsp by 8 Write operand at address given by %rsp

15 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 pushq %rdi Memory TOP PC PC: 0x IR: pushq %rdi RAX: RBX: RCX: RDX: RSI: RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

16 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 pushq %rdi Memory TOP 1. %rsp = %rsp - 8 PC PC: 0x IR: pushq %rdi RAX: RBX: RCX: RDX: RSI: RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

17 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 0x5 pushq %rdi Memory TOP 1. %rsp = %rsp mem[%rsp] = %rdi PC PC: 0x IR: pushq %rdi RAX: RBX: RCX: RDX: RSI: RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

18 Stack pop Instruction popq dest Store the value at address %rsp to dest Increment %rsp by 8

19 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 0x5 popq %rsi pushq %rdi Memory TOP PC PC: 0x IR: popq %rsi RAX: RBX: RCX: RDX: RSI: RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

20 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 0x5 popq %rsi pushq %rdi Memory TOP 1. %rsi = mem[%rsp] PC PC: 0x IR: popq %rsi RAX: RBX: RCX: RDX: RSI: 0x5 RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

21 0x x x1 0x2 BOTTOM (Initial ESP Value) CPU 0x x x x x x x x x x x x x x x x x3 0x4 0x5 popq %rsi pushq %rdi Memory TOP 1. %rsi = mem[%rsp] 2. %rsp = %rsp + 8 PC PC: 0x IR: popq %rsi RAX: RBX: RCX: RDX: RSI: 0x5 RDI: 0x5 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

22 Control transfer call Instruction call label(func name) Push return address on stack Current pc + 8 Jump label Change the pc to the address of the label int add(int a, int b) { int c = a + b; return c; int main() { int a = 0; int b = 2; int c = add(a, b); printf("%d\b", c); return 0;

23 Control transfer call Instruction call label(func name) Push return address on stack Current pc + 8 Jump label Change the pc to the address of the label add: leal (%rdi,%rsi), %eax ret GCC O1 *.c main: movl movl call movl $2, %esi $0, %edi add %eax, %edx

24 Control transfer call Instruction ret Pop 8 bytes from the stack to PC pc = mem[%rsp] add: leal (%rdi,%rsi), %eax ret GCC O1 *.c main: movl movl call movl $2, %esi $0, %edi add %eax, %edx

25 0x x BOTTOM TOP CPU 0x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory PC PC: 0x IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: RBP: movl $2, %esi 0x ZF: 0 SF: 0 CF: 0 OF: 0

26 0x x BOTTOM TOP CPU 0x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory PC PC: 0x IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: RBP: movl $2, %esi 0x2 0x ZF: 0 SF: 0 CF: 0 OF: 0

27 0x x BOTTOM TOP CPU 0x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory PC PC: 0x IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: RBP: movl $9, %edi 0x2 0x0 0x ZF: 0 SF: 0 CF: 0 OF: 0

28 0x x BOTTOM TOP CPU 0x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory PC PC: 0x IR: call add RAX: RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

29 0x x BOTTOM TOP CPU 0x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory 1.push return address on stack. PC PC: 0x IR: call add RAX: RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

30 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP 1.push return address on stack. PC PC: 0x IR: call add RAX: RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

31 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP 1.push return address on stack. 2.Jump to add PC PC: 0x IR: call add RAX: RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

32 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP 1.push return address on stack. 2.Jump to add PC PC: 0x IR: call add RAX: RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

33 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP 1.push return address on stack. 2.Jump to add PC PC: IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: RBP: 0x leal (%rdi,%rsi), %eax 0x2 0x0 0x ZF: 0 SF: 0 CF: 0 OF: 0

34 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP PC PC: IR: RAX: RBX: RCX: RDX: RSI: RDI: RSP: RBP: 0x leal (%rdi,%rsi), %eax 0x2 0x2 0x0 0x ZF: 0 SF: 0 CF: 0 OF: 0

35 0x x BOTTOM CPU 0x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory TOP pop 8 bytes to PC PC PC: 0x IR: ret RAX: 0x2 RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

36 0x x x x x x x x x x x x x x x x x x movl %eax, %edx call add movl $0, %edi movl $2, %esi ret leal (%rdi,%rsi), %eax Memory BOTTOM TOP pop 8 bytes to PC PC CPU PC: 0x IR: ret RAX: 0x2 RBX: RCX: RDX: RSI: 0x2 RDI: 0x0 RSP: 0x RBP: ZF: 0 SF: 0 CF: 0 OF: 0

37 C s calling convention: args/return values Registers First 6 arguments %rdi %rsi %rdx %rcx %r8 %r9 Return value Stack Arg n Arg 8 Arg 7 %rax Only allocate stack space when needed

38 C s calling convention: args/return values Registers First 6 Arguments: %rdi, %rsi, %rdx, %rcx, %r8, %9 Return value: %rax int add(int a, int b, int c, int d, int e, int f, int g, int h) { int r = a + b + c + d + e + f + g + h; return r; int main() { int c = add(1, 2, 3, 4, 5, 6, 7, 8); printf("%d\b", c); return 0;

39 C s calling convention: args/return values int add(int a, int b, int c, int d, int e, int f, int g, int h) { int r = a + b + c + d + e + f + g + h; return r; main: subq $8, %rsp pushq $8 pushq $7 movl $6, %r9d movl $5, %r8d movl $4, %ecx movl $3, %edx movl $2, %esi movl $1, %edi call add add: addl addl addl addl addl movl addl addl ret %esi, %edi %edi, %edx %edx, %ecx %r8d, %ecx %r9d, %ecx %ecx, %eax 8(%rsp), %eax 16(%rsp), %eax

40 How to allocate/deallocate local variables? Use registers whenever possible Allocate local variables on the stack subq $0x8,%rsp //allocate 8 bytes movq $1, 8(%rsp) //store 1 in the allocated 8 bytes

41 Carnegie Mellon Calling convention: Register Saving When procedure f calls g: f is the caller, g is the callee Can caller assume register values do not change when callee returns? If not, caller must save register values (in memory) that it needs to use them later

42 Carnegie Mellon Calling convention: register saving Some registers are caller saved, others are callee saved Caller saved Caller saves caller saved registers on stack before the call Callee saved Callee saves callee saved registers on stack before using Callee restores them before returning to caller

43 C calling convention: Register Usage Callersaved Return value Arguments %rax %rdi %rsi %rdx %rcx %r8 %r9 %r10 %r11 Callee can directly use these registers Callee-saved %rbx %r12 %r13 %r14 %rbp %rsp Caller can assume these registers are unchanged.

44 Example int add2(int a, int b) { return a + b; add2: leal ret (%rdi,%rsi), %eax int add3(int a, int b, int c) { int r = add2(a, b); r = r + c; return r; add3: pushq %rbx movl %edx, %ebx movl $0, %eax call add2 addl %ebx, %eax popq %rbx ret Registers First 6 Arguments: %rdi, %rsi, %rdx, %rcx, %r8, %9 Return value: %rax

45 Example int add2(int a, int b) { return a + b; add2: leal ret (%rdi,%rsi), %eax int add3(int a, int b, int c) { int r = add2(a, b); r = r + c; return r; add3: pushq %rbx # use %rbx to keep r movl %edx, %ebx movl $0, %eax call add2 addl %ebx, %eax popq %rbx # restore %rbx before ret ret Registers First 6 Arguments: %rdi, %rsi, %rdx, %rcx, %r8, %9 Return value: %rax

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

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon Carnegie Mellon Machine-Level Programming III: Procedures 15-213/18-213/14-513/15-513: Introduction to Computer Systems 7 th Lecture, September 18, 2018 Today Procedures Mechanisms Stack Structure Calling

More information

Machine-level Programs Procedure

Machine-level Programs Procedure Computer Systems Machine-level Programs Procedure Han, Hwansoo Mechanisms in Procedures Passing control To beginning of procedure code Back to return point Passing data Procedure arguments Return value

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides Mechanisms in Procedures Passing control

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture 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

More information

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

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing 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

More information

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

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

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

%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker Procedure Call Registers Return %rax %eax %r8 %r8d Arg 5 %rbx %ebx %r9 %r9d Arg 6 Arg 4 %rcx %ecx %r10 %r10d Arg 3 %rdx %edx %r11 %r11d Arg 2 %rsi %esi %r12 %r12d Arg 1 %rdi %edi %r13 %r13d ptr %esp %r14

More information

Machine- Level Representa2on: Procedure

Machine- Level Representa2on: Procedure Machine- Level Representa2on: Procedure CSCI 2021: Machine Architecture and Organiza2on Pen- Chung Yew Department Computer Science and Engineering University of Minnesota With Slides from Bryant, O Hallaron

More information

University of Washington

University of Washington Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp

More information

Lecture 4 CIS 341: COMPILERS

Lecture 4 CIS 341: COMPILERS Lecture 4 CIS 341: COMPILERS CIS 341 Announcements HW2: X86lite Available on the course web pages. Due: Weds. Feb. 7 th at midnight Pair-programming project Zdancewic CIS 341: Compilers 2 X86 Schematic

More information

Machine-Level Programming (2)

Machine-Level Programming (2) Machine-Level Programming (2) Yanqiao ZHU Introduction to Computer Systems Project Future (Fall 2017) Google Camp, Tongji University Outline Control Condition Codes Conditional Branches and Conditional

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent instructions and conventions 2 Function Call Problems (1) Calling and returning

More information

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University 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

More information

Function Calls and Stack

Function Calls and Stack Function Calls and Stack Philipp Koehn 16 April 2018 1 functions Another Example 2 C code with an undefined function int main(void) { int a = 2; int b = do_something(a); urn b; } This can be successfully

More information

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames 6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (Instruc. Pointer) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in

More information

The Stack & Procedures

The Stack & Procedures The Stack & Procedures CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/648/

More information

Procedures and the Call Stack

Procedures and the Call Stack Procedures and the Call Stack Topics Procedures Call stack Procedure/stack instructions Calling conventions Register-saving conventions Why Procedures? Why functions? Why methods? int contains_char(char*

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

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

Stack Frame Components. Using the Stack (4) Stack Structure. Updated Stack Structure. Caller Frame Arguments 7+ Return Addr Old %rbp Stack Frame Components Frame pointer %rbp Stack pointer Caller Frame rguments 7+ Return ddr Old %rbp Saved Registers + Local Variables rgument Build 1 Using the Stack (4) Stack Structure long call_incr()

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Intel x86

More information

Intel x86-64 and Y86-64 Instruction Set Architecture

Intel x86-64 and Y86-64 Instruction Set Architecture CSE 2421: Systems I Low-Level Programming and Computer Organization Intel x86-64 and Y86-64 Instruction Set Architecture Presentation J Read/Study: Bryant 3.1 3.5, 4.1 Gojko Babić 03-07-2018 Intel x86

More information

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE351 Spring 2018, Midterm Exam April 27, 2018 CSE351 Spring 2018, Midterm Exam April 27, 2018 Please do not turn the page until 11:30. Last Name: First Name: Student ID Number: Name of person to your left: Name of person to your right: Signature indicating:

More information

Data Representa/ons: IA32 + x86-64

Data Representa/ons: IA32 + x86-64 X86-64 Instruc/on Set Architecture Instructor: Sanjeev Se(a 1 Data Representa/ons: IA32 + x86-64 Sizes of C Objects (in Bytes) C Data Type Typical 32- bit Intel IA32 x86-64 unsigned 4 4 4 int 4 4 4 long

More information

Areas for growth: I love feedback

Areas for growth: I love feedback Assembly part 2 1 Areas for growth: I love feedback Speed, I will go slower. Clarity. I will take time to explain everything on the slides. Feedback. I give more Kahoot questions and explain each answer.

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

Machine/Assembler Language Putting It All Together

Machine/Assembler Language Putting It All Together COMP 40: Machine Structure and Assembly Language Programming Fall 2015 Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

More information

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth Registers Ray Seyfarth September 8, 2011 Outline 1 Register basics 2 Moving a constant into a register 3 Moving a value from memory into a register 4 Moving values from a register into memory 5 Moving

More information

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures Machine- Level Programming III: Switch Statements and IA32 Procedures CS 485: Systems Programming Fall 2015 Instructor: James Griffioen Adapted from slides by R. Bryant and D. O Hallaron (hjp://csapp.cs.cmu.edu/public/instructors.html)

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Machine Language CS 3330 Samira Khan

Machine Language CS 3330 Samira Khan Machine Language CS 3330 Samira Khan University of Virginia Feb 2, 2017 AGENDA Logistics Review of Abstractions Machine Language 2 Logistics Feedback Not clear Hard to hear Use microphone Good feedback

More information

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

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

Assembly Programming IV

Assembly Programming IV Assembly Programming IV CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis 1 Administrivia Homework 2 due

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Sungkyunkwan University

Sungkyunkwan University Switch statements IA 32 Procedures Stack Structure Calling Conventions Illustrations of Recursion & Pointers long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case

More information

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

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

More information

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

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack bottom.. Earlier Frames. Frame for calling function P. Increasing address CS 107 Stack "bottom" Earlier Frames Lecture 13: Assembly Part III Argument n Friday, November 10, 2017 Computer Systems Increasing address Argument 7 Frame for calling function P Fall 2017 Stanford University

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 12 CS24 MIDTERM Midterm format: 6 hour overall time limit, multiple sittings (If you are focused on midterm, clock should be running.) Open book

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Programming

More information

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

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

1 Number Representation(10 points)

1 Number Representation(10 points) Name: Sp15 Midterm Q1 1 Number Representation(10 points) 1 NUMBER REPRESENTATION(10 POINTS) Let x=0xe and y=0x7 be integers stored on a machine with a word size of 4bits. Show your work with the following

More information

x86 Programming I CSE 351 Winter

x86 Programming I CSE 351 Winter x86 Programming I CSE 351 Winter 2017 http://xkcd.com/409/ Administrivia Lab 2 released! Da bomb! Go to section! No Luis OH Later this week 2 Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

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

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code. Where We Are Source code if (b == 0) a = b; Low-level IR code Optimized Low-level IR code Assembly code cmp $0,%rcx cmovz %rax,%rdx Lexical, Syntax, and Semantic Analysis IR Generation Optimizations Assembly

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

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

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012 6.172 Performance Engineering of Software Systems SPEED LIMIT PER ORDER OF 6.172 LECTURE 3 C to Assembly I-Ting Angelina Lee September 13, 2012 2012 Charles E. Leiserson and I-Ting Angelina Lee 1 Bugs

More information

x64 Cheat Sheet Fall 2014

x64 Cheat Sheet Fall 2014 CS 33 Intro Computer Systems Doeppner x64 Cheat Sheet Fall 2014 1 x64 Registers x64 assembly code uses sixteen 64-bit registers. Additionally, the lower bytes of some of these registers may be accessed

More information

Lecture 7: More procedures; array access Computer Architecture and Systems Programming ( )

Lecture 7: More procedures; array access Computer Architecture and Systems Programming ( ) Systems Group Department of Computer Science ETH Zürich Lecture 7: More procedures; array access Computer Architecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 1 Last Time

More information

The Stack & Procedures

The Stack & Procedures The Stack & Procedures CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Homework 2 due

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Programming

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

The von Neumann Machine

The von Neumann Machine The von Neumann Machine 1 1945: John von Neumann Wrote a report on the stored program concept, known as the First Draft of a Report on EDVAC also Alan Turing Konrad Zuse Eckert & Mauchly The basic structure

More information

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

Buffer Overflows Many of the following slides are based on those from Complete Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron

More information

CS61 Section Solutions 3

CS61 Section Solutions 3 CS61 Section Solutions 3 (Week of 10/1-10/5) 1. Assembly Operand Specifiers 2. Condition Codes 3. Jumps 4. Control Flow Loops 5. Procedure Calls 1. Assembly Operand Specifiers Q1 Operand Value %eax 0x104

More information

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018 238P: Operating Systems Lecture 3: Calling conventions Anton Burtsev October, 2018 What does CPU do internally? (Remember Lecture 01 - Introduction?) CPU execution loop CPU repeatedly reads instructions

More information

Computer Organization: A Programmer's Perspective

Computer Organization: A Programmer's Perspective A Programmer's Perspective Instruction Set Architecture Gal A. Kaminka galk@cs.biu.ac.il Outline: CPU Design Background Instruction sets Logic design Sequential Implementation A simple, but not very fast

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 5 LAST TIME Began exploring x86-64 instruction set architecture 16 general-purpose registers Also: All registers are 64 bits wide rax-rdx are

More information

Do not turn the page until 5:10.

Do not turn the page until 5:10. University of Washington Computer Science & Engineering Autumn 2018 Instructor: Justin Hsia 2018-10-29 Last Name: First Name: Student ID Number: Name of person to your Left Right All work is my own. I

More information

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang Project 3 Questions CMSC 313 Lecture 12 How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP

More information

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

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014) CSE351 Autumn 2014 Midterm Exam (29 October 2014) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate will prove

More information

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

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook CS356: Discussion #15 Review for Final Exam Marco Paolieri (paolieri@usc.edu) Illustrations from CS:APP3e textbook Processor Organization Pipeline: Computing Throughput and Delay n 1 2 3 4 5 6 clock (ps)

More information

Assembly Programming IV

Assembly Programming IV Assembly Programming IV CSE 410 Winter 2017 Instructor: Justin Hsia Teaching Assistants: Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui The Data That Turned the World Upside Down The company

More information

x86-64 Programming III

x86-64 Programming III x86-64 Programming III CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan http://xkcd.com/1652/ Administrivia Homework 2 due Wednesday (7/11) Lab

More information

18-600: Recitation #4 Exploits (Attack Lab)

18-600: Recitation #4 Exploits (Attack Lab) 18-600: Recitation #4 Exploits (Attack Lab) September 19th, 2017 Announcements Some students have triggered the bomb multiple times Use breakpoints for explode_bomb() Attack lab will be released on Sep.

More information

C++ Named Return Value Optimization

C++ Named Return Value Optimization C++ Named Return Value Optimization post. Robert.Schneider@hotmail.de meetup.com/c-user-group-karlsruhe auto create() -> T { T castor /* initialize */; //... return castor; } void use() { T pollux = create();

More information

CSCI 2021: x86-64 Control Flow

CSCI 2021: x86-64 Control Flow CSCI 2021: x86-64 Control Flow Chris Kauffman Last Updated: Mon Mar 11 11:54:06 CDT 2019 1 Logistics Reading Bryant/O Hallaron Ch 3.6: Control Flow Ch 3.7: Procedure calls Goals Jumps and Control flow

More information

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT CS33 Intro to Computer Systems XI 1 Copyright 2017 Thomas

More information

18-600: Recitation #4 Exploits

18-600: Recitation #4 Exploits 18-600: Recitation #4 Exploits 20th September 2016 Agenda More x86-64 assembly Buffer Overflow Attack Return Oriented Programming Attack 3 Recap: x86-64: Register Conventions Arguments passed in registers:

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 7 LAST TIME Dynamic memory allocation and the heap: A run-time facility that satisfies multiple needs: Programs can use widely varying, possibly

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 4 LAST TIME Enhanced our processor design in several ways Added branching support Allows programs where work is proportional to the input values

More information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Compiling C Control Flow 1 8: Compiling C

More information

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017 143A: Principles of Operating Systems Lecture 5: Calling conventions Anton Burtsev January, 2017 Stack and procedure calls Stack Main purpose: Store the return address for the current procedure Caller

More information

University*of*Washington*

University*of*Washington* Roadmap* C:* car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly* language:* Machine* code:* Computer* system:* get_mpg: pushq movq... popq %rbp %rsp,

More information

Computer Architecture and System Programming Laboratory. TA Session 3

Computer Architecture and System Programming Laboratory. TA Session 3 Computer Architecture and System Programming Laboratory TA Session 3 Stack - LIFO word-size data structure STACK is temporary storage memory area register points on top of stack (by default, it is highest

More information

Lecture 4: x86_64 Assembly Language

Lecture 4: x86_64 Assembly Language CSCI-GA.1144-001 PAC II Lecture 4: x86_64 Assembly Language Some slides adapted (and slightly modified) from: Randy Bryant Dave O Hallaron Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The eamples and discussion in the following slides have been adapted from a variet of sources, including: Chapter 3 of Computer Sstems 3 nd Edition b Brant and O'Hallaron 86 Assembl/GAS

More information

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly II: Control Flow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Processor State (x86-64) RAX 63 31 EAX 0 RBX EBX RCX RDX ECX EDX General-purpose

More information

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017 143A: Principles of Operating Systems Lecture 4: Calling conventions Anton Burtsev October, 2017 Recap from last time Stack and procedure calls What is stack? Stack It's just a region of memory Pointed

More information

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CSE 351 Midterm Exam Spring 2016 May 2, 2015 Name: CSE 351 Midterm Exam Spring 2016 May 2, 2015 UWNetID: Solution Please do not turn the page until 11:30. Instructions The exam is closed book, closed notes (no calculators, no mobile phones, no laptops,

More information

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

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly I: Basic Operations Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Basic Execution Environment RAX RBX RCX RDX RSI RDI RBP RSP R8 R9 R10

More information

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016 CS 31: Intro to Systems Functions and the Stack Martin Gagne Swarthmore College February 23, 2016 Reminders Late policy: you do not have to send me an email to inform me of a late submission before the

More information

Function Call Convention

Function Call Convention Function Call Convention Compass Security Schweiz AG Werkstrasse 20 Postfach 2038 CH-8645 Jona Tel +41 55 214 41 60 Fax +41 55 214 41 61 team@csnc.ch www.csnc.ch Content Intel Architecture Memory Layout

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

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

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

2 Systems Group Department of Computer Science ETH Zürich. Argument Build 3. %r8d %rax. %r9d %rbx. Argument #6 %rcx. %r10d %rcx. Static chain ptr %rdx

2 Systems Group Department of Computer Science ETH Zürich. Argument Build 3. %r8d %rax. %r9d %rbx. Argument #6 %rcx. %r10d %rcx. Static chain ptr %rdx Last Time Lecture 7: More procedures; array access Computer rchitecture and Systems Programming (252-0061-00) Timothy Roscoe Herbstsemester 2012 For loops - goto version goto jump to middle version Jump

More information

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

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective void P() { y = Q(x); print(y); return;... int Q(int t) { int v[10]; return v[t]; Computer Systems: A Programmer s Perspective %rax %rbx 0x101 0x41 0x7FFFFA8 0x1 0x7FFFFF8 0xB5A9 0x7FFFFF0 0x789ABC 0x7FFFFE8

More information

Machine-Level Programming I: Basics

Machine-Level Programming I: Basics Machine-Level Programming I: Basics CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN Slides adapted from Bryant & O Hallaron s slides 1 Today: Machine Programming I: Basics History

More information

COMP 210 Example Question Exam 2 (Solutions at the bottom)

COMP 210 Example Question Exam 2 (Solutions at the bottom) _ Problem 1. COMP 210 Example Question Exam 2 (Solutions at the bottom) This question will test your ability to reconstruct C code from the assembled output. On the opposing page, there is asm code for

More information

Computer Systems C S Cynthia Lee

Computer Systems C S Cynthia Lee Computer Systems C S 1 0 7 Cynthia Lee 2 Today s Topics Function call and return in x86-64 Registers Call stack NEXT TIME: NEW topic: the build process Taking a look at each step of the process Preprocessor,

More information

Assembly II: Control Flow

Assembly II: Control Flow Assembly II: Control Flow Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)

More information

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun http://xkcd.com/409/

More information

x86-64 Programming III & The Stack

x86-64 Programming III & The Stack x86-64 Programming III & The Stack CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/1652/ Administrative

More information

The Hardware/Software Interface CSE351 Spring 2013

The Hardware/Software Interface CSE351 Spring 2013 The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II 2 Today s Topics: control flow Condition codes Conditional and unconditional branches Loops 3 Conditionals and Control Flow A conditional

More information

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11 X86 Debug Computer Systems Section 3.11 GDB is a Source Level debugger We have learned how to debug at the C level But the machine is executing X86 object code! How does GDB play the shell game? Makes

More information

Instruction Set Architectures

Instruction Set Architectures Instruction Set Architectures! ISAs! Brief history of processors and architectures! C, assembly, machine code! Assembly basics: registers, operands, move instructions 1 What should the HW/SW interface

More information

Lecture 3 CIS 341: COMPILERS

Lecture 3 CIS 341: COMPILERS Lecture 3 CIS 341: COMPILERS HW01: Hellocaml! Announcements is due tomorrow tonight at 11:59:59pm. HW02: X86lite Will be available soon look for an announcement on Piazza Pair-programming project Simulator

More information