CS367. Program Control

Similar documents
Machine-Level Programming II: Control

Machine-Level Programming II: Control

Outline. Review: Assembly/Machine Code View. Processor State (x86-64, Par2al) Condi2on Codes (Explicit Se^ng: Compare) Condi2on Codes (Implicit Se^ng)

CS429: Computer Organization and Architecture

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

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

Machine-Level Programming II: Control

Machine Level Programming: Control

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

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

Assembly II: Control Flow

Machine-Level Programming (2)

x86 Programming III CSE 351 Autumn 2016 Instructor: Justin Hsia

x86 Programming II CSE 351 Winter

x86-64 Programming II

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

The Hardware/Software Interface CSE351 Spring 2013

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

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

Assembly Programming III

Machine- Level Programming II: Arithme6c & Control

x86 64 Programming II

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Machine-Level Programming II: Control Flow

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

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Controlling Program Flow

Machine Level Programming II: Arithmetic &Control

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

x86-64 Programming III & The Stack

Assembly Programming IV

Credits to Randy Bryant & Dave O Hallaron

x86-64 Programming III

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

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

Sungkyunkwan University

Machine- Level Representa2on: Procedure

CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. "Used to detect unsigned overflow

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

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

Giving credit where credit is due

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008

Page # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.)

Machine-Level Programming II: Control and Arithmetic

CS241 Computer Organization Spring Addresses & Pointers

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

Assembly Programming IV

Foundations of Computer Systems

Machine- level Programming II: Control Flow

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

Page 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.)

Compiling C Programs Into X86-64 Assembly Programs

Machine Programming 2: Control flow

Chapter 3 Machine-Level Programming II Control Flow

Systems Programming and Computer Architecture ( )

x64 Cheat Sheet Fall 2014

Machine Language CS 3330 Samira Khan

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

Intel x86-64 and Y86-64 Instruction Set Architecture

Machine- Level Programming II: Arithme6c & Control

Machine Representa/on of Programs: Control Flow cont d. Previous lecture. Do- While loop. While- Do loop CS Instructors: Sanjeev Se(a

Sungkyunkwan University

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSC 252: Computer Organization Spring 2018: Lecture 6

Machine- Level Programming II: Arithme c & Control

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

Assembly Language II: Addressing Modes & Control Flow

Three Kinds of Instruc;ons

The Hardware/Software Interface CSE351 Spring 2015

System Programming and Computer Architecture (Fall 2009)

Lecture 4: x86_64 Assembly Language

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

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

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

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

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

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

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

Areas for growth: I love feedback

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

Data Representa/ons: IA32 + x86-64

Machine- Level Programming III: Switch Statements and IA32 Procedures

Foundations of Computer Systems

Credits and Disclaimers

Review addressing modes

How Software Executes

Computer Organization: A Programmer's Perspective

CSCI 2021: x86-64 Control Flow

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

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

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

CSE 351 Midterm Exam

Process Layout and Function Calls

Do not turn the page until 5:10.

Branching and Looping

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Recitation #6. Architecture Lab

Lecture 3 CIS 341: COMPILERS

Transcription:

CS367 Program Control

outline program control condition codes branching looping conditional moves switches (special case of branching)

Condition Codes

Processor State (x86-64, Partial) Info about currently executing program Temporary data ( %rax, ) Location of runtime stack ( %rsp ) Location of next instruction ( %rip, ) Status of recent tests (CF,ZF,SF,OF) Current stack top Registers %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp %rip %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 Instruc7on pointer next instruc7on CF ZF SF OF Condi7on codes

Condition Codes (Implicit Setting) Single bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addq Src,Dest t = a+b ZF set if t==0 CF set if carry out (unsigned overflow) SF set if t<0 (as signed) OF set if two s-complement (signed) overflow (a>0 && b>0 && t<0) (a<0 && b<0 && t>=0) Not set by leaq instruction

Condition Codes (Explicit Setting: Compare) Explicit Setting by Compare Instruction cmpq Src2, Src1 cmpq b, a (like computing (a-b) without setting destination) CF set if carryout (unsigned) ZF set if a==b SF set if (a-b)<0 (as signed) OF set if (signed) overflow (a>0 && b<0 && (a-b)<0) (a<0 && b>0 && (a-b)>0)

Condition Codes (Explicit Setting: Test) Explicit Setting by Test instruction testq Src2, Src1 testq b, a like computing a&b without setting destination Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask ZF set when a&b == 0 SF set when a&b < 0

Reading Condition Codes SetX Instructions Set low-order byte of destination to 0 or 1 based on combinations of condition codes Doesn't alter other (7) bytes SetX Condi7on Descrip7on sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero sets SF Nega7ve setns ~SF Nonnega7ve setg ~(SF^OF)&~ZF Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) setl (SF^OF) Less (Signed) setle (SF^OF) ZF Less or Equal (Signed) seta ~CF&~ZF Above (unsigned) setb CF Below (unsigned)

x86-64 Integer Registers %rax %al %r8 %r8b %rbx %bl %r9 %r9b %rcx %cl %r10 %r10b %rdx %dl %r11 %r11b %rsi %sil %r12 %r12b %rdi %dil %r13 %r13b %rsp %spl %r14 %r14b %rbp %bpl %r15 %r15b Can reference low-order byte: sometimes 'l' for lower, or 'b' for byte

Reading Condition Codes (Cont.) Typically use movzbl to finish job "move, with zero-extending, a byte to a long-word." also zeroes out upper 32-bit others: movzbq (same as movzbl!), movsbl/movsbq (sign-extend) int gt (long x, long y) { return x > y; Register %rdi %rsi %rax Use(s) Argument x Argument y Return value cmpq %rsi, %rdi # Compare x:y. note ordering! setg %al # record whether > relation holds. movzbl %al, %eax # Zero rest of %rax (l isn't a typo!) ret

practice: condition codes (on pytania)

Branching

Jumping j<rel> instructions Jump to different part of code depending on condition codes jx Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF) ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned)

Conditional Branch Example long biggest (long x, long y) { long result = x; if (y > x) result = y; return result; biggest: movq %rdi, %rax # result = x cmpq %rsi, %rdi # x:y jg.l4 # jump when x>y movq %rsi, %rax # y>x, update result.l4: #!(y>x) ret jg: "jump when last comparison was a greater-than". branch occurs with opposite condition ( ) Register %rdi %rsi %rax Use(s) Argument x Argument y Return value

Converting C branching to assembly if-else test condition, branch or fall through accordingly. may need extra labels/jumps to go to correct place afterwards. goto direct translation; jmp (unconditionally) to given label. usually guarded by if-statement anyways. switch statements more complicated either "decision tree" of condition-checkbranches, or use of a jump table.

if-statements C if (expr) { thenstmt else { elsestmt assembly <test expr> j<rel> nope # when!expr thenstmt jmp done nope: elsestmt done: if (x==y) { z = 100; else { z = 0; cmpq %rsi, %rdi # x:y jne nope movq $100,%rax jmp done nope: movq $0, %rax done:...

goto-statements they don't even have to be inside an if, but that's the most common. if (x==y) { goto outer; C if (expr) { goto place... place:... assembly <test expr> j<rel> nope #when!expr jmp X nope:... place:... cmpq %rsi, %rdi # x:y jne nope jmp outer nope:...

practice: branching (on pytania)

Loops

converting loop statements while loops: similar to if-statement, but jumps to previous label. must guard against initial iteration we first jump to the test. do-while loops: same as while, w/o guarding against 1 st iteration. for loops: logically convert to while loop.

do-while loops C assembly do { y+=10; x++; while(x<5); y*=100; do { body while (expr); nextstmt loop: body test: <test expr> j<rel> loop #when test done: nextstmt loop: addq $10, %rsi # y incq %rdi # x test: cmpq $5, %rdi # x:5 jl loop done: imulq $100, %rsi

while loops C while (expr) { body nextstmt assembly jmp test loop: body test: <test expr> j<rel> loop #when test done: nextstmt while(x<5) { y+=10; x++; y*=100; jmp test loop: addq $10, %rsi # y incq %rdi # x test: cmpq $5, %rdi # x:5 jl loop done: imulq $100, %rsi

for loops convert for-loops to while-loops, and then to assembly. C-for for (init; guard; update) { body C-while init; while (guard){ body update

for-loop example C-for, C-while for (int i=0; i<10; i++) { x *= 2; ========================== int i=0; while (i<10){ x *= 2; i++; assembly movq $0, %rsi jmp test loop: shlq $1, %rdi # x*=2 incq %rsi # i++ test: cmpq $10, %rsi # i : 10 jl loop # if i<10 done:

practice: loops (on pytania)

Conditional Moves

conditional moves branching has a cost: processor wants to pipeline many instructions at once, but can't look beyond the next branch. common operation: x = guard? expr1 : expr2 ; usage: x = expr2 test guard cmov<rel> expr1 (examples: cmovge, cmovl, cmovne, etc.)

example conditional move C (if-expr and if-stmt versions) z = x<y? 100 : 0; ========================== /* maybe written this way */ if (x<y) z = 100; else z = 0; assembly (new vs old) movq $0, %rax cmpq %rsi, %rdi # x:y cmovl $100, %rax ========old approach======== cmpq %rsi, %rdi # x:y jl thenlabel movq $0, %rax jmp donelabel thenlabel: movq $100, %rax donelabel:

Using Conditional Moves Conditional Move Instructions Instruction supports: if (Test) Dest ß Src Supported in post-1995 x86 processors GCC tries to use them But, only when known to be safe Why? Branches are very disruptive to instruction flow through pipelines Conditional moves do not require control transfer

Bad Cases for Conditional Move Expensive Computations val = guard? hard1(x) : hard2(x); lots of extra work! Risky Computations val = p? *p : 0; follows bad pointers! Side-effectful computations val = x>0? x*=7 : x+=3; x modified both ways!

Switches

Sparse Switch Example /* Return x/111 if x is multiple && <= 999. -1 otherwise */ int div111(int x) { switch(x) { case 0: return 0; case 111: return 1; case 222: return 2; case 333: return 3; case 444: return 4; case 555: return 5; case 666: return 6; case 777: return 7; case 888: return 8; case 999: return 9; default: return -1; Sparse cases (case values are always known.) order them into tree of conditions Example Not practical to use jump table Would require 1000 entries most would be empty Obvious translation into if-then-else would have up to 9 tests

Sparse Switch Code Structure < 444 = > 4 111 L8 777 < = > 1 0 222 555 888 = = = = > -1 0 2 5 8 L17 L4 333 666 999 L14 L5-1 = 7 3-1 6-1 Organizes cases as binary tree Logarithmic performance < L16 = = > = 9 movl 8(%ebp),%eax # get x cmpl $444,%eax # x:444 je L8 jg L16 cmpl $111,%eax # x:111 je L5 jg L17 testl %eax,%eax # x:0 je L4 jmp L14... L5: movl $1,%eax jmp L19 L6: movl $2,%eax jmp L19 L7: movl $3,%eax jmp L19 L8: movl $4,%eax jmp L19...

converting switch statements dense case values all case values are basically consecutive. create array of places to jump to, and rework switch value to become index into that array. jmp *table will index into, get value, and jump there in one step.

Dense Switch Statement Example Multiple-case labels Here: 5 & 6 Fall through cases Here: 2 Missing cases Here: 4 long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; return w;

Jump Table Structure Switch Form switch(x) { case val_0: Block 0 case val_1: Block 1... case val_n-1: Block n 1 Transla7on (Extended C) goto *JTab[x]; jtab: Jump Table Targ0 Targ1 Targ2... Targn-1 Targ0: Targ1: Targ2: Targn-1: Jump Targets Code Block 0 Code Block 1 Code Block 2... Code Block n 1

Switch Statement Example long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... return w; switch_eg: movq %rdx, %rcx cmpq $6, %rdi # x:6 ja.l8 # use default jmp *.L4(,%rdi,8) Register %rdi %rsi %rdx %rax Use(s) Argument x Argument y Argument z Return value What range of values takes default? Note that w isn't ini7alized here.

Indirect jump Switch Statement Example long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... return w; Jump table switch_eg: movq %rdx, %rcx cmpq $6, %rdi # x:6 ja.l8 # Use default jmp *.L4(,%rdi,8) # goto *JTab[x].section.rodata.align 8.L4:.quad.L8 # x = 0.quad.L3 # x = 1.quad.L5 # x = 2.quad.L9 # x = 3.quad.L8 # x = 4.quad.L7 # x = 5.quad.L7 # x = 6

Assembly Setup Explanation Table Structure Each target requires 8 bytes Base address at.l4 Jumping Direct: jmp.l8 Jump target is denoted by label.l8 Indirect: jmp *.L4(,%rdi,8) Start of jump table:.l4 Must scale by factor of 8 (addresses are 8 bytes) Fetch target from effective Address.L4 + x*8 Only for 0 x 6 Jump table.section.rodata.align 8.L4:.quad.L8 # x = 0.quad.L3 # x = 1.quad.L5 # x = 2.quad.L9 # x = 3.quad.L8 # x = 4.quad.L7 # x = 5.quad.L7 # x = 6

Jump Table switch(x) { case 1: //.L3 w = y*z; Jump table.section.rodata.align 8.L4:.quad.L8 # x = 0.quad.L3 # x = 1.quad.L5 # x = 2.quad.L9 # x = 3.quad.L8 # x = 4.quad.L7 # x = 5.quad.L7 # x = 6 break; case 2: //.L5 w = y/z; /* Fall Through */ case 3: //.L9 w += z; break; case 5: case 6: //.L7 w -= z; break; default: //.L8 w = 2;

Code Blocks (x == 1) switch(x) { case 1: //.L3 w = y*z; break;....l3: movq imulq ret %rsi, %rax # y %rdx, %rax # y*z Register %rdi %rsi %rdx %rax Use(s) Argument x Argument y Argument z Return value

Handling Fall-Through long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;... case 2: w = y/z; goto merge; case 3: merge: w = 1; w += z;

Code Blocks (x == 2, x == 3) long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;....l5: # Case 2 movq %rsi, %rax cqto idivq %rcx # y/z jmp.l6 # goto merge.l9: # Case 3 movl $1, %eax # w = 1.L6: # merge: addq %rcx, %rax # w += z ret Register %rdi %rsi %rdx %rax Use(s) Argument x Argument y Argument z Return value

Code Blocks (x == 5, x == 6, default) switch(x) {... case 5: //.L7 case 6: //.L7 w -= z; break; default: //.L8 w = 2;.L7: # Case 5,6 movl $1, %eax # w = 1 subq %rdx, %rax # w -= z ret.l8: movl $2, %eax # 2 ret Register %rdi %rsi %rdx %rax # Default: Use(s) Argument x Argument y Argument z Return value