Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Organization COMP 210

Similar presentations


Presentation on theme: "Computer Organization COMP 210"— Presentation transcript:

1 Computer Organization COMP 210
Integers Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Consequences of overflow Using shifts to perform power-of-2 multiply/divide

2 C Puzzles Answers on the last slide
Assume machine with 32 bit word size, two’s complement integers For each of the following C expressions, either: Argue that is true for all argument values Give example where not true x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x <= 0  -x >= 0 Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y;

3 Binary addition 1

4 Carry bit Problem: cells have fixed number of bits
If addition requires more bits, must carry a bit out. Called a carry bit or a carry out. Example: 6-bit cell (assume unsigned) Carry bit Carry bit

5 Binary subtraction 0 1 11 10 6 0 0 1 1 3 0 0 1 1 (6 – 3 = 3)
(6 – 3 = 3) When the bottom bit is larger than the top bit, must borrow from the next left position.

6 Subtraction: Carry bit
When subtract, may have to borrow. If most significant bit needs a borrow, must carry a bit in. Called a carry in. Set the C bit to indicate that the result is not valid. Example: 6-bit cell (assume unsigned) –58 ? Carry bit Carry bit

7 Visualizing Integer Addition
4-bit integers u, v Compute true sum Add4(u , v) Values increase linearly with u and v Forms planar surface May need extra bit Largest 4 bit number = 15 = 30 30 = (5 bits) Add4(u , v) v u

8 Unsigned Addition Standard Addition Function
• • • Operands: w bits + v • • • True Sum: w+1 bits u + v • • • Discard Carry: w bits UAddw(u , v) • • • Standard Addition Function Ignores carry output Implements Modular Arithmetic s = UAddw(u , v) = u + v mod 2w

9 Unsigned Addition Explanation for results 1 1 1 1 15 1 1 1 1 0 30 30
= 16 –16

10 Visualizing Unsigned Addition
Wraps Around If true sum ≥ 2w At most once Overflow UAdd4(u , v) True Sum 2w 2w+1 Overflow v Modular Sum u

11 Adding 2’s complement 0 0 1 +1 0 1 1 +3 1 1 0 –2
Just add as normal. Always works (ignore the carry bit). –2 –2 –4

12 Two’s Complement Addition
u • • • Operands: w bits + v • • • True Sum: w+1 bits u + v • • • Discard Carry: w bits TAddw(u , v) • • • TAdd and UAdd have Identical Bit-Level Behavior Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v Will give s == t

13 Status bits CPU keeps track of 4 bits: CVZN bits
These are saved in a special register A register has one word of storage They are set when an arithmetic operation is performed a few other operations in assembly language will also set them Carry: bit that is carried out of an addition overflow (v): set if the arithmetic overflows (see later slide) zero (z): set to 1 if the result of the arithmetic was zero, otherwise the z bit is set to 0 negative (n): set to 1 if the result of the arithmetic is negative, otherwise the n bit is set to 0 Flag: C V Z N 1

14 Status Bits Example 1: 1 0 1 0 + 1 0 1 1 0 1 0 1 c: 1 z: 0 v: 1 n: 0
c: 1 z: 0 v: 0 n: 1 Example 3: c: 1 z: 1 v: 0 n: 0 Example 4: c: 1 z: 0 v: 0 n: 1

15 Overflow bit What if the result is too big for the number of bits?
Have overflow. Carry bit no longer indicates whether the sum is in range. CPU contains a special bit called the overflow bit denoted by the letter V If sum is out of range, V = 1 Otherwise V = 0

16 Overflow bit CPU will always set the V bit and continue. Does not care if V = 1 or if V = 0. Note that overflow cannot occur if: Adding two numbers of different signs Result must be smaller than either number Subtracting two numbers of same sign This is the same as adding two numbers of different signs: x – y = x + (–y)

17 Detecting 2’s Comp. Overflow
Task Given s = TAddw(u , v) Determine if s = Addw(u , v) Example int s, u, v; s = u + v; Claim Overflow iff either: u, v < 0, s  0 (NegOver) u, v  0, s < 0 (PosOver) ovf = (u<0 == v<0) && (u<0 != s<0); 2w –1 2w–1 PosOver NegOver

18 Characterizing TAdd Functionality True sum requires w+1 bits
Drop off MSB Treat remaining bits as 2’s comp. integer –2w –1 –2w 2w –1 2w–1 True Sum TAdd Result 1 000…0 1 100…0 0 000…0 0 100…0 0 111…1 100…0 000…0 011…1 PosOver u v < 0 > 0 NegOver PosOver TAdd(u , v) NegOver (NegOver) (PosOver)

19 Visualizing 2’s Comp. Addition
NegOver Values 4-bit two’s comp. Range from -8 to +7 Wraps Around If sum  2w–1 Becomes negative At most once If sum < –2w–1 Becomes positive TAdd4(u , v) v u PosOver

20 How do we get to bits? We write programs in an abstract language like C How do we get to the binary representation? Compilers & Assemblers! We write x = 5. The compiler changes it into mov 5, 0x005F The assembler changes it into: 80483c7: 8b 45 5F Compiler figures out that this is an integer and changes it into 2’s Complement

21 Conversions: see slide 17 When to use unsigned: see slide 26
C Puzzle Answers Assume machine with 32 bit word size, two’s comp. integers TMin makes a good counterexample in many cases x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x <= 0  -x >= 0 x < 0  ((x*2) < 0) False: TMin ux >= 0 True: 0 = UMin x & 7 == 7  (x<<30) < 0 True: x1 = 1 ux > -1 False: 0 x > y  -x < -y False: -1, TMin x * x >= 0 False: x > 0 && y > 0  x + y > 0 False: TMax, TMax x >= 0  -x <= 0 True: –TMax < 0 x <= 0  -x >= 0 False: TMin Conversions: see slide 17 Casting: see slide 20, 21 When to use unsigned: see slide 26


Download ppt "Computer Organization COMP 210"

Similar presentations


Ads by Google