Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

32
Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College

Transcript of Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Page 1: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Arithmetic for Computers

CPSC 252 Computer Organization

Ellen Walker, Hiram College

Page 2: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Encoding Numbers

• Two symbols (1 vs. 0)– Binary logic easiest to implement

electronically

• How to represent arbitrary numbers?– Ascii Characters– 4 bits per decimal digit (Binary Coded

Decimal)– Raw binary (base 2 numbers)

Page 3: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

ASCII vs. BCD vs. Binary

• 33 in ASCII00110011 00110011 (8*log10 bits)

• 33 in Binary Coded Decimal0011 0011 (4*log10 bits)

• 33 in Binary100001 log2 bits

Page 4: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Bit Numbering Convention

• Bits numbered from right to left, starting with 033 = 0 0 1 0 0 0 0 1Bits: 7 6 5 4 3 2 1 0

• Bit 0 is Least Significant Bit (LSB)• Bit 7 is Most Significant Bit (MSB)

Page 5: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Signed Numbers (Sign / Magnitude)

• Sign + Magnitude– One bit used for sign (convention 1 =

negative)– N-1 bits for magnitude

• Difficulties– 2 representations for 0– Where does the sign go? (MSB vs. LSB)– Complex addition algorithm

Page 6: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Two’s Complement

• MSB (sign bit) is the -2^(N-1) place and all other bits are 2^x (0<=x<N-1)

• Advantages:– No changes to addition algorithm– Easy negation (flip the bits and add 1)– Easy test for negative (MSB is 1)

Page 7: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Largest & Smallest Values

• N bits can represent 2^N combinations– Unsigned: 0 to (2^N) - 1– Sign/Magnitude -(2^N-1)+1 to (2^N-1)-1– 2’s Complement -2^N-1 to (2^N-1)-1

• Arithmetic operations that result in values outside this range cause arithmetic overflow

Page 8: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Arithmetic Overflow

• Arithmetic overflow occurs when the sign bit is incorrect after an operation

• Example (8 bit 2’s complement)01111111 127

+ 00000011 3

--------------------

10000010 -126

Page 9: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Unsigned Integers

• If negative values aren’t appropriate, use all bits

• Values from 0 to (2^N)-1 (all positive)

• Example: addresses

Page 10: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Effect on Instruction sets

• Instructions for both signed and unsigned values– Loading / storing– Comparisons– Arithmetic / Logic operations

Page 11: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Sign Extension

• When copying a shorter signed number into a longer register, don’t change the sign!– 11110000 = 111111110000, not 000011110000

• To avoid this, replicate the sign bit all the way to the MSB

• Sign bit replication never changes the value of a number

Page 12: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Shortcuts for Decimal to Binary

• Compute minimal bit pattern for absolute value– Repeated divide-by-two, saving

remainders– MSB must be 0 (add one if needed)

• If negative value is desired, flip the bits and add 1

• Sign-extend to required number of bits

Page 13: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Decimal-to-binary examples

• 1037 (16 bit)

• -38 (8 bit)

Page 14: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Loading Numbers into Registers

• A complete register - lw– No special cases, since all 32 bits specified

• A byte – lb (load byte) sign-extends– lbu (load byte unsigned) pads with 0’s

• A halfword (2 bytes)– lh (load halfword) sign-extends– Lhu (load halfword unsigned) pads with 0’s

Page 15: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Comparisons

• Set on less than (slt, slti)– Treats registers as signed numbers

• Set on less than unsigned (sltu, sltui)– Treats registers as unsigned numbers

• Example– $s1 holds 00….001, $s2 holds 11..110– Slt $t0, $s1, $s2 puts 0 in $t0– Sltu $t0, $s1, $s2 puts 1 in $t0

Page 16: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Shortcut for 0<=x < y

• Bit patterns for negative numbers, when treated as unsigned, are larger than all bit patterns for positive numbers. (Why?)

• If (unsigned) x < (unsigned) y, and y is known to be positive, we also know that x is positive.

• Use sltu to check both negative and too big in one instruction.

Page 17: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Integer Addition & Subtraction

• Algorithm in base 2 is same as base 10• Add up each column of numbers from

right to left– A column consists of 2 numbers plus the

carry from the previous column (0 if first column)

– If the sum is greater than 1, carry a 1 to the next column, e.g. 1+1+1 = 3 (1, carry the 1)

Page 18: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

One-Column Addition Table

In1 In2 C in Out C out

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

Page 19: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Addition example (8 bit 2’s complement)

• Carry shown above in red 01100000

00110100 52

+ 00010010 18

--------------------

01000110 70

Page 20: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Subtraction

• Use the usual subtraction algorithm with borrows

• Negate the second operand, then add– Advantage: reuse existing hardware– Negation is easy in hardware

• Bit flip• Increment

Page 21: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Overflow (Signed Numbers)

• Adding two positive numbers– Overflow if sign bit becomes 1

• Adding two negative numbers– Overflow if sign bit becomes 0

• Adding positive and negative number– Overflow cannot occur (why?)

Page 22: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Signed Overflow Detection Algorithm

• IF the sign bits of the operands are equal,

• AND the sign bit of the result does not equal the sign bits of the operands

• THEN overflow has occurred

Page 23: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Unsigned Integers

• Overflow could be detected by having a separate sign bit in addition to the 32 bit register

• However, overflow in memory addresses is commonly ignored– “Wrap” from highest to lowest location

Page 24: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Relevant Instructions

• Add, addi, sub, subi– Detect overflow and cause an exception

when it occurs

• Addu, addiu, subu– Never cause an overflow exception

Page 25: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Dealing with Overflow

• Exception code (like a procedure call)• Special conditional branch

– Many architectures, not MIPS– “Branch on overflow” instruction

• Write your own code– Do an unsigned arithmetic operation– Check signs of operands and result– Use xor operation: 1 if different, 0 if equal (see p.

228)

Page 26: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Consequences of Fixed Integer Representations

• Limited number of values (both positive and negative)

• Moving from shorter to longer format requires sign extension

• Unsigned representations allow twice as many values (but no negatives)

• Arithmetic overflow must be detected– Correct algorithm on valid inputs yields incorrect

result

Page 27: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Multiplication

• Multiplication result needs twice as many bits– E.g. 7*7 = 49 (7 is 3 bits, 49 is 6 bits)

• Multiplication is more complex than addition/subtraction– Develop algorithm, implement using

simpler hardware

Page 28: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Multiplication Algorithms

• Repeated addition– Easy to implement in software– Can only multiply positive numbers directly– Time depends on magnitude of operands

• Shift-and add– Efficient use of hardware– Time depends only on width of operands– No special case for negative numbers

Page 29: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Shift and Add Algorithm

• For each bit in multiplier– If bit = 1, add multiplicand to result– Shift multiplicand left by 1

Page 30: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Shift and Add example

10101 21

X 101 5

---------

10101

000000 (included for completeness)

+ 1010100

------------------

1101001 105 (1+8+32+64)

Page 31: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Division

• Use the long division algorithm (shift and subtract)

• Put 1 in the quotient whenever leading bit is 1, else put 0 in the quotient

• When all bits from dividend have been used, if difference is not 0, it is the remainder.

Page 32: Arithmetic for Computers CPSC 252 Computer Organization Ellen Walker, Hiram College.

Division Example

10101

101 | 1101001

101

0110

101

0101

101

0