Lecture 5 - Integers and Arithmetic4
Download
Report
Transcript Lecture 5 - Integers and Arithmetic4
+
CS 325: CS Hardware and Software
Organization and Architecture
Integers and Arithmetic
Part 4
+
Outline
Binary
Multiplication
Booth’s Algorithm
Number
Representations
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
Multiplication by bit shifting and addition.
Removes the need for multiply circuit
Requires:
A way to compute 2’s Complement
Available as fast hardware instructions
X86 assembly instruction: NEG
A way to compare two values for equality
A
B
A
How to do this quickly?
0
0
Exclusive Not OR (NXOR) Gate
Compare all sequential bits of bit string A
0
1
and bit string B. Values are equal if the
1
0
comparison process produces all 1s.
A way to shift bit strings.
1
1
Arithmetic bit shift, which preserves the sign bit when shifting to the
right.
10110110
arithmetic shift right
11011011
x86 assembly instruction: SAR
NXOR B
1
0
0
1
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
Example: 5 x -3
First, convert to 2s comp bin:
5 = 0101
-3 = 1101
If
we add 0 to the right of both values, there are
4 0-1 or 1-0 switches in 0101, and 3 in 1101. Pick
1101 as X value, and 0101 as Y value
Next,
2s Comp of Y: 1011
for bin subtraction.
Next,
set 2 registers, U and V, to 0. Make a table
using U, V, and 2 additional registers X, and X-
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
Register X is set to the predetermined value of x, and X-1 is set to 0
U
V
X
X-1
0000
0000
1101
0
Rules: Look at the LSB of X and the number in the X-1 register.
If the LSB of X is 1, and X-1 is 0, we subtract Y from U.
If LSB of X is 0, and X-1 is 1, then we add Y to U.
If both LSB of X and X-1 are equal, do nothing and skip to shifting stage.
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
In our case, the LSB of X is one, and X-1 is zero, so we subtract Y from U.
U
V
X
X-1
0000
0000
1101
0
1000
1110
1
+1011
1011
1101
Next, we do an arithmetic right shift on U and V
1011 1101, 0000 1000
Copy the LSB of X into X-1
And then perform a circular right shift on X
1101 1110
Repeat the process three more times.
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
The LSB of X is zero, and X-1 is one, so we add Y to U.
U
V
X
X-1
1101
1000
1110
1
0100
0111
0
+0101
0010
0001
Next, we do an arithmetic right shift on U and V
0010 0001, 1000 0100
Copy the LSB of X into X-1
And then perform a circular right shift on X
1110 0111
Repeat the process two more times.
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
The LSB of X is one, and X-1 is zero, so we subtract Y from U.
U
V
X
X-1
0001
0100
0111
0
0010
1011
1
+1011
1100
1110
Next, we do an arithmetic right shift on U and V
1100 1110, 0100 0010
Copy the LSB of X into X-1
And then perform a circular right shift on X
0111 1011
Repeat the process one more time.
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
The LSB of X is one, and X-1 is one, begin shifts.
U
V
X
X-1
1110
0010
1011
1
1111
0001
1101
1
Next, we do an arithmetic right shift on U and V
1110 1111, 0010 0001
Copy the LSB of X into X-1
And then perform a circular right shift on X
1011 1101
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
The result is stored in U followed by V.
U
V
X
X-1
1111
0001
1101
1
11110001
This result is stored in 2’s complement notation.
Convert to decimal:
11110001 00001111 -1510
This gives the correct result of 3 x -5
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
Another Example: 7 x -4
First, convert to 2s comp bin:
7 0111, add zero to right gives 01110, 2 switches
-4 1100, add zero to right gives 11000, 1 switch
X = 1100
Y = 0111
-Y = 1001, for easy bin subtract
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
U
V
X
X-1
0: 0000
0000
1100
0
1: 0000
0000
0110
0
2: 0000
0000
0011
0
+1001
1001
3: 1100
1000
1001
1
4: 1110
0100
1100
1
Result of 7 x -4: UV
11100100 00011100 -2810
+
2’s Complement Binary
Multiplication – Booth’s Algorithm
Try:
-9 x 7
+
Numbers are stored at addresses
Memory
A
is a place to store bits
word is a fixed number of bits
Ex: 32 bits, or 4 bytes
An address is also a fixed
number of bits
Represented as
unsigned numbers
+
Numbering Bits and Bytes
Need
to choose order for:
Storage
in physical memory system
Transmission over serial/parallel medium
(data network)
Bit
order
Handled
by hardware
Usually hidden from programmer
Byte
order
Affects
multi-byte data items such as
integers
Visible and important to programmers
+
Possible Byte Orders
Least
significant byte of integer in
lowest memory location
Little endian
Most
Significant byte of integer in
lowest memory location.
Big endian
+
Byte Order Illustration
Note: Difference is especially important when transferring
data between computers for which the byte ordering
differs.
+
Sign Extension
Convert
2’s comp number using N bits
to more than N bits (int to long int):
Replicate
the MSB (sign bit) of the smaller
number to fill new bits.
2’s comp positive number has infinite 0s
2’s comp negative number has infinite 1s
Ex: 16bit
-410 to 32-bit:
1111 1111 1111 1100
1111 1111 1111 1111 1111 1111 1111 1100
+
Conclusion
We
represent “things” in computers as
particular bit patterns: N bits 2N
Decimal
for human calculations, binary
for computers, hex for convenient way to
write binary
2’s
comp universal in computing: so
make sure to learn!
Number
are infinite, computers are not,
so errors can occur (overflow,
underflow)
Know
the powers of 2.