CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

Similar documents
CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

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

Assembly II: Control Flow

The Hardware/Software Interface CSE351 Spring 2013

Machine Language CS 3330 Samira Khan

Assembly Language II: Addressing Modes & Control Flow

System Programming and Computer Architecture (Fall 2009)

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

Machine-Level Programming II: Control

CS367. Program Control

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

x86-64 Programming II

CS429: Computer Organization and Architecture

CSCI 2021: x86-64 Control Flow

Machine-Level Programming II: Control

Machine Level Programming: Control

Controlling Program Flow

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Assembly Programming III

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

x86 Programming II CSE 351 Winter

last time Assembly part 2 / C part 1 condition codes reminder: quiz

CS61 Section Solutions 3

Machine Program: Procedure. Zhaoguo Wang

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Credits and Disclaimers

CS356 Unit 5. Translation to Assembly. Translating HLL to Assembly ASSEMBLY TRANSLATION EXAMPLE. x86 Control Flow

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

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

Review addressing modes

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

CS 3330 Exam 1 Fall 2017 Computing ID:

CS241 Computer Organization Spring Addresses & Pointers

Machine Programming 2: Control flow

Lab 3. The Art of Assembly Language (II)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

5.1. CS356 Unit 5. x86 Control Flow

Intel x86-64 and Y86-64 Instruction Set Architecture

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Machine-Level Programming II: Control Flow

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

How Software Executes

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Credits to Randy Bryant & Dave O Hallaron

CSC 252: Computer Organization Spring 2018: Lecture 6

Machine-Level Programming (2)

Machine/Assembler Language Putting It All Together

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

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

Second Part of the Course

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

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Areas for growth: I love feedback

Complex Instruction Set Computer (CISC)

Machine-Level Programming II: Control and Arithmetic

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Branching and Looping

CS241 Computer Organization Spring Loops & Arrays

Sungkyunkwan University

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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

Program Exploitation Intro

CPS104 Recitation: Assembly Programming

x86 64 Programming II

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Machine-Level Programming II: Control

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

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

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

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

Process Layout and Function Calls

Machine- Level Programming II: Arithme6c & Control

Chapter 3 Machine-Level Programming II Control Flow

Assembly Language: IA-32 Instructions

Credits and Disclaimers

Machine Level Programming II: Arithmetic &Control

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CS , Fall 2007 Exam 1

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

2/12/2016. Today. Machine-Level Programming II: Control. Condition Codes (Implicit Setting) Processor State (x86-64, Partial)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

CSC 8400: Computer Systems. Machine-Level Representation of Programs

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

Assembly Language: Part 2

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

Corrections made in this version not seen in first lecture:

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

Systems Programming and Computer Architecture ( )

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Compiler Construction D7011E

The Instruction Set. Chapter 5

Introduction to IA-32. Jo, Heeseung

Instruction Set Architecture

INTRODUCTION TO IA-32. Jo, Heeseung

Transcription:

CS 261 Fall 2018 Mike Lam, Professor x86-64 Control Flow

Topics Condition codes Jumps Conditional moves Jump tables

Motivation We cannot translate the following C function to assembly, using only data movement and arithmetic operations Fundamental requirement: ability to control the flow of program execution (i.e., decision-making) Necessary for translating structured code int min (int x, int y) { if (x < y) { return x; else { return y;

Control flow The program counter (PC) tracks the address of the next instruction to be executed To change the PC in assembly, use a jump instruction Often the jump will be relative to the current PC value In assembly, the target of a jump is usually a label, which is converted to a code address by the assembler Labels are written using colon notation However, unconditional jumps aren t sufficient for decisionmaking In fact, the compiler can just re-arrange code to avoid them L1: movl $2, %eax jmp L1 movl $3, %eax movl $4, %eax # never executed!

Control flow Conditional jumps only jump under certain conditions Compilers translate block-structured code to linear code using conditional jumps We can do this in C using the goto statement General template: "if (<cond>) goto <label>;" Syntax for labels is the same in C and assembly (colon notation) CS:APP: C goto code is code that uses only if/goto and goto No blocks (and therefore no else blocks or explicit loops) Not a good idea in general! Famous letter by Dijkstra: "Go To Statement Considered Harmful" However, it is useful for pedagogical purposes (closer to assembly)

Example C code: if (x < y) { printf("a"); else { printf("b"); printf("c"); note inverted condition! C goto code: L1: L2: if (x >= y) goto L1; printf("a"); goto L2; printf("b"); printf("c"); C code: while (x < 5) { x = x 1; C goto code: goto L2; L1: x = x 1; L2: if (x < 5) goto L1;

Conditional jumps In machine/assembly code, conditional jumps are often encoded using a pair of instructions The first sets the condition codes of the CPU On x86-64, the FLAGS register Arithmetic/logical instructions do this as a side effect Special-purpose instructions cmp and test The second makes a jump depending on the value of the condition codes On x86-64, many different variants: jump-if-equal, jump-ifless-than, etc.

Condition codes x86-64: special %flags register stores bits for these condition codes: CF (carry): last operation resulted in a carry out or borrow in (e.g, overflow for unsigned arithmetic) ZF (zero): last operation resulted in a zero SF (sign): last operation resulted in a negative value OF (overflow): last operation caused a two's complement overflow (negative or positive) As well as a few we won t use: PF (parity): last operation produced an even number of 1 bits AF (adjust): last operation resulted in a carry out for the four least significant bits IF (interrupt): CPU will handle interrupts Use $eflags to reference this register in GDB E.g., print $eflags or display $eflags

Condition codes Special cmp and test instructions cmp compares two values (computes arg 2 - arg 1 ) NOTE REVERSED ORDERING also, the result is not saved Type-agnostic: all flags are set, but not all are relevant! test checks bits (computes arg 2 & arg 1 ) Often, the arguments are the same (or one is a bit mask) cmpl %eax, %ecx testl $0xFF, %edx # means compare %ecx with %eax # means check low-order 8 bits of %edx

Jump instructions Type difference

Example C code: int min (int x, int y) { if (x < y) { return x; else { return y; x86-64 assembly: (x in %edi, y in %esi) min: cmpl %esi, %edi jge.l3 movl %edi, %eax ret.l3: movl %esi, %eax ret

Example C code: int min (int x, int y) { if (x < y) { return x; else { return y; x86-64 assembly: (x in %edi, y in %esi) min: y x cmpl %esi, %edi jge.l3 movl %edi, %eax ret.l3: movl %esi, %eax ret

Example C code: int min (int x, int y) { if (x < y) { return x; else { return y; x86-64 assembly: (x in %edi, y in %esi) min: cmpl %esi, %edi jge.l3 movl %edi, %eax ret.l3: movl %esi, %eax ret

Example C code: int min (int x, int y) { if (x < y) { return x; else { return y; x86-64 assembly: (x in %edi, y in %esi) min: cmpl %esi, %edi jge.l3 movl %edi, %eax ret.l3: movl %esi, %eax ret

Conditionals (in goto code) if (<test-expr>) <true-branch> else <false-branch> if (!<test-expr>) goto false; <true-branch> goto done; false: <false-branch> done: If/else if (<top-expr>) if (<test-expr>) <true-branch> else <false-branch> else <else-branch> if (!<top-expr>) goto else; if (!<test-expr>) goto false; <true-branch> goto done; false: <false-branch> done: goto end; else: <else-branch> end: Nested if/else

Conditionals (in goto code) if (<test-expr>) <true-branch> else <false-branch> if (!<test-expr>) goto false; <true-branch> goto done; false: <false-branch> done: If/else if (<top-expr>) if (<test-expr>) <true-branch> else <false-branch> else <else-branch> if (!<top-expr>) goto else; if (!<test-expr>) goto false; <true-branch> goto done; false: <false-branch> done: goto end; else: <else-branch> end: Nested if/else

Conditional moves Similar to conditional jumps, but they move data if certain condition codes are set Benefit: no branch prediction penalty We'll see how this produces faster code in a few weeks In C code: "x = ( <cond>? <tvalue> : <fvalue>)" cmpq %rax, %rbx jg L01 movq %rax, %rcx jmp L02 L01: movq %rbx, %rcx L02: movq %rax, %rcx cmpq %rax, %rbx cmovg %rbx, %rcx

Conditional moves Similar to conditional jumps, but they move data if certain condition codes are set Benefit: no branch prediction penalty We'll see how this produces faster code in a few weeks In C code: "x = ( <cond>? <tvalue> : <fvalue>)" cmpq %rax, %rbx jg L01 movq %rax, %rcx jmp L02 L01: movq %rbx, %rcx L02: movq %rax, %rcx cmpq %rax, %rbx cmovg %rbx, %rcx

Loops Basic idea: jump back to an earlier label Three basic forms: Do-while loops Jump-to-middle loops Guarded-do loops Note: we ll use goto code in C first Just to avoid unnecessary complication If you can translate a loop into goto code, it's then much easier to convert to assembly

Loops do while (<test-expr>); loop: if (<test-expr>) goto loop; Do-while loop while (<test-expr>) goto test; loop: test: if (<test-expr>) goto loop if (!<test-expr>) goto done loop: if (<test-expr>) goto loop done: Jump-tomiddle loop Guardeddo loop

Loops do while (<test-expr>); loop: if (<test-expr>) goto loop; Do-while loop while (<test-expr>) goto test; loop: test: if (<test-expr>) goto loop if (!<test-expr>) goto done loop: if (<test-expr>) goto loop done: Jump-tomiddle loop Guardeddo loop

Loops for (<init-expr>; <test-expr>; <update-expr>) goto test; loop: test: if (<test-expr>) goto loop if (!<test-expr>) goto done loop: if (<test-expr>) goto loop done: Jump-to-middle loop Guarded-do loop

Loops for (<init-expr>; <test-expr>; <update-expr>) <init-expr> goto test; loop: <update-expr> test: if (<test-expr>) goto loop <init-expr> if (!<test-expr>) goto done; loop: <update-expr> if (<test-expr>) goto loop done: Jump-to-middle loop Guarded-do loop

Switch statements One approach: convert to if/elseif code Problem: performance varies based on ordering and actual runtime values! switch (x) { case 10: do_blah(); break; case 11: do_foo(); break; case 13: do_bar(); break; case 15: do_baz(); break; default: error(); if (x == 10) { do_blah(); else if (x == 11) { do_foo(); else if (x == 13) { do_bar(); else if (x == 15) { do_baz(); else { error();

Switch statements Indexed indirect jump ("computed goto") Jump to an address stored in a register Implemented using a data structure called a jump table Efficient when # of options is high and the value range is small switch (x) { case 10: do_blah(); break; case 11: do_foo(); break; case 13: do_bar(); break; case 15: do_baz(); break; default: error(); Jump Table Index Destination 0 0x400e51 1 0x400e88 2 0x401900 3 0x400f12 4 0x401900 5 0x400f34 x is in %rdx, address of jump table is in %rbx subq $0xA, %rdx movq (%rbx,%rdx,0x8), %rcx jmp *%rcx

Related coursework We can always (and automatically!) translate from structured code to linear/goto code This is what a compiler does! If you re interested in learning more about how this works, plan to take CS 432 as your systems elective Structured Code (C, Java, etc.) Compiler Linear Code (x86-64, etc.)

Exercise Convert the following C function into x86-64 assembly: int sum = 0; int x = 1; while (x < 10) { sum = sum + x; x = x + 1;

Exercise Convert the following C function into x86-64 assembly: int sum = 0; int x = 1; while (x < 10) { sum = sum + x; x = x + 1; L1: L2: movl $0, %eax # sum = 0 movl $1, %edx # x = 1 jmp L2 # goto L2 addl %edx, %eax # sum = sum + x addl $1, %edx # x = x + 1 cmpl $10, %edx # if (x < 10) jl L1 # goto L1