+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

19
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4

Transcript of + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

Page 1: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+ CS 325: CS Hardware and SoftwareOrganization and Architecture

Integers and Arithmetic

Part 4

Page 2: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+Outline

Binary Multiplication Booth’s Algorithm

Number Representations

Page 3: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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

How to do this quickly?Exclusive Not OR (NXOR) Gate

Compare all sequential bits of bit string A and bit string B. Values are equal if the comparison process produces all 1s.

A way to shift bit strings. Arithmetic bit shift, which preserves the sign bit when

shifting to the right.10110110 arithmetic shift right 11011011

x86 assembly instruction: SAR

A B A NXOR B

0 0 1

0 1 0

1 0 0

1 1 1

Page 4: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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-1.

Page 5: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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

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.

U V X X-1

0000 0000 1101 0

Page 6: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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.

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.

U V X X-1

0000 0000 1101 0

+1011

1011

1101 1000 1110 1

Page 7: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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.

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.

U V X X-1

1101 1000 1110 1

+0101

0010

0001 0100 0111 0

Page 8: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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.

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.

U V X X-1

0001 0100 0111 0

+1011

1100

1110 0010 1011 1

Page 9: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+2’s Complement Binary Multiplication – Booth’s Algorithm The LSB of X is one, and X-1 is one, begin shifts.

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

U V X X-1

1110 0010 1011 1

1111 0001 1101 1

Page 10: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+2’s Complement Binary Multiplication – Booth’s Algorithm The result is stored in U followed by V.

This result is stored in 2’s complement notation. Convert to decimal:

11110001 00001111 -1510

This gives the correct result of 3 x -5

U V X X-1

1111 0001 1101 1

11110001

Page 11: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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

Page 12: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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: UV11100100 00011100 -2810

Page 13: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+2’s Complement Binary Multiplication – Booth’s Algorithm

Try: -9 x 7

Page 14: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+Numbers are stored at addressesMemory is a place to store bits

A word is a fixed number of bitsEx: 32 bits, or 4 bytes

An address is also a fixed number of bits

Represented as unsigned numbers

Page 15: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+Numbering Bits and Bytes

Need to choose order for:Storage in physical memory systemTransmission over serial/parallel medium

(data network)

Bit orderHandled by hardwareUsually hidden from programmer

Byte orderAffects multi-byte data items such as

integersVisible and important to programmers

Page 16: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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

Page 17: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+Byte Order Illustration

Note: Difference is especially important when transferring data between computers for which the byte ordering differs.

Page 18: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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 11001111 1111 1111 1111 1111 1111 1111 1100

Page 19: + CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.

+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.