Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carnegie Mellon Ithaca College

Similar presentations


Presentation on theme: "Carnegie Mellon Ithaca College"— Presentation transcript:

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

2 Today Control: Condition codes Conditional branches Loops
Ithaca College Today Control: Condition codes Conditional branches Loops Switch Statements

3 Processor State (x86-64, Partial)
Ithaca College Processor State (x86-64, Partial) Information about currently executing program Temporary data ( %rax, … ) Location of runtime stack ( %rsp ) Location of current code control point ( %rip, … ) Status of recent tests ( CF, ZF, SF, OF ) Registers %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Instruction pointer %rip Current stack top CF ZF SF OF Condition codes

4 Condition Codes (Implicit Setting)
Ithaca College 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 CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 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

5 Note that these flags are set just like we talked about in chapter 2!
Condition Codes Examples: assume %rax contains 20 addl $10, %rax CF  0 ZF  0 SF  0 OF  0 subl $50, %rax CF  0 SF  1 Note that these flags are set just like we talked about in chapter 2!

6 Condition Codes Single Bit Registers
CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Also implicitly Set By logical Operations xorl Src,Dest C analog: t = a ^ b (a = Src, b = Dest) CF set to 0 ZF set if t == 0 SF set if t < 0 OF set to 0 For shift operations: CF set to last bit shifted out

7 Condition Codes (Implicit Setting)
There are no Boolean types in assembly language. There is no concept of Boolean operators in assembly language. So what is there? Bits All we can do is look at bits. What those bits mean is determined by the context of the program. Instructions set flags. Other instructions take action based on the bits in those flags. Don’t think about it as comparison as in the C language sense.

8 Condition Codes (Explicit Setting: Compare)
Ithaca College 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 carry out from most significant bit (used for unsigned comparisons) ZF set if a == b SF set if (a-b) < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) Note: notice that the operands are in reverse order; b is the first operand , but we compute a – b! Note 2: there are also cmpw and cmpb instructions.

9 Comparison and Boolean
In C if (a < b ) Which is the same as if (a – b < 0 ) a – b < 0 creates a Boolean value In assembly cmpq %rsi, %rdi Assuming that %rdi contains a and %rsi contains b Doesn’t create a Boolean value Does set condition codes ** we’ll talk about the if part later!

10 Example %rax 0000 %rcx 0010 %rdx %rbx %rsi %rdi %rsp %rbp cmpq %rax, %rcx execute %rcx - %rax then set condition codes NOTHING SAVED! Condition codes CF ZF SF OF ?? ?? ?? ??

11 Example %rax 0000 %rcx 0010 %rdx %rbx %rsi %rdi %rsp %rbp cmpq %rax, %rcx execute %rcx - %rax then set condition codes NOTHING SAVED! Condition codes CF ZF SF OF

12 Condition Codes (Explicit Setting: Test)
Explicit Setting by Test instruction testl/testq Src2, Src1 testl 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 %eax, %eax sets the condition codes depending on the value in %eax Note 2: there are also testw and testb instructions. Remember that bitwise operations are more efficient than arithmetic


Download ppt "Carnegie Mellon Ithaca College"

Similar presentations


Ads by Google