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

COMPUTER ORGANIZATION AND DESIGN 6th

Edition
The Hardware/Software Interface

Chapter 3
Arithmetic for Computers
§3.1 Introduction
Arithmetic for Computers

◼ Operations on integers
◼ Addition and subtraction
◼ Multiplication and division
◼ Dealing with overflow
◼ Floating-point real numbers
◼ Representation and operations

Chapter 3 — Arithmetic for Computers — 2


Binary Representation

01011000 00010101 00101110 11100111


Most significant bit Least significant bit
▪ To represent the following quantity:

0 x 231 + 1 x 230 + 0 x 229 + … + 1 x 20

▪ A 32-bit word can represent 232 numbers between


0 and 232-1
▪ This is known as the unsigned representation as we’re
assuming that numbers are always positive.
33
Negative Numbers
▪ The 32 bits can only represent 232 numbers
▪ If we wish to also represent negative numbers, we can
represent 231 positive numbers (incl zero) and 231
negative numbers.

0000 0000 0000 0000 0000 0000 0000 0000two = 0ten


0000 0000 0000 0000 0000 0000 0000 0001two = 1ten

0111 1111 1111 1111 1111 1111 1111 1111two = 231-1

1000 0000 0000 0000 0000 0000 0000 0000two = -231


1000 0000 0000 0000 0000 0000 0000 0001two = -(231 – 1)
1000 0000 0000 0000 0000 0000 0000 0010two = -(231 – 2)

1111 1111 1111 1111 1111 1111 1111 1110two = -2
1111 1111 1111 1111 1111 1111 1111 1111two = -1 44
Example

▪ Compute the 32-bit 2’s complement representations for


the following decimal numbers:
5, -5, -6

5: 0000 0000 0000 0000 0000 0000 0000 0101


-5: 1111 1111 1111 1111 1111 1111 1111 1011
-6: 1111 1111 1111 1111 1111 1111 1111 1010

▪ Given -5, verify that negating and adding 1 yields


the number 5.
55
Signed / Unsigned
The hardware recognizes two formats:

▪ unsigned (e.g., the C declaration unsigned int)


▪ all numbers are positive, a 1 in the most significant
bit just means it is a really large number

▪ signed (C declaration is signed int or just int)


▪ numbers can be +/- , a 1 in the MSB means the
number is negative

66
Sign Extension
▪ Occasionally, 16-bit signed numbers must be
converted into 32-bit signed numbers
▪ for example, when doing an add with an immediate operand

▪ The conversion is simple: take the most significant bit


and use it to fill up the additional bits on the left (known
as sign extension)
▪ Example:
210 goes from 0000 0000 0000 0010
to
0000 0000 0000 0000 0000 0000 0000 0010
-210 goes from 1111 1111 1111 1110
to
77
1111 1111 1111 1111 1111 1111 1111 1110
MIPS Instructions
▪ Consider the following comparison instruction:
slt $t0, $t1, $zero
and $t1 contains the 32-bit number 1111 01…01

▪ What gets stored in $t0?

▪ The result depends on whether $t1 is a signed or


unsigned number
▪ The compiler/programmer must track this and accordingly
use either slt or sltu

slt $t0, $t1, $zero stores 1 in $t0


sltu $t0, $t1, $zero stores 0 in $t0 88
§3.2 Addition and Subtraction
Integer Addition
◼ Example: 7 + 6

◼ Overflow if result out of range


◼ Adding +ve and –ve operands, no overflow
◼ Adding two +ve operands
◼ Overflow if result sign is 1
◼ Adding two –ve operands
◼ Overflow if result sign is 0

Chapter 3 — Arithmetic for Computers — 9


Integer Subtraction
◼ Add negation of second operand
◼ Example: 7 – 6 = 7 + (–6)
+7: 0000 0000 … 0000 0111
–6: 1111 1111 … 1111 1010
+1: 0000 0000 … 0000 0001
◼ Overflow if result out of range
◼ Subtracting two +ve or two –ve operands, no overflow
◼ Subtracting +ve from –ve operand
◼ Overflow if result sign is 0
◼ Subtracting –ve from +ve operand
◼ Overflow if result sign is 1

Chapter 3 — Arithmetic for Computers — 10


Overflow

11
11
§3.3 Multiplication
Multiplication
add operation

multiplicand
1000
multiplier
× 1001
1000
0000
0000
1000
product 1001000

Length of product is
the sum of operand
lengths

Chapter 3 — Arithmetic for Computers — 13


Multiplication Hardware
add operation

Initially 0

Chapter 3 — Arithmetic for Computers — 14


Multiplication

Example: Using 4-bit numbers to save


space, multiply 2 3 , or 0010 0011 .
ten ten two two

Initially 0

15
MIPS Multiplication
◼ Two 32-bit registers for product
◼ HI: most-significant 32 bits of the result
◼ LO: least-significant 32-bits of the result
◼ MIPS Instructions
◼ mult rs, rt / multu rs, rt
◼ 64-bit product in HI/LO
◼ mfhi rd / mflo rd
◼ Move from HI/LO to rd
◼ Can test HI value to see if product overflows 32 bits
◼ mul rd, rs, rt
◼ Least-significant 32 bits of product –> rd

Chapter 3 — Arithmetic for Computers — 16


§3.4 Division
Division
◼ Check for 0 divisor
◼ Long division approach
quotient ◼ If divisor ≤ dividend bits
dividend ◼ 1 bit in quotient, subtract
1001 ◼ Otherwise
1000 1001010 ◼ 0 bit in quotient, bring down next
-1000 dividend bit
divisor
10 ◼ Restoring division
101 ◼ Do the subtract, and if remainder
1010 goes < 0, add divisor back
-1000 ◼ Signed division
remainder 10
◼ Divide using absolute values
◼ Adjust sign of quotient and remainder
n-bit operands yield n-bit as required
quotient and remainder

Chapter 3 — Arithmetic for Computers — 17


Division Hardware
Initially divisor
in left half

Initially dividend

Chapter 3 — Arithmetic for Computers — 18


Division Initially divisor
in left half

Example: Using a 4-bit version of the


algorithm to save pages, let’s try dividing 7
ten

by 2 , or 0000 0111 by 0010 .


ten two two

Initially dividend

19
MIPS Division
◼ Use HI/LO registers for result
◼ HI: 32-bit remainder
◼ LO: 32-bit quotient
◼ Instructions
◼ div rs, rt / divu rs, rt
◼ No overflow or divide-by-0 checking
◼ Software must perform checks if required
◼ Use mfhi, mflo to access result

Chapter 3 — Arithmetic for Computers — 20

You might also like