Binary Arithmetic
- Quiz
- Division
- Extended Precision Arithmetic
- Project 1 (Homework 5)
Binary Division
- a lot simpler than decimal division
- terms: ((dividend - remainder) / divisor) = quotient
(quotient * divisor) + remainder = dividend
- example: dividend 13, divisor 4, quotient 3, remainder 1
- division operation has 2 inputs (dividend and divisor) and
two outputs (quotient and remainder)
Unsigned Binary Division: algorithm
(A - R) / B = Q
- Q = R = 0
- compute B' = B shifted to the left until the most
significant bit is 1.
- looplab: if A < B then R = A and we are done.
- Q = Q * 2
- if A >= B' then:
- shift B' to the right (logical shift).
- return to step "looplab".
Unsigned Binary Division: example
130 / 11: A = 10000010, B = 00001011
A | B' | A >= B' | Q |
10000010 | 10110000 | f | 00000000 |
10000010 | 01011000 | t | 00000001 |
00101010 | 00101100 | f | 00000010 |
00101010 | 00010110 | t | 00000101 |
00010100 | 00001011 | t | 00001011 |
00001001 | 00000101 | -- | 00001011 |
|
Q = 00001011 = 11[10],
R = 00001001 = 9[10]
Nonrestoring division
- Note conditional subtraction: subtract only if A >= B
- Implementation: always do A - B, and add B back again
if the result is negative.
- Note that on the next step we will subtract B/2.
- Mathematically, the computation if A - B < 0 is
A - B + B - B2^{-1} = A - B + B2^{-1}
- Optimization: instead of adding B back, add B/2 (the shifted
B) if the dividend A is negative.
- This is called non-restoring division
- The quotient gets a "1" bit when the result is positive, a "0"
bit otherwise.
Nonrestoring Division: example
130 / 11: A = 10000010, B = 00001011
A | B' | A - B' or | Q |
| | A + B' | |
10000010 | 10110000 | 11010010 | 00000000 |
11010010 | 01011000 | 00101010 | 00000001 |
00101010 | 00101100 | 11111110 | 00000010 |
11111110 | 00010110 | 00010100 | 00000101 |
00010100 | 00001011 | 00001001 | 00001011 |
|
Q = 00001011 = 11[10],
R = 00001001 = 9[10]
SPARC Division
- .div, .udiv, .rem, .urem,
- dividend in %o0 , divisor in %o1, result in %o0
- no special instruction such as mulscc
- assumption: division is rare, no need to have it in hardware
Intel Division
- DIV instruction for unsigned division
- DIV reg/mem: only source (divisor) is explicit, destination
(dividend and quotient) is implicit:
- 8 bits: AX = dividend, AL = quotient, AH = remainder
- 16 bits: DX:AX = dividend, AX = quotient,
DX = remainder
- 16 bits: EDX:EAX = dividend, EAX = quotient,
EDX = remainder
Extended Precision Arithmetic
- For arithmetic over more than 32/64 bits
- Example: cryptographic code (RSA)
- Example: scientific computations
- Do the arithmetic one word at a time
- Similar to decimal arithmetic (one digit at a time)
Extended Precision Addition
- Start with low-order word
- Add with carry for all but the first word (or clear carry flag,
and add with carry for all words)
- SPARC: addx or addxcc
- Intel: ADC
Extended Precision Subtraction
- Start with low-order word
- Subtract with carry (borrow) for all but the first word
(or clear carry flag, and subtract with carry/borrow for all words)
- SPARC: subx or subxcc
- Intel: SBB
Alternative: complement and add (to complement, do the 1's complement,
then use extended-precision addition to add 1)
Extended Precision Multiplication
- A * B
- Multiply A by low-order word (or bit) of B
- Shift A, then multiply by next low-order word (or bit) of B
- Add the two products, repeat
- Use extended-precision addition as subroutine
Extended Precision Division
- Produce one bit of quotient at a time
- Use restoring or non-restoring division
- Use extended-precision addition and subtraction as subroutines
- Need a test for positive/negative result or for magnitude comparison
Project
- build a four-function calculator
- adding, subtracting, multiplying, and dividing
- unsigned binary numbers up to 4000 bits (500 bytes) long
- two-week, double-credit homework (due October 7th)
- input-output: /home/1/esb/312/inout.c
- full description: http://www.ics.hawaii.edu/~esb/1998fall.ics312/hw5.html