Carnegie Mellon Ithaca College

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
Controlling Program Flow. – 2 – Control Flow Computers execute instructions in sequence. Except when we change the flow of control Jump and Call instructions.
תרגול 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.
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.
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 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming IV Control Comp 21000: Introduction.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Samira Khan University of Virginia Feb 2, 2017
Conditional Branch Example
Computer Architecture
Assembly Programming III CSE 410 Winter 2017
Assembly Programming III CSE 351 Spring 2017
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II: Control
Machine-Level Representation of Programs II
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming II: Control Flow
x86 Programming II CSE 351 Autumn 2016
x86-64 Programming II CSE 351 Autumn 2017
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Controlling Program Flow
x86-64 Programming II CSE 351 Winter 2018
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Programming III CSE 351 Spring 2017
Instructor: David Ferry
Processor Architecture: The Y86-64 Instruction Set Architecture
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
3.5 Conditional Codes Spring, 2015 Euiseong Seo
Machine-Level Programming 2 Control Flow
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Machine-Level Programming 2 Control Flow
Processor Architecture: The Y86-64 Instruction Set Architecture
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Carnegie Mellon Ithaca College
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming II CSE 351 Summer 2018
x86-64 Programming III CSE 351 Autumn 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming II CSE 351 Autumn 2018
Machine-Level Programming II: Control Flow
EECE.3170 Microprocessor Systems Design I
X86 Assembly - Control.
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Control Flow Sept. 12, 2007
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Carnegie Mellon Ithaca College
Credits and Disclaimers
Presentation transcript:

Carnegie Mellon Ithaca College Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3*

Today Control: Condition codes Conditional branches Loops Ithaca College Today Control: Condition codes Conditional branches If-then-else statements Conditional moves Loops Switch Statements

Using Conditional Moves Ithaca College 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 C Code val = Test ? Then_Expr : Else_Expr; Goto Version result = Then_Expr; eval = Else_Expr; nt = !Test; if (nt) result = eval; return result;

Conditional Move cmovX Instructions Ithaca College Conditional Move * source/destination may be 16, 32, or 64 bits (not 8). No size suffix: assembler infers the operand length based on destination register cmovX Instructions Jump to different part of code depending on condition codes jX Synonym Condition Description cmove S*,R* cmovz ZF Equal / Zero cmovne S,R cmovnz ~ZF Not Equal / Not Zero cmovs S,R SF Negative cmovns S,R ~SF Nonnegative cmovg S,R cmovnle ~(SF^OF)&~ZF Greater (Signed) cmovge S,R cmovnl ~(SF^OF) Greater or Equal (Signed) cmovl S,R cmovnge (SF^OF) Less (Signed) cmovle S,R cmovng (SF^OF)|ZF Less or Equal (Signed) cmova S,R cmovnbe ~CF&~ZF Above (unsigned) cmovae S,R cmovnb ~CF Above or equal (unsigned) cmovb S,R cmovnae CF Below (unsigned) cmovbe S,R cmovna CF | ZF Below or equal (unsigned)

Conditional Move Example Ithaca College Conditional Move Example long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx # y subq %rdi, %rdx # eval = y-x cmpq %rsi, %rdi # x:y cmovle %rdx, %rax # if <=, result = eval ret

Bad Cases for Conditional Move Ithaca College Bad Cases for Conditional Move Expensive Computations val = Test(x) ? Hard1(x) : Hard2(x); Both values get computed Only makes sense when computations are very simple In general, gcc only uses conditional moves when the two expressions can be computed very easily, e.g., single instructions. Risky Computations val = p ? *p : 0; Both values get computed May have undesirable effects (always dereference p but it may be null!) Computations with side effects val = x > 0 ? x*=7 : x+=3; Both values get computed Must be side-effect free

Practice Problem Generation Register Use(s) %rdi Argument x %rsi Ithaca College Practice Problem Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Generation test: leaq 0(,%rdi,8), %rax testq %rsi, %rsi jle .L4 movq %rsi, %rax subq %rdi, %rax movq %rdi, %rdx andq %rsi, %rdx cmpq %rsi, %rdi cmovge %rdx, %rax ret .L4: addq %rsi, %rdi cmpq %-2, %rsi cmovle %rdi, %rax long test (long x, long y) { long val = _________; if (_________){ if (__________){ val = ________; else } else if (________) return val; } test %rsi, %rsi Does %rsi & %rsi long val = 8*x; if (y > 0) { if (x < y) val = y - x; else val = x & y; }else if (y <= -2) val = x*y; return val;

Practice Problem Generation Register Use(s) %rdi Argument x %rsi Ithaca College Practice Problem Register Use(s) %rdi Argument x %rsi Argument y %rax Return value Generation test: leaq 0(,%rdi,8), %rax testq %rsi, %rsi jle .L4 movq %rsi, %rax subq %rdi, %rax movq %rdi, %rdx andq %rsi, %rdx cmpq %rsi, %rdi cmovge %rdx, %rax ret .L4: # x <= y addq %rsi, %rdi cmpq %-2, %rsi cmovle %rdi, %rax long test (long x, long y) { long val = 8*x; if (y > 0){ if (x < y){ val = y - x; else val = x & y; } else if (y <= -2) val = x + y; return val; } long val = 8*x; if (y > 0) { if (x < y) val = y - x; else val = x & y; }else if (y <= -2) val = x + y; return val;

Condition Codes (Explicit Setting: Test) Ithaca College 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 CF set to 0 ZF set when a&b == 0 SF set when a&b < 0 OF set to 0 Note: typically the same operand is repeated to test whether it is negative, zero, or positive: testl %rax, %rax sets the condition codes depending on the value in %rax Note 2: there are also testl, testw and testb instructions.

Reading Condition Codes Ithaca College Reading Condition Codes SetX Instructions Set low-order byte of destination to 0 or 1 based on combinations of condition codes Does not alter remaining 7 bytes SetX Condition Description sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero sets SF Negative setns ~SF Nonnegative 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 %r8 %rbx %r9 %rcx %r10 %rdx %r11 %rsi %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

Reading condition codes Consider: setl D (SF^OF) Less (Signed <) D  (SF^OF) First compare two numbers, a and b where both are in 2’s complement form using an arithmetic, logical, test or cmp instruction Then use setX to set a register with the result of the test cmpq %rax, %rdx setl %al puts the result in byte register %al

Reading condition codes (cont) Assume a is in %rax and b in %rdx cmpq %rax, %rdx setl %al puts the result in byte register %al First instruction sets the condition code. cmpq computes b – a If b < a then b – a < 0 If there is no overflow, this is indicated by SF If there is positive overflow (b – a is large), we have b – a < 0 but OF is set If there is negative overflow (b – a is very small), we have b – a > 0 but OF is set In either case the sign flag will indicate the opposite of the sign of the true difference. Hence, use exclusive-or of the OF and SF Second instruction sets the %al register to 00000000 or 00000001 depending on the value of (SF^OF) setl D ; D  (SF^OF)

Reading condition codes (cont) Example: assume %rax holds 20 and %rdx holds 50 cmpq %rax, %rdx 50 – 20, SF  0, OF  0 setl %al %al  0 ^ 0 = 00000000 Example: assume %rax holds 0x8000001 and %rdx holds 20 20 – (-2147483647) , SF  1, OF  1 %al  1 ^ 1 = 00000000 setq gives false; 20 is not less than -2147483647 setl D ; D  (SF^OF)

Reading condition codes (cont) Example: assume %rax holds 20 and %rdx holds 0x8000001 cmpq %rax, %rdx (-2147483647) - 20 , SF  0, OF  1 0x80000001 + 0xFFFFFFEC = 0x7FFFFFED (note that 7 = 0111) setl %al %al  0 ^ 1 = 00000001 setq gives true; -2147483647 is less than 20

If this is zero, can’t be > Setg Greater (Signed) ~(SF^OF)&~ZF ~SF&~OF If this is zero, can’t be > First do an arith, logical, cmp or test. Then use the flags If the overflow flag is set, can’t be > if we did a cmpl or testl as the previous instruction. Why? CF ZF SF OF Condition codes

Reading Condition Codes (Cont.) Ithaca College Reading Condition Codes (Cont.) SetX Instructions: Set single byte based on combination of condition codes One of addressable byte registers Does not alter remaining bytes Typically use movzbl to finish job 32-bit instructions also set upper 32 bits to 0 Register Use(s) %rdi Argument x %rsi Argument y %rax Return value int gt (long x, long y) { return x > y; } cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %rax # Zero rest of %rax ret

Reading Condition Codes (Cont.) Ithaca College Reading Condition Codes (Cont.) Register Use(s) %rdi Argument x %rsi Argument y %rax Return value %rax %ah %al %rdx %dh %dl All 0’s: 0000000000000000000000000 %rcx %ch %cl %rbx %bh %bl Either 00000000 or 00000001 %rsi %rdi int gt (long x, long y) { return x > y; } %rsp %rbp cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %rax # Zero rest of %rax ret

Summarizing C Control Assembler Control Standard Techniques Ithaca College Summarizing C Control if-then-else do-while while, for switch Assembler Control Conditional jump Conditional move Indirect jump (via jump tables) Compiler generates code sequence to implement more complex control Standard Techniques Loops converted to do-while or jump-to-middle form Large switch statements use jump tables Sparse switch statements may use decision trees (if-elseif-elseif-else) long val = 8*x; if (y > 0) { if (x < y) val = y - x; else val = x & y; }else if (y <= -2) val = x*y; return val;

Summary Today Next Time Control: Condition codes Ithaca College Summary Today Control: Condition codes Conditional branches & conditional moves Next Time Loops Switch statements Stack Call / return Procedure call discipline