E N D
SE225 Computer Organization and Assembly Language Lecture # 11- Arithmetic for computers
What do computers do? • Computers manipulate representations of things! • What can you represent with N bits? • 2N things! • Which things? • Numbers! Characters! Pixels! Dollars! Position! Instructions! ... • Depends on what operations you do on them
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
Arithmetic Operations • Numbers represented in binary • Positive & Negative numbers ? • Largest Number ? • Result larger than largest number? • Fractions & real numbers representation? • Hardware add, subtract, multiply, divide?
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. +1ten would be: 0000 0000 0000 0000 0000 0000 0000 0001 • And - 1ten in sign and magnitude would be: 1000 0000 0000 0000 0000 0000 0000 0001 • Arithmetic circuit more complicated • Special steps depending whether signs are the same or not
2’s Complement Number line 00000 11111 00001 11100 • 2 N-1 non-negatives • 2 N-1 negatives • one zero • how many positives? • comparison? • overflow? 00010 0 -1 1 2 -2 . . . . . . 15 -15 -16 01111 10001 10000
Signed vs Unsigned • Signed Load • Unsigned Load • Instructions for Load • lw (load word) • lb (load byte) • lh (load half)
Signed v. Unsigned Comparisons • X = 1111 1111 1111 1111 1111 1111 1111 1100two • Y = 0011 1011 1001 1010 1000 1010 0000 0000two • Is X > Y? • unsigned: YES • signed: NO • Converting to decimal to check • Signed comparison: -4ten < 1,000,000,000ten? • Unsigned comparison: 4,294,967,292ten < 1,000,000,000ten
Signed vs Unsigned (see on less than) • stl (set on less than) • stli (set on less than immediate) • stlu (set on less than unsigned) • stliu (set on less than immediate unsigned)
Signed vs Unsigned (see on less than) EXAMPLE • $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 • $s1 = 0000 0000 0000 0000 0000 0000 0000 0000 • stl $t0, $s0, $s1 • stli $t0, $s0, $s1
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 -4ten to 32-bit: 1111 1111 1111 1100two 1111 1111 1111 1111 1111 1111 1111 1100two
Numbers are stored at addresses 00000 • Memory is a place to store bits • A word is a fixed number of bits (eg, 32) at an address • also fixed no. of bits • Addresses are naturally represented as unsigned numbers 01110 101101100110 11111 = 2k - 1
Addition & Subtraction • Addition: Binary Addition • Subtraction: Binary 2’s comp. Addition • Overflow • Adding two positive numbers • Adding two negative numbers
Determining Overflow 0111 (+7) 0111 (+7) 0101 (+5)1011 (-5) 1100 (-4) 10010 (+2) 1001 (-7) 1001 (-7) 0101 (+5)1011 (-5) 1110 (-2) 10100 (+4) 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
New Arithmetic Instruction to Ignore Overflow • add, addi and sub – causes exception on overflow • addu, addiu and subu – do not cause exceptions on overflow
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
0 1 ALU - Logic Operations Start out by supporting AND and OR operations Operation 2-to-1 Mux A AB Result A+B B If Operation = 0, Result = A • BIf Operation = 1, Result = A Ú B Two operands, two results.We need only one result... The Operation input comes from logic that looks at the opcode
cin 0 1 ab 00 01 11 10 Arithmetic Unit
Operation A 0 0 Result 1 1 Cin Op (2 bits) + 2 B Cin A Result ALU B Cout Cout Adding to our ALU (Op is now 2 bits) CarryIn Operation Function 00 A • B 01 A Ú B 10 A + B Add an Adder CarryOut Connect CarryIn (from previous bit) and CarryOut (to next bit) Expand Mux to 3-to-1 (Op is now 2 bits)
1-bit & 32-bit ALU • Stack 32 of our 1-bit ALU’s together • Each one gets one bit from A and one from B • Connect to common Operation controls • Now we can do 32-bit AND and OR operations • Connect Cout’s to Cin’s • Now, 32-bit adds will work • Note: Carry will ripple through the stages, one at a time • Ripple-Carry Adder
Operation CarryIn • Our ALU can add now, but what about subtraction? • To compute A - B, we can instead compute A + (-B) • In 2’s complement, -B = B + 1 A 0 1 Result 0 + 2 • Add an inverter, and a signal BInvert to get B 1 CarryOut Subtracting Set to 1 for LSB BInvert B B • Now, how about that +1? • CarryIn to LSB is unused (always zero) • Set it to 1! • Subtraction just sets BInvert and Cin to 1 For subtraction: Set CarryInof LSB to 1, Set BInvert to 1
Operation BInvert CarryIn A 0 0 1 1 Result B 0 + 2 2 1 3 CarryOut Support for SLT • We need to support the SLT operation • Set Result to 0000 0000 0000 0000 0000 0000 0000 0001if A <B • A<B is equivalent to(A - B) < 0 • Subtract B from A • If the result is negative, then set LSB of Result to ‘1’, all others to ‘0’ • The result is negative if the MSB after the subtraction is ‘1’ (Two’s complement) Less Less will be ‘0’ for bits 1-31, special for bit 0 We’re going to have to do something differentfor the MSB and the LSB
Operation BInvert CarryIn A 0 1 Result B 0 + 2 1 Less 3 MSB Only CarryOut That tricky MSB • To properly execute the SLT, we need to Set the LSB if the MSB is ‘1’ • (After a subtraction) • Can’t use the ‘Result’ of the MSB • Op will set the Mux to the ‘Less’ Field • Bring out the adder output directly: ‘Set’ Set • Also, we need to check for overflow • Overflow if Cin to MSB is different from Cout of MSB OverFlow