Lecture 5-Computer Arithematics-Part 1-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Computer Organization

and Architecture

Chapter 4
Computer Arithmetic

Lecture 5
Course Content in Lectures

• Chapter 1 - Introduction

• Chapter 2 - Computer Evolution and Performance

• Chapter 3 - Computer Function and Interconnection

• Chapter 4 - Computer Arithmetic

• Chapter 5 - Instruction Sets: Characteristics and Functions

• Chapter 6 - Instruction Sets: Addressing Modes and Formats

• Chapter 7 - Processor Structure and Function

2
Arithmetic & Logic Unit
• Does the calculations
• Everything else in the computer is there
to service this unit
• Handles integers
• Handles floating point (real) numbers
ALU Inputs and Outputs
Integer Representation
• Only have 0 & 1 to represent everything
• Positive numbers stored in binary
—e.g., 41=00101001
• No minus sign
• No period (100.1101)
• To represent signed numbers:
— Sign-Magnitude
— Two’s compliment
Unsigned Integers
• In general, if an n-bit sequence of binary
digits 𝑎𝑛−1 𝑎𝑛−2 … 𝑎1 𝑎0 is interpreted as an
unsigned integer A, its value is
𝑛−1

𝐴 = ෍ 2𝑖 𝑎𝑖
𝑖=0
• Example] 11010011 = 20 + 21 + 24 + 26 +
27 =1 + 2 + 16 + 64 + 128 = 211
• An n-bit word can represent numbers
from 0 to 2𝑛 − 1 . Example, 8-bit can
represent numbers from 0 to 255.
Sign-Magnitude
• It used to represent positive and negative
numbers.
• Left most bit is sign bit
— 0 means positive
— 1 means negative
— +18 = 00010010
— -18 = 10010010
Sign-Magnitude
• The general case can be expressed as
follows:
𝐴 = σ𝑛−2
𝑖=0 2 𝑖𝑎
𝑖 if 𝑎𝑛−1 = 0
𝐴 = − σ𝑛−2
𝑖=0 2 𝑖 𝑎 if 𝑎
𝑖 𝑛−1 = 1

• Example]
— 1101 = − 20 + 22 = −5
— 0101 = 20 + 22 = 5
Sign-Magnitude
• Problems
—Need to consider both sign and magnitude in
arithmetic
– Example]
4 − 2 = 4 + −2 = 0100 + 1010 = 1110 (−6) incorrect
—Two representations of zero (+0 (00000000)
and -0(10000000))
Two’s Complement
• twos complement uses most significant bit
as a sign bit, making it easy to test
whether an integer is positive or negative.
— +3 = 00000011
— +2 = 00000010 +3 = 00000011
— +1 = 00000001 11111100
— +0 = 00000000 + 1
11111101 (-3)
— -1 = 11111111
— -2 = 11111110
— -3 = 11111101
Characteristics of Two’s Complement

8-bit can represent numbers between −27 𝑡𝑜 +27 −1 (−128 𝑡𝑜 + 127)


Characteristics of Two’s Complement

Zero is represented as 0000 in 4-bit or 00000000 in 8-bit


Characteristics of Two’s Complement

To represent +3 in 4-bit → 0011 and in 8-bit → 00000011


To represent -3 in 4-bit → 1101 and in 8-bit → 11111101
Characteristics of Two’s Complement

5 0101
+7 + 0111
------ ---------
12 1100 (overflow)
Characteristics of Two’s Complement

0010 5 0101
1101 -2 + 1110
+ 1
--------
------ ---------
1110 3 0011
Two’s Complement
• The equation defines the twos
complement representation for both
positive and negative numbers.
𝑛−2

𝐓𝐰𝐨𝐬 𝐂𝐨𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭 𝐴 = −2𝑛−1 𝑎𝑛−1 + ෍ 2𝑖 𝑎𝑖


𝑖=0

• Examples:
— 1110 = −23 × 1 + 22 × 1 + 21 × 1 + 20 × 0 = −8 + 4 + 2 + 0 = −2
— 0111 = −23 × 0 + 22 × 1 + 21 × 1 + 20 × 1 = 0 + 4 + 2 + 1 = 7
Value Box
Negation Special Case 1
• 0= 00000000
• Bitwise not 11111111
• Add 1 to LSB +1
• Result 1 00000000
• Overflow is ignored, so:
• -0=0
Negation Special Case 2
• -128 = 10000000
• bitwise not 01111111
• Add 1 to LSB +1
• Result 10000000
• So:
• -(-128) = -128 X
• Monitor MSB (sign bit)
• It should change during negation
Range of Numbers
• 8 bit 2s complement
—+127 = 01111111 = 27 -1
— -128 = 10000000 = -27
• 16 bit 2s complement
—+32767 = 011111111 11111111 = 215 - 1
— -32768 = 100000000 00000000 = -215
Geometric Depiction of Twos
Complement Integers
Addition and Subtraction
• Normal binary addition
• Monitor sign bit for overflow
Addition and Subtraction
• To subtract one number (subtrahend) from
another (minuend), take the twos complement
(negation) of subtrahend and add it to minuend.

i.e., a - b = a + (-b)
Hardware for Addition and Subtraction

• So, we only need addition and complement circuits


Multiplication
• Complex
• Work out partial product for each digit
• Take care with place value (column)
• Add partial products
Multiplication Example
• 1011 Multiplicand (11 dec)
• x 1101 Multiplier (13 dec)
• 1011 Partial products
• 0000 Note: if multiplier bit is 1 copy
• 1011 multiplicand (place value)
• 1011 otherwise zero
• 10001111 Product (143 dec)
• Note: need double length result (2𝑛 bits)

For each 1 on the multiplier, an add and a shift operation are


required; but for each 0, only a shift is required.
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


bit to right

C and A initial 0
Repeat for each bit in multiplier
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


bit to right

0 0000
1101
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
1 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


ADD bit to right

0 1011
1101
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
1 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


Shift bit to right

0 0101
1110
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
2 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


Shift bit to right

0 0010
1111
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
3 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


ADD bit to right

0 1101
1111
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
3 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


Shift bit to right

0 0110
1111
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
4 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


ADD bit to right

1 0001
1111
Unsigned Binary Multiplication

IF 𝑄0 = 1 𝑡ℎ𝑒𝑛 𝐴 = 𝐴 + 𝑀 and
4 1011 shift C, A,Q one bit to right

IF 𝑄0 = 0 𝑡ℎ𝑒𝑛 shift C, A,Q one


Shift bit to right

0 1000
1111
Execution of Example
Flowchart for Unsigned Binary
Multiplication
Multiplying Negative Numbers
• In unsigned binary multiplication, We
multiplied 11 (1011) by 13 (1101) to get
143 (10001111). If we interpret these as
twos complement numbers, we have -5
(1011) times -3 (1101) equals -113
(10001111).
Multiplying Negative Numbers
• In case of negative numbers, each partial
product must be a negative twos
complement number of 2𝑛 bits. Note that
this is accomplished by padding out each
partial product to the left with binary 1s.

What about 7 × -3?


Multiplying Negative Numbers
• Solution 1:
—convert both multiplier and multiplicand to
positive numbers, perform the multiplication,
and then take the twos complement of the
result if and only if the sign of the two original
numbers differed.
• Solution 2:
—Booth Algorithm
Booth’s Algorithm

Q 𝑄−1
Booth’s Algorithm

1 M = 0111

A = 0000

Q = 0011 𝑄−1 =0

A = 1001
Booth’s Algorithm

1 M = 0111

A = 1100

Q = 1001 𝑄−1 = 1

A = 1001
Booth’s Algorithm

2 M = 0111

A = 1100

Q = 1001 𝑄−1 = 1
Booth’s Algorithm

2 M = 0111

A = 1110

Q = 0100 𝑄−1 = 1
Booth’s Algorithm

3 M = 0111

A = 1110

Q = 0100 𝑄−1 = 1

A = 0101
Booth’s Algorithm

3 M = 0111

A = 0010

Q = 1010 𝑄−1 = 0

A = 0101
Booth’s Algorithm

4 M = 0111

A = 0010

Q = 1010 𝑄−1 = 0
Booth’s Algorithm

4 M = 0111

A = 0001

Q = 0101 𝑄−1 = 0

Result = 00010101
Example of Booth’s Algorithm
Examples of Booth’s

You might also like