Download Computer architecture Lecture6 and more Slides Computer Architecture and Organization in PDF only on Docsity!
Lecture 09- Computer Arithmetic
EE
Computer Architecture
1
2
Limits of Computer Numbers
Bits can represent anything!
Characters?
26 letter => 5 bits
upper/lower case + punctuation => 7 bits
(in 8)
rest of the world’s languages => 16 bits
(unicode)
Logical values?
0 -> False, 1 => True
colors?
locations / addresses? commands?
but N bits => only 2N^ things
4 EE204-L
Positive & Negative Numbers
So far, unsigned numbers
Obvious solution: define leftmost bit to be
sign!
Representation called sign and magnitude
MIPS uses 32-bit integers. +
ten
would be:
And - 1
ten
in sign and magnitude would be:
Arithmetic circuit more complicated
Special steps depending whether signs are
2’s Complement Number line
2 N-1^ non-
negatives
2 N-1^ negatives
one zero
how many
positives?
comparison?
overflow?
Sign extension
Convert 2’s complement number using n bits
to more than n bits
Simply replicate the most significant bit (sign
bit) of smaller to fill new bits
2’s comp. positive number has infinite 0s
2’s comp. negative number has infinite 1s
16-bit -
ten
to 32-bit:
two
two
8 EE204-L
Signed v. Unsigned Comparisons
X = 1111 1111 1111 1111 1111 1111 1111
two
Y = 0011 1011 1001 1010 1000 1010 0000
two
Is X > Y?
unsigned: YES
signed: NO
Converting to decimal to check
Signed comparison:
ten
ten?
Unsigned comparison:
ten
ten?
slt, slti and sltu, sltiu instructions
Determining Overflow
Overflow occurs when the signs of the values are the same,
and the sign of the result is different.
Overflow if Cin to MSB is different from Cout of MSB
Arithmetic and Logic Unit
The ALU is at the heart of the CPU
Does math and logic
• The ALU is primarily involved in R-type
instructions
– Perform an operation on two registers and
produce a result
• Where is the operation specified?
– The instruction type specifies the operation
– The ALU will have to be controlled by the
instruction opcode
ALU - Logic Operations
0 1 A B Operation Result 2-to-1 Mux If Operation = 0, Result = A • B If Operation = 1, Result = A Ú B Start out by supporting AND and OR operations Two operands, two results. We need only one result... The Operation input comes from logic that looks at the opcode
Arithmetic Unit
a b cin s cout 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
ab
cin
Adding to our ALU
CarryIn CarryOut ALU A B Cout Cin Result Cin Cout Op (2 bits) Operation Function 00 A • B 01 A Ú B 10 A + B Operation Function 00 A • B 01 A Ú B 10 A + B
(Op is now 2 bits) Add an Adder Connect CarryIn (from previous bit) and CarryOut (to next bit) Expand Mux to 3-to-1 (Op is now 2 bits) 0 1 Operation Result A B 2 0 1
1-bit & 32-bit ALU
• Connect to common
Operation controls
AND and OR operations
• Stack 32 of our 1-bit
ALU’s together
- (^) Each one gets one bit
from A and one from B
• Connect C
out
’s to C
in
’s
- (^) Now, 32-bit adds will
work
- (^) Note: Carry will ripple
through the stages, one
at a time
Subtraction
a input b input Cin input output A 0 0 A 0 B 0 B A 0 1 A+ A 0 0 A – 1* A B 1 A+B+ A B* 1 A – B A B 0 A + B A B* 0 A – B -