Numbers and Arithmetic
Numbers and Arithmetic
Numbers and Arithmetic
memory
inst
register file
+4
alu
+4
PC
offset
=? control
imm cmp
new pc
target extend
Example
Build a circuit (e.g. voting machine) Building blocks (encoders, decoders, multiplexors)
Number Representations
Recall: Binary
Two symbols (base 2): true and false; 0 and 1 Basis of Logic Circuits and all digital computers
Number Representations
Recall: Binary
Two symbols (base 2): true and false; 1 and 0 Basis of Logic Circuits and all digital computers
37
1101
23 22 21 20 162161160
0x 2 7 d
Number Representations
Recall: Binary
Two symbols (base 2): true and false; 1 and 0 Basis of Logic Circuits and all digital computers
37
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 1 0000 1 0001 1 0010
. .
0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22
. .
0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12
. .
99 100
Number Representations
How to convert a number between different bases? Base conversion via repetitive division
Divide by base, write remainder, move left with quotient
637 10 = 63 remainder 7 63 10 = 6 remainder 3 6 10 = 0 remainder 6
lsb (least significant bit)
Number Representations
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 92=4 remainder 1 42=2 remainder 0 22=1 remainder 0 12=0 remainder 1 msb (most significant bit) 637 = 10 0111 1101 (can also be written as 0b10 0111 1101)
msb lsb
Number Representations
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 dec = hex = bin 2 16 = 0 remainder 2
msb
10 11 12 13 14 15
= = = = = =
Number Representations
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 015, which is one hex digit E.g. 0b10 0111 1101
0b10 = 0x2 0b0111 = 0x7 0b1101 = 0xd Thus, 637 = 0x27d = 0b10 0111 1101 Range: 00001111 (binary) => 0x0 0xF (hex) => 015 (decimal)
Binary to Octal
Convert each group of three bits from binary to oct Three bits range in value from 07, which is one octal digit
E.g. 0b1 001 111 101
Range: 00001111 (binary) => 0x0 0xF (hex) => 015 (decimal) 0b1 = 0x1 0b001 = 0x1 0b111 = 0x7 0b101 = 0x5 Thus, 637 = 0o1175 = 0b10 0111 1101
Number Representations
Recall: Binary
Two symbols (base 2): true and false; 0 and 1 Basis of Logic Circuits and all digital computers
37
1101
23 22 21 20 162161160
0x 2 7 d
Takeaway
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!).
Next Goal
Binary Arithmetic: Add and Subtract two binary numbers
Binary Addition
How do we do arithmetic in binary? Addition works the same way regardless of base
Add the digits in each position Propagate the carry Unsigned binary addition is pretty easy Combine two bits at a time Along with a carry
1-bit Adder
A Cout S
A 0 0 1 1 B Cout S 0 1 0 1
Half Adder
Adds two 1-bit numbers Computes 1-bit result and 1-bit carry No carry-in
Full Adder
Cin
Adds three 1-bit numbers Computes 1-bit result and 1-bit carry Can be cascaded
Activity: Truth Table and Sum-of-Product. Logic minimization via Karnaugh Maps and algebraic minimization. Draw Logic Circuits
4-bit Adder
A[4] B[4] Cout S[4] 4-Bit Full Adder
Adds two 4-bit numbers and carry in Cin Computes 4-bit result and carry out Can be cascaded
4-bit Adder
A3 B 3 Cout S3 S2 S1 S0 A2 B 2 A1 B 1 A0 B 0 Cin
Adds two 4-bit numbers, along with carry-in Computes 4-bit result and carry out Carry-out = overflow indicates result does not fit in 4 bits
Takeaway
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!). Adding two 1-bit numbers generalizes to adding two numbers of any size since 1-bit full adders can be cascaded.
Next Goal
How do we subtract two binary numbers? Equivalent to adding with a negative number How do we represent negative numbers?
Problem?
Two zeros: +0 different than -0 Complicated circuits
IBM 7090
Twos Complement
Non-negatives Negatives
(as usual): (twos complement: flip then add 1):
Binary Subtraction
Twos Complement Subtraction Why create a new circuit? Just use addition
How?
Binary Subtraction
Twos Complement Subtraction
Subtraction is simply addition, where one of the operands has been negated
Negation is done by inverting all bits and adding one A B = A + (-B) = A + (B + 1)
B3
B2
B1
B0
A3 Cout
A2
A1
A0 1
S3
S2
S1
S0
Takeaway
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!). Adding two 1-bit numbers generalizes to adding two numbers of any size since 1-bit full adders can be cascaded. Using Twos complement number representation simplifies adder Logic circuit design (0 is unique, easy to negate). Subtraction is simply adding, where one operand is negated (twos complement; to negate just flip the bits and add 1). .
Next Goal
How do we detect and handle overflow?
Overflow
When can overflow occur?
adding a negative and a positive?
Takeaway
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!). Adding two 1-bit numbers generalizes to adding two numbers of any size since 1-bit full adders can be cascaded. Using Twos complement number representation simplifies adder Logic circuit design (0 is unique, easy to negate). Subtraction is simply adding, where one operand is negated (twos complement; to negate just flip the bits and add 1). Overflow if sign of operands A and B != sign of result S. Can detect overflow by testing Cin != Cout of the most significant bit (msb), which only occurs when previous statement is true.
A Calculator
A B
8
adder
S 0=add 1=sub
mux
decoder
Next Goal
Performance
Performance
Speed of a circuit is affected by the number of gates in series (on the critical path or the deepest level of logic)
Combinational Logic
tcombinational
outputs expected
inputs arrive
A 2 B2
C2
A1 B1
C1
A0 B0 C0
S0
S2
S1
First full adder, 2 gate delay Second full adder, 2 gate delay
Example
Build a circuit (e.g. voting machine) Building blocks (encoders, decoders, multiplexors)
Voting machine
For now, lets just display the numerical identifier to the ballot supervisor we wont do counting yet, just decoding we can use four photo-sensitive transistors to find out which hole is punched out
A photo-sensitive transistor detects the presence of light Photo-sensitive material triggers the gate
Ballot Reading
Input: paper with a hole in it Output: number the ballot supervisor can record
Ballots
Input
Photo-sensitive transistor
photons replenish gate depletion region can distinguish dark and light spots on paper
Vdd i0
i1 i2 i3 i4 i5 i6
Output
7-Segment LED
photons emitted when electrons fall into holes
d7 d6
d5 d4
d3 d2
d1 d0
Block Diagram
detect
Encoders
N might be large
0 1 2
4 5 6 7
encoder
...
...
1
o0 2 o1
o2
4 A 3-bit encoder
with 4 inputs for simplicity
1
o0 2 o1
1 0 0 0
o2
4 A 3-bit encoder
with 4 inputs for simplicity
Ballot Reading
detect
enc
Ballot Reading
Ok, we built first half of the machine Need to display the result
Ballots The 3410 optical scan vote counter reader machine
7LED decode
0 0 1
0 1 0 0 1 1
d2
d1
d3
d0
1 0 0
1 0 1 1 1 0 1 1 1
d4
d5
d6
0 0 1
0 1 0 0 1 1
0 1
1 1 1 1
1 1
0 1 1 0
1 0
0 0 1 0
1 1
1 1 1 0
0 0
1 1 1 0
1 1
0 1 1 1
1 1
1 0 0 1
d2
d1
d3
d0
1 0 0
1 0 1 1 1 0 1 1 1
d4
d5
d6
detect
enc
7LED decode
Ballots
The 3410 optical scan vote counter reader machine
Building Blocks
2N
binary encoder
N N
N
binary decoder
0
Multiplexor
1
2
2N
...
2M-1
Administrivia
Make sure you are
Registered for class, can access CMS Have a Section you can go to Have project partner in same Lab Section
Administrivia
Check online syllabus/schedule
http://www.cs.cornell.edu/Courses/CS3410/2013sp/schedule.html
Slides and Reading for lectures Office Hours Homework and Programming Assignments Prelims (in evenings):
Tuesday, February 26th Thursday, March 28th April 25th
Summary
We can now implement any combinational (combinatorial) logic circuit
Decompose large circuit into manageable blocks
Encoders, Decoders, Multiplexors, Adders, ...
Can implement circuits using NAND or NOR gates Can implement gates using use P- and N-transistors And can add and subtract numbers (in twos compliment)! Next time, state and finite state machines