Lecture 4 Computer Arithematic (Sign Magnitude)
Lecture 4 Computer Arithematic (Sign Magnitude)
Computer Organization
and Architecture
8th Edition
Chapter 9
Computer Arithmetic
Sign Magnitude
Twos complement
ALU Inputs and Outputs
What is signed number?
01 00 (+4) 1 1 00 (-4)
+ 01 01 (+5) + 1 01 1 (-5)
01 001 (-7) 1 01 1 1 (+7)
• We cannot just include the carry out to produce a five-digit result,
as for unsigned addition. If we did, (-4) + (-5) would result in +23!
• Also, unlike the case with unsigned numbers, the carry out cannot
be used to detect overflow.
– In the example on the left, the carry out is 0 but there is over-
flow.
– Conversely, there are situations where the carry out is 1 but
there is no overflow.
Detecting signed overflow
• The easiest way to detect signed overflow is to look at all
the sign bits.
01 00 (+4) 1 1 00 (-4)
+ 01 01 (+5) + 1 01 1 (-5)
01 001 (-7) 1 01 1 1 (+7)
• Overflow occurs only in the two situations above:
– If you add two positive numbers and get a negative
result.
– If you add two negative numbers and get a positive
result.
• Overflow cannot occur if you add a positive number to a
negative number.
Sign extension
• In everyday life, decimal numbers are assumed to have
an infinite number of 0s in front of them. This helps in
“lining up” numbers.
• To subtract 231 and 3, for instance, you can imagine:
231
- 003
228
• Overflow is still a problem, but signed overflow is very
different from the unsigned overflow we mentioned last
time.
• Sign extension is needed to properly “lengthen”negative
numbers.
Addition of Numbers in Twos Complement Representation
Subtraction of Numbers
in Twos Complement Representation (M – S)
Geometric Depiction of Twos Complement Integers
Why does this work?