1

1 2/14/2020 Reading Condition Codes x86-64 Integer Registers %rax - PDF document

2/14/2020 These Slides Control: Condition codes Conditional branches Machine-Level Programming II: Control Loops Switch Statements CSci 2021: Machine Architecture and Organization February 17th-19th, 2020 Your instructor: Stephen


  1. 2/14/2020 These Slides  Control: Condition codes  Conditional branches Machine-Level Programming II: Control  Loops  Switch Statements CSci 2021: Machine Architecture and Organization February 17th-19th, 2020 Your instructor: Stephen McCamant Based on slides originally by: Randy Bryant, Dave O’Hallaron 1 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Processor State (x86-64, Partial) Condition Codes (Implicit Setting)  Information about  Single bit registers currently executing  CF Carry Flag (for unsigned) SF Sign Flag (for signed) Registers program  ZF Zero Flag OF Overflow Flag (for signed) %rax %r8  Temporary data %rbx %r9 ( %rax , … ) %rcx %r10  Implicitly set (think of it as side effect) by arithmetic operations  Location of runtime stack %rdx %r11 Example: addq Src , Dest ↔ t = a+b ( %rsp ) %rsi %r12 CF set if carry out from most significant bit (unsigned overflow)  Location of current code %rdi %r13 ZF set if t == 0 control point %rsp %r14 ( %rip , … ) SF set if t < 0 (as signed; i.e., copy of sign bit) %rbp %r15  Status of recent tests OF set if two’s -complement (signed) overflow ( CF, ZF, SF, OF ) (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) %rip Instruction pointer Current stack top Condition codes  Not set by leaq instruction CF ZF SF OF 3 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Condition Codes (Explicit Setting: Compare) Condition Codes (Explicit Setting: Test)  Explicit Setting by Compare Instruction  Explicit Setting by Test instruction  cmpq Src2 , Src1  testq Src2 , Src1  cmpq b,a like computing a-b without setting destination  testq b,a like computing a&b without setting destination  CF set if carry out from most significant bit (used for unsigned comparisons)  Sets condition codes based on value of Src1 & Src2  ZF set if a == b  Useful to have one of the operands be a mask  SF set if (a-b) < 0 (as signed)  OF set if two’s -complement (signed) overflow  ZF set when a&b == 0 (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)  SF set when a&b < 0 5 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1

  2. 2/14/2020 Reading Condition Codes x86-64 Integer Registers %rax %r8 %al %r8b  SetX Instructions  Set low-order byte to 0 or 1 based on condition codes %rbx %r9 %bl %r9b SetX Condition Description %rcx %r10 %cl %r10b sete ZF Equal / Zero setne ~ZF Not Equal / Not Zero %rdx %r11 %dl %r11b sets SF Negative (“Sign”) setns ~SF Nonnegative %rsi %r12 %sil %r12b setg ~(SF^OF)&~ZF Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) %rdi %r13 %dil %r13b setl (SF^OF) Less (Signed) setle (SF^OF)|ZF Less or Equal (Signed) %rsp %r14 %spl %r14b seta ~CF&~ZF Above (unsigned >) setae ~CF Above or equal (unsigned >=) %rbp %r15 %bpl %r15b setb CF Below (unsigned <)  Can reference low-order byte setbe CF|ZF Below or equal (unsigned <=) 7 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Reading Condition Codes (Cont.) Exercise Break: More Conditions  SetX Instructions:  Every condition can be negated by putting “n” in the  Set single byte based on combination of condition mnemonic, for “not” codes  We skipped some of these conditions in the previous table, because they  One of addressable byte registers were equivalent to others  Does not alter remaining bytes  Which other conditions are these equivalent to?  Typically use movzbl to finish job  32-bit instructions also set upper 32 bits to 0 Register Use(s) 1. setng : not greater than int gt (long x, long y) %rdi Argument x { return x > y; %rsi Argument y 2. setnbe : not below or equal } %rax Return value cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %eax # Zero rest of %rax ret 9 10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Equivalents of More Conditions Today  Intuition: cover three cases: <, =, >  Control: Condition codes  setng not greater than (signed)  Conditional branches  If not greater, than either less than or equal: setle  Loops  Check conditions:  Switch Statements  ~ ( ~ (SF ^ OF) & ~ ZF) = ~~ (SF ^ OF) | ~~ ZF = (SF ^ OF) | ZF ✓  setnbe not below or equal (unsigned)  If not below or equal, must be above: seta  Check conditions:  ~ (CF | ZF) = ~ CF & ~ ZF ✓ 11 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

  3. 2/14/2020 Jumping Conditional Branch Example (Old Style)  Generation  jX Instructions > gcc – Og -S – fno-if-conversion control.c  Jump to different part of code depending on condition codes absdiff: long absdiff cmpq %rsi, %rdi # x:y jX Condition Description (long x, long y) jle .L4 { movq %rdi, %rax jmp 1 Unconditional long result; subq %rsi, %rax je ZF Equal / Zero if (x > y) ret jne ~ZF Not Equal / Not Zero result = x-y; .L4: # x <= y js SF Negative else movq %rsi, %rax jns ~SF Nonnegative result = y-x; subq %rdi, %rax jg ~(SF^OF)&~ZF Greater (signed) return result; ret } jge ~(SF^OF) Greater or Equal (signed) jl (SF^OF) Less (signed) Register Use(s) jle (SF^OF)|ZF Less or Equal (signed) %rdi Argument x ja ~CF&~ZF Above (unsigned) %rsi Argument y jb CF Below (unsigned) %rax Return value 14 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition General Conditional Expression Translation Expressing with Goto Code (Using Branches)  C allows goto statement C Code  Jump to position designated by label val = Test ? Then_Expr : Else_Expr ; long absdiff long absdiff_j (long x, long y) (long x, long y) val = x>y ? x-y : y-x; { { long result; long result; if (x > y) int ntest = x <= y; Goto Version if (ntest) goto Else; result = x-y; ntest = ! Test ; else result = x-y;  Create separate code regions for if (ntest) goto Else; result = y-x; goto Done; then & else expressions val = Then_Expr ; Else: return result;  Execute appropriate one goto Done; } result = y-x; Else: Done: val = Else_Expr ; return result; Done: } . . . 16 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Using Conditional Moves Conditional Move Example  Conditional Move Instructions long absdiff (long x, long y)  Instruction supports: { Register Use(s) if (Test) Dest  Src long result; %rdi Argument x  Supported in post-1995 x86 processors if (x > y) C Code result = x-y; %rsi Argument y  GCC tries to use them val = Test else %rax Return value ? Then_Expr  But, only when known to be safe result = y-x; : Else_Expr ; return result;  Why? }  Branches are very disruptive to Goto Version instruction flow through pipelines absdiff: result = Then_Expr ; movq %rdi, %rax # x  Conditional moves do not require subq %rsi, %rax # result = x-y eval = Else_Expr ; control transfer movq %rsi, %rdx nt = ! Test ; subq %rdi, %rdx # eval = y-x if (nt) result = eval; cmpq %rsi, %rdi # x:y return result; cmovle %rdx, %rax # if <=, result = eval ret 18 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3

Recommend


More recommend