CPE 232 Computer Organization MIPS Arithmetic - Part I
CPE 232 Computer Organization MIPS Arithmetic - Part I
CPE 232 Computer Organization MIPS Arithmetic - Part I
Dr. Gheith Abandah [Adapted from the slides of Professor Mary Irwin (www.cse.psu.edu/~mji) which in turn Adapted from Computer Organization and Design, Patterson & Hennessy, 2005, UCB]
maxint
MSB
1111 1111 1111 1111 1111 1111 1111 1110two = 2ten 1111 1111 1111 1111 1111 1111 1111 1111two = 1ten
minint
LSB
copy the most significant bit (the sign bit) into the empty bits 0010 -> 0000 0010 1010 -> 1111 1010 sign extend versus zero extend (lb vs. lbu)
2
zero ovf
1
1
ALU 32 4 m (operation)
result
and, andi, nor, or, ori, xor, xori beq, bne, slt, slti, sltiu, sltu
sign extend addi, addiu andi, ori, xori, slti, sltiu zero extend lbu, addiu, sltiu no overflow detected addu, addiu, subu, multu, divu, sltiu, sltu
3
Negate
-(23 - 1) =
23 - 1 =
2sc binary 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111
decimal -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
4
Binary Addition
A B
0 1 1 1
S = A B carry_in
add/sub A0 B0 A1
control (0=add,1=sub) B0
B1 A2
S1
S2
B2
c32=carry_out
7
Overflow Detection
Overflow: the result is too large to represent in 32 bits Overflow occurs when
adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive gives a negative or, subtract a positive from a negative gives a positive
Carry into MSB xor Carry out of MSB, ex for 4 bit signed numbers
1 0 1 1 0 0 1 1 1 1 1 1 0 7 3 6 + 1 0 1 1 0 1 0 1 0 1 1 0 1 1 4 5 7
0 1
Bit wise operations (no carry operation involved) Need a logic gate for each function, mux to choose the output Use subtraction to determine if (a b) < 0 (implies a < b) Copy the sign bit into the low order bit of the result, set remaining result bits to 0
Again use subtraction: (a - b) = 0 implies a = b Additional logic to nor all result bits together
Immediates are sign extended outside the ALU with wiring (i.e., no logic needed)
9
MIPS ALU
Least-significant bits
Function and or add sub slt Bnegate 0 0 0 1 1 Operation 00 01 10 10 11
10
MIPS ALU
Most-significant bit
Bnegate 0 0 0 1 1
Operation 00 01 10 10 11
11
MIPS ALU
12
13
Carry-Lookahead Adder
Carry-Lookahead Circuit
14
Carry-Lookahead Adder
gi = ai . bi pi = ai + bi
15
Carry-Lookahead Adder
Carry Equations:
c1 = g0 + p0c0
c0 c4 g0 p0 c3 c2 c1 a0 b0
s0
17
P = p0p1p2p3
18