Organization Overflow: what is signed and unsigned overflow? Overflow: how are signed and unsigned overflow recognized? Condition Codes and Branch Instructions -------------------------------------------------- Overflow: what is signed and unsigned overflow? -------------------------------------------------- "Overflow" means that the result of an operation does not fit in the allotted number of bits "unsigned overflow": the result doesn't fit, viewing the numbers as unsigned numbers "signed overflow": the result doesn't fit, viewing the numbers as signed numbers The V bit in the CCR is set when signed overflow occurs The C bit in the CCR is set when a carry results from addition; equivalent to unsigned overflow Why do we have two different bits? Because the C bit does not tell you whether or not signed overflow occurs To see this, consider the following cases; they show that a carry may or may not result in the case of signed overflow. **The following assumes 1-byte numbers*** Case 1: Carry, No Overflow (C=1, V=0) Decimal: Binary: signed: 10 + (-3) 00001010 unsigned: 10 + 253 +11111101 Case 2: Overflow, No Carry (V=1, C=0) Decimal: Binary: 100+50 01100100 +00110010 Case 3: Overflow, Carry (V=1, C=1) Decimal: Binary: signed: -100 + (-50) 10011100 unsigned: 156 + 206 +11001110 Case 4: No Overflow, No Carry (V=0, C=0) Decimal: Binary: 1 + 1 00000001 +00000001 Do a "reality check" in each case; before looking at the bit strings, or performing the operation, is there unsigned overflow? signed overflow? example: Does FF+FF result in overflow? Depends on whether they are signed or unsigned! ------------------------------------------------ Overflow: how are signed and unsigned overflow recognized? ------------------------------------------------ Unsigned overflow: Addition: a carry from the most significant bit Subtraction: A borrow out of the most significant bit Signed overflow: Addition: (positive + positive), result is negative (negative + negative), result is positive Subtraction: (positive - negative), result is negative (negative - positive), result is positive In lecture: Condition code bits are ALU outputs Go over notation used in the Boolean Formulas, Appendix A Confirm the above rules, looking at the boolean formulas for ADDA and SUBA ---------------------------------------------------- Overflow: Why do we care? --------------------------- Basic issue: will the result fit? Another important issue: the condition codes are checked by the branch instructions; the branch instructions are building blocks of if-statements and conditional loops ------------------------------------------------------------- The condition codes are set by compare and other instructions ------------------------------------------------------------- Lecture: intuitive idea of compare instructions followed by branch instructions; the branch instructions check the condition codes resulting from the compare instruction note that the condition codes are set after lots of operations, so an explicit compare instruction isn't always strictly necessary. However, explicit compare instructions make the code more readable. -------------------------------------------------- The M notation in the manual is a bit confusing. It is the effective address, for direct, extended, and indexed addressing. For immediate addressing, it is the immediate value. -------------------------------------------------- The N,Z,V,C bits are all affected by a compare op. N and V are for signed values; C is for unsigned values; Z is for both. -------------------------------------------------- CMP operations: (AccX) - (M) CP operations: (2-byte register) - (M):(M+1) The register does not change. Purpose? To set the condition codes. EGs: CMPA #$78 (AccA) - 01111000 CMPB Width (AccB) - (Width) CPD 20,X (AccD) - (20+(IX)):(20+(IX)+1) Suppose you are comparing X and M When is the N bit set after a compare instruction? When X_2c - M_2c is negative When is the Z bit set after a compare instruction? When X - M = 0 When is the C bit set after a compare instruction? When X_us - M_us requires a borrow out of MS bit When does this occur? When X_us < M_us When is the V bit set after a compare instruction? When you have either pattern: 011, 100 Intuitive: when the sign bit is not the one you would expect if everything fits properly. E.g., when you subtract a positive from a negative, you expect a negative number (-5 - 3 = -8) ------------------------------------------------- When do the branches succeed? ----------------------------- A branch instruction succeeds (the branch is performed) when the boolean formula evaluates to true Very important: some are for signed operands, others are for unsigned operands, some are for both Examples: For each, ask: is it for signed or unsigned operands? conceptually, when does the branch succeed? what is the boolean formula? examples of when it does and doesn't succeed ----------------------------- BLO: unsigned succeeds when X_us < M_us C=1 LDAA #$88 CMPA #$80 BLO label1 does not branch LDAA #$80 CMPA #$88 BLO label1 branches LDAA #$FF CMPA #0 BLO label1 does not branch LDAA #$0 CMPA #FF BLO label1 branches ----------------------------- BLT: signed succeeds when X_2c < M_2c N xor V = 1 EITHER: the result is negative, and there is no overflow (N=1,V=0) examples of all four cases both pos: 10 - 12 (and 10 IS less than 12) both neg: -10 - (-7) (and -10 IS less than -7) X neg, M pos: result always negative (if no overflow) (so X IS less than M) X pos, M neg: result never negative (if no overflow) in this case you can't have N=1,V=0 OR: the result is positive, and there is overflow (N=0,V=1) V=1 cases: 011 or 100 N=0,V=1: only 100 X is negative, M is positive, so X IS less than M LDAA #-128 CMPA #-120 BLT label1 branches LDAA #-120 CMPA #-128 BLT label1 does not branch LDAA #$80 CMPA #$88 BLT label1 branches; same as -128, -120 LDAA #$88 CMPA #$80 BLT label1 does not branch; same as -120, -128 LDAA #$FF CMPA #0 BLT label1 branches; same as -1, 0 LDAA #$0 CMPA #FF BLT label1 does not branch; same as 0, -1 -------------------------------- BHS: unsigned succeeds when X_us > M_us C=0 LDAA #$88 CMPA #$80 BHS label1 branches ---------------------------------------------- Look at the manual entry for SUB: C is ``set if the absolute value of the contents of memory are larger than the absolute value of the contents of the accumulator; cleared otherwise.'' This use of ``absolute value'' is confusing. What is the absolute value of $FF? Well, if we would take the absolute value of -5 to be 5, the absolute value of $FF could be considered to be 1 (since $FF_2c = -1_10). ``Treating them as unsigned values'' is less confusing. ================================================