Machine-Level Programming: Control Flow

Slides:



Advertisements
Similar presentations
University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Machine-Level Programming II: Control Flow Sept. 12, 2002 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
– 1 – Homework 1 Count 25 Mean 73.8 Std Min 22 Max 96 Mode 66 Median80.5.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
תרגול 5 תכנות באסמבלי, המשך
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
University of Washington x86 Programming II The Hardware/Software Interface CSE351 Winter 2013.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
Lecture 7 Flow of Control Topics Finish Lecture 6 slides Lab 02 = datalab comments Assembly Language flow of control Test 1 – a week from wednesday February.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
Machine-Level Programming II Control Flow Sept. 14, 2000 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Conditional Branch Example
Computer Architecture
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II Control Flow Sept. 9, 1999
Machine-Level Programming II: Control
Machine-Level Representation of Programs II
Ch. 2 Two’s Complement Boolean vs. Logical Floating Point
Machine-Level Programming II: Control Flow
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Instructor: David Ferry
Condition Codes Single Bit Registers
Comp Integers Spring 2015 Topics Numeric Encodings
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Machine-Level Representation of Programs II
Carnegie Mellon Ithaca College
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Credits and Disclaimers
Presentation transcript:

Machine-Level Programming: Control Flow Topics Condition codes If-then-else Do-While, While, For loops Switch statements

Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Implicitly Set By Arithmetic Operations addl Src,Dest C analog: t = a + b CF set if carry out from most significant bit Used to detect unsigned overflow ZF set if t == 0 SF set if t < 0 OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not Set by leal instruction

Setting Condition Codes Explicit Setting by Compare Instruction cmpl Src2,Src1 cmpl b,a like computing a-b without setting destination CF set if carry out from most significant bit Used for unsigned comparisons ZF set if a == b SF set if (a-b) < 0 OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

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

Reading Condition Codes

Jumping

“If” Translation C Code Goto Version int max(int x, int y){ if (x > y) return x; else return y; } Goto Version int goto_max(int x, int y){ int rval; int cond = (x <= y); if (cond) { rval = y; // false part goto done; } rval = x; // true part done: return rval;

“Do-While” Translation C Code do Body while (Test); Goto Version loop: Body if (Test) goto loop

“While” Translation C Code Goto Version while (Test) Body if (!Test) goto done; loop: Body if (Test) goto loop; done:

“For” Translation For Version While Version Do-While Version for (Init; Test; Update ) Body Init; while (Test ) { Body Update ; } Do-While Version Goto Version Init; if (!Test) goto done; do { Body Update ; } while (Test) done: Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done:

Switch Statements Implementation Options Series of conditionals typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op){ switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; } Implementation Options Series of conditionals Good if few cases Slow if many Jump Table Lookup branch target Avoids conditionals Possible when cases are small integer constants GCC Picks one based on case structure

Jump Table Structure Jump Targets Switch Form Jump Table Code Block Targ0: 1 Targ1: 2 Targ2: n–1 Targn-1: • switch(op) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1 } Targ0 Targ1 Targ2 Targn-1 • jt: Approx. Translation target = JTab[op]; goto *target;

Sparse Switch Example Not practical to use jump table /* 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; } Not practical to use jump table Would require 1000 entries Obvious translation into if-then-else would have max. of 9 tests

Sparse Switch Code Structure 444 111 777 222 555 888 333 666 999 < > = 4 < > < > = = 1 7 -1 < >  =  = =  = 2 5 8 -1  -1  -1  = = = 3 6 9 Organizes cases as binary tree Logarithmic performance

Sparse Switch Code Compares x to possible case values 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 . . . Compares x to possible case values Jumps different places depending on outcomes . . . L5: movl $1,%eax jmp L19 L6: movl $2,%eax L7: movl $3,%eax L8: movl $4,%eax

Summarizing C Control Assembler Control Compiler Standard Techniques if-then-else do-while while switch Assembler Control jump Conditional jump Compiler Must generate assembly code to implement more complex control (&&, ||) Standard Techniques All loops converted to do-while form Large switch statements use jump tables Conditions in CISC CISC machines generally have condition code registers Conditions in RISC Use general registers to store condition information Special comparison instructions E.g., on Alpha: cmple $16,1,$1 Sets register $1 to 1 when Register $16 <= 1