Testing, Arithmetic
- Signed / Unsigned Testing
- Multiplication
- Assembly-language Multiplication
Testing -- SPARC
Signed | Unsigned |
bl | blu |
ble | bleu |
be | be |
bne | bne |
bge | bgeu |
bg | bgu |
|
Signed and Unsigned branches test different condition codes
Testing -- Intel
Signed | Unsigned |
JL | JB |
JLE | JBE |
JE | JE |
JNE | JNE |
JGE | JAE |
JG | JA |
|
Signed and Unsigned branches test different condition codes (see page
3-241 of Intel manual). "B" stands for "below", "A" for
"above".
Other condition-code testing
SPARC | Intel |
bneg | JS |
bpos | JNS |
bz (be) | JZ (JE) |
bnz (bne) | JNZ (JNE) |
bvs | JO |
bvc | JNO |
bcs (blu) | JC (JB) |
bcc (bgeu) | JNC (JAE) |
|
Binary Multiplication
- base b
- multiply X = X3X2X1 by Y = Y3Y2Y1
- if Y >= 0,
X * Y = X * Y3 * b^2 + X * Y2 * b^1 + X * Y1 * b^0
- reduces problem to multiplying X3X2X1 by a single digit Yi,
and adding the results
- if b = 2, the multiplication is easy
- result of multiplying two m- by n-digit numbers may have
as many as m * n digits.
Unsigned Binary Multiplication
- multiply X = 0111 01012 = 117[10] by Y = 1001 00112 = 147[10]
- expected 16-bit result is R = 17199[10] = 0100 0011 0010 11112
- add shifted copies of X corresponding to the "1" bits of Y:
0000 0000 0111 0101 +
0000 0000 1110 1010 +
0000 0111 0101 0000 +
0011 1010 1000 0000
=====================
0100 0011 0010 1111
Complement Multiplication
- base 10
- multiply X = 13 by Y = -12 = 88
(expected result is R = -156 = 9844)
- but, R' = 13 * 88 = 1144 != 9844
- R' = X * (10^2 - Y) = (X * 10^2) - (X * Y)
- So we need to subtract (X * 10^2) to get the correct result R
- -(X * 10^2) = -1300 = 8700
- R = R' - X * 10^2 = 1144 + 8700 = 9844
Complement Multiplication
using enough digits
- base 10
- multiply X = 0013 by Y = -12 = 9988
(expected result is R = -156 = 9844)
- R = 13 * 9988 = 129844 mod 10000 = 9844
Signed Binary Multiplication
sufficient digits
- multiply X = 0000 0000 0111 01012 = 117[10]
by Y = 1111 1111 1001 00112 = -109[10]
- R = -12753[10] = 1100 1110 0010 1111
0000 0000 0111 0101 +
0000 0000 1110 1010 +
0000 0111 0101 0000 +
0011 1010 1000 0000 +
0111 0101 0000 0000 +
1110 1010 0000 0000 +
1101 0100 0000 0000 +
1010 1000 0000 0000 +
0101 0000 0000 0000 +
1010 0000 0000 0000 +
0100 0000 0000 0000 +
1000 0000 0000 0000
=====================
100 1100 1110 0010 1111
Signed Binary Multiplication
- multiply X = 0111 01012 = 117[10] by Y = 1001 00112 = -109[10]
- R = -12753[10] = 1100 1110 0010 1111
0000 0000 0111 0101 +
0000 0000 1110 1010 +
0000 0111 0101 0000 +
0011 1010 1000 0000
=====================
0100 0011 0010 1111
- correction add: -X * 2^8 = 1000 1011 0000 0000
0100 0011 0010 1111 +
1000 1011 0000 0000
=====================
1100 1110 0010 1111
Multiplication -- insight
- The top bits of a sign-extended Y, Y[2n-1]... Y[n],
will always be all 1
- -1 * 2^n will always be all 1 in its top bits
- X * -1 * 2^n is the same as X * Y[2n-1]... Y[n]
- Two different algorithms produce the same correct result
SPARC Multiplication
- no built-in multiplication ( .mul, .umul)
- multiplier placed into special %Y register
- clear high-order part of product to zero
- clear N and Z flags
- mulscc (multiply step) 32 times:
- shift multiplicand right, saving bit shifted out,
shifting in N XOR V
- if %Y AND 1 then dest = dest + multiplicand
- shift %Y to the right, shifting in the bit taken
from the multiplicand
- mulscc with zero
- 64-bit result is in %r, %Y
Intel Multiplication
- MUL instruction for unsigned multiplication
- MUL reg/mem: only source s is explicit, destination
is implicit:
- 8 bits: AX = AL * s
- 16 bits: DX:AX = AX * s
- 32 bits: EDX:EAX = EAX * s
- OF and CF are set if the upper half is non-zero
Exercise
- Multiply 77 * 13 (8-bit numbers) on both SPARC and Intel
- Multiply 15 * -17 (8-bit numbers) on both SPARC and Intel
- Step through with debugger to verify behavior, correctness
- Bring printout of code to class on Wed for "mutual grading"