03 Numbers and Arithmetic W
03 Numbers and Arithmetic W
Hakim Weatherspoon
CS 3410, Spring 2015
Computer Science
Cornell University
See: P&H Chapter 2.4, 3.2, B.2, B.5, B.6
inst
memory register alu
file
+4 +4
addr
=?
PC din dout
offset control cmp
memory
new target
imm
pc extend
99
100
How to convert a number between different bases?
Base conversion via repetitive division
• Divide by base, write remainder, move left with quotient
lsb (least significant bit)
• 637 ÷ 8 = 79 remainder 5
• 79 ÷ 8 = 9 remainder 7
• 9 ÷ 8 = 1 remainder 1
• 1 ÷ 8 = 0 remainder 1 msb (most significant bit)
637 = 0omsb1175lsb
Convert a base 10 number to a base 2 number
Base conversion via repetitive division
• Divide by base, write remainder, move left with quotient
lsb (least significant bit)
• 637 ÷ 2 = 318 remainder 1
• 318 ÷ 2 = 159 remainder 0
• 159 ÷ 2 = 79 remainder 1
• 79 ÷ 2 = 39 remainder 1
• 39 ÷ 2 = 19 remainder 1
• 19 ÷ 2 = 9 remainder 1
• 9÷2=4 remainder 1
• 4÷2=2 remainder 0
• 2÷2=1 remainder 0
• 1÷2=0 remainder 1 msb (most significant bit)
637 = 10 0111 1101 (can also be written as 0b10 0111 1101)
msb lsb
Convert a base 10 number to a base 16 number
Base conversion via repetitive division
• Divide by base, write remainder, move left with quotient
lsb
• 637 ÷ 16 = 39 remainder 13
• 39 ÷ 16 = 2 remainder 7
• 2 ÷ 16 = 0 remainder 2 dec = hex = bin
msb 10 = 0xa = 1010
11 = 0xb = 1011
637 = 0x 2 7 13 = 0x ?2 7 d 12
13
=
=
0xc
0xd
= 1100
= 1101
Thus, 637 = 0x27d 14 = 0xe = 1110
15 = 0xf = 1111
Convert a base 2 number to base 8 (oct) or 16 (hex)
Binary to Hexadecimal
• Convert each nibble (group of four bits) from binary to hex
• A nibble (four bits) ranges in value from 0…15, which is one hex digit
– Range: 0000…1111 (binary) => 0x0 …0xF (hex) => 0…15 (decimal)
• E.g. 0b10 0111 1101
– 0b10 = 0x2
– 0b0111 = 0x7
– 0b1101 = 0xd
– Thus, 637 = 0x27d = 0b10 0111 1101
Binary to Octal
• Convert each group of three bits from binary to oct
• Three bits range in value from 0…7, which is one octal digit
– Range: 0000…1111 (binary) => 0x0 …0xF (hex) => 0…15 (decimal)
• E.g. 0b1 001 111 101
– 0b1 = 0x1
– 0b001 = 0x1
– 0b111 = 0x7
– 0b101 = 0x5
– Thus, 637 = 0o1175 = 0b10 0111 1101
We can represent any number in any base
• Base 10 – Decimal
637 6∙102 + 3∙101 + 7∙100 = 637
102 101 100
• Base 2 — Binary
10 0111 1101 1∙29+1∙26+1∙25+1∙24+1∙23+1∙22+1∙20 = 637
29 28 27 26 25 24 23 22 21 20
• Base 8 — Octal
0o 1 1 7 5 1∙83 + 1∙82 + 7∙81 + 5∙80 = 637
83 82 81 80
• Base 16 — Hexadecimal
0x 2 7 d 2∙162 + 7∙161 + d∙160 = 637
162161160 2∙162 + 7∙161 + 13∙160 = 637
Digital computers are implemented via logic circuits and thus
represent all numbers in binary (base 2).
111
001110 Unsigned binary addition is pretty easy
+ 011100 • Combine two bits at a time
• Along with a carry
101010
Binary addition requires
• Add of two bits PLUS carry-in
• Also, carry-out if necessary
A B Half Adder
• Adds two 1-bit numbers
Cout • Computes 1-bit result and
1-bit carry
• No carry-in
S
A B Cout S
0 0
0 1
1 0
1 1
A B Full Adder
• Adds three 1-bit numbers
Cout Cin • Computes 1-bit result and 1-bit carry
• Can be cascaded
S
Activity: Truth Table and Sum-of-Product.
A B Cin Cout S
0 0 0 Logic minimization via Karnaugh Maps and
0 0 1 algebraic minimization.
0 1 0 Draw Logic Circuits
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
A[4] B[4] 4-Bit Full Adder
• Adds two 4-bit numbers and carry in
Cout Cin • Computes 4-bit result and carry out
• Can be cascaded
S[4]
A3 B 3 A2 B 2 A1 B 1 A0 B 0
Cout Cin
S3 S2 S1 S0
0000 = +0
1000 = -0
IBM 7090
Second Attempt: One’s complement
• Leading 0’s for positive and 1’s for negative
• Negative numbers: complement the positive number
0111 = 7
Problem? 1000 = -7
• Two zero’s still: +0 different than -0
• -1 if offset from two’s complement
• Complicated circuits
– Carry is difficult
0000 = +0
1111 = -0
PDP 1
What is used: Two’s Complement Representation
Examples
• 1 + -1 =
• -3 + -1 =
• -7 + 3 =
• 7 + (-3) =
• What is wrong with the following additions?
–7+1 -7 + -3 -7 + -1
Why create a new circuit?
Just use addition using two’s complement math
• How?
Two’s Complement Subtraction
• Subtraction is simply addition,
where one of the operands has been negated
– Negation is done by inverting all bits and adding one
� + 1)
A – B = A + (-B) = A + (B
B3 B2 B1 B0
A3 A2 A1 A0
Cout
S3 S2 S1 S0
Two’s Complement Subtraction
• Subtraction is simply addition,
where one of the operands has been negated
– Negation is done by inverting all bits and adding one
� + 1)
A – B = A + (-B) = A + (B
B3 B2 B1 B0
A3 A2 A1 A0
Cout 1
S3 S2 S1 S0
Q: How do we detect and handle overflows?
Q: What if (-B) overflows?
Digital computers are implemented via logic circuits and thus
represent all numbers in binary (base 2).
We (humans) often write numbers as decimal and hexadecimal
for convenience, so need to be able to convert to binary and
back (to understand what computer is doing!).