COMP0068 Lecture4 Computer Arithmetic

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

Computer Architecture & Operating

Systems
(COMP0068)
Lecture 4: Computer Arithmetic

Relevant book sections:


Harris & Harris Book (2nd Ed.) – Section 1.4.
Patterson & Hennessy Book (4th Ed.) – Section 2.4.
– Chapter 3 more detail.
Lecture Overview
• In this lecture I’m going to survey important aspects of
computer arithmetic that you need to know for this
module.
• Some of this material will be well known and we will go
through it fairly rapidly.
• Other material will be less familiar and we will go
through it in more detail.
• In particular we will present addition / subtraction in a
rather formal algorithmic fashion ... this will prepare us
for understanding how instructions are coded in
processors.
• We will see how modern computers represent negative
numbers ... they do something different from the LMC
which uses a familiar “sign-and-magnitude” format. 2
Why Binary Arithmetic?

• Mapping data and instruction into hardware

• Hardware can only deal with binary digits, 0 and 1.

• Hardware must represent all numbers, integers,


floating point, positive or negative, by binary digits,
called bits.
Revision: Base 10 decimal system
Any number can be represented as a power series:
207110 = 2.103 + 0.102 + 7.101 + 1. 100
= 2.103 + 0.102 + 7.101 + 1
The subscript 10 on 200110 is there to reflect that this is
a decimal number.

“2071” is simply short for the power series above.

10 symbols in decimal: digits from 0 to 9 are used

Multiplying a decimal number by 10 (i.e. it’s base) involves


appending a ‘0’ to the number … 4
Binary System – base 2
2 symbols in binary – 0,1

11012 = 1.23 + 1.22 + 0.21 + 1.20 = 1310

“two” = 102

Multiplying by base: 11012 * 102 110102

Multiplying by two just adds a zero


to the right of a binary number.

What is 10101112 in base 10 ?


Show your working.
5
Binary system - mapping

MSB Binary Digit LSB


28 27 26 25 24 23 22 21 20
256 128 64 32 16 8 4 2 1

Example

Decimal digital value 256 128 64 32 16 8 4 2 1


Binary digit value 1 0 1 1 0 0 1 0 1

1011001012 = ?
6
Converting the other way ...
from decimal à binary ?
One way of converting from decimal to binary is by first
dividing by the “largest power of two” …
and then successively dividing by smaller ones
Repeated Division-by-2 Method
Quotient
(Result)

msb 1 0 1 lsb
1 0
22 ) 5 2 ) 1 2 ) 1

à 1012
Remainder

What is 4210 in binary ?


Hexadecimal System – base 16 –
more complex
16 symbols in hex – 0,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F

7D116 = ‘7’.162 + ‘D’.161 + ‘1’.160 = 200110


“sixteen” = 1016
Multiplying by base: 7D116 * 1016 7D1016

Multiplying by sixteen just adds a zero


to the right of a hexadecimal number.

What is BAD16 in base 10 ? What is 4210 in base 16 ?


8
Unfortunately we aren’t very good with
binary …
Can you read, replicate or remember this binary number:-

001010111110101110101101001000012
This has 32 bits (binary digits) – the standard size for
addresses and data in many current computers.

Need shorter representation for displays, input for humans.


(computers don’t care)
A decimal representation is fine if the number has a meaning
as a decimal number, e.g. a salary, number of cars sold,
but not for all things, e.g. not for a computer instruction. 9
Hexadecimal – Shorthand for a Binary Number
For each 4 bit pattern there is a unique hexadecimal symbol

Binary Hex Binary Hex Binary Hex Binary Hex


0000 - 0 0100 - 4 1000 - 8 1100 - C
0001 - 1 0101 - 5 1001 - 9 1101 - D
0010 - 2 0110 - 6 1010 - A 1110 - E
0011 - 3 0111 - 7 1011 - B 1111 - F
Translating binary to hex and hex to binary is simple:
just translate each group of 4 bits starting from right!
Binary number - 001010111110101110101101001000012
Space it out - 0010 1011 1110 1011 1010 1101 0010 00012
Hex equivalent 2 B E B A D 2 1

001010111110101110101101001000012 = 2BEBAD2116 10
Talking binary! (compared to talking decimal)

• A “binary digit” or “bit” is just a single 0 or 1 value.


A lower-case “b” is used to represent a unit of bits.
• A “byte” is 8 bits and an uppercase “B” is used for a unit of bytes.

• Now traditionally 1 kilobyte (of memory for instance) was written


1KB and was equal to 210 = 1024 bytes.
• Similarly 1 megabyte = 1MB = 220 = 1024 KB = 1,048,576 bytes.
• And “giga” -> 230, “tera” -> 240, “peta” -> 250, “exa” -> 260

• HOWEVER ... this use of Kilo for 1024 rather than 1000 directly
conflicted with international S.I. unit nomenclature .... so ...

11
Introduction of kibibyte, mebibyte, etc.
• 1 kibibyte = 1KiB = 1024 bytes
• 1 mebibyte = 1MiB = 1024 KiB = 1,048,576 bytes
• 1 gibibyte = 1GiB = 1024 MiB, etc.

• BUT ... both nomenclatures are still in widespread use ... so often it
is the context which tells you whether it is a “binary” version of the
units or a “decimal” version of the units.

• So 1 KB of memory is mostly likely to be 1024 bytes (or what should


more formally called 1 KiB of memory these days).

• But in communications 512 Kbps is 512,000 bits per second.


• And 1 MFLOP is 1,000,000 floating point instructions per second. 12
Decimal System - Addition 34510
27310 +
We all know how to add in decimal:
61810
Let digits of 345 be labeled: r2, r1, r0 r2=3, r1 =4, r0 =5

Let digits of 273 be labeled: s2, s1, s0 s2=2, s1 =7, s0 =3

Rule for adding columns: ri + si + ci = ci+1.10 + ti


ti is the answer digit
ci+1 is the carry out into the next column
ci is the carry-in to the column from the right

Column 0 (c0 =0) :- 5 + 3 + 0 -> 0.10 + 8 -> t0 = 8


Column 1 (c1 =0) :- 4 + 7 + 0 -> 1.10 + 1 -> t1 = 1
Column 2 (c2 =1) :- 3 + 2 + 1 -> 0.10 + 6 -> t2 = 6
13
Column 3 and on :- 0 + 0 + 0 -> 0.10 + 0 -> t3 = 0 (leading zeroes)
Binary - Base 2 – a simpler operation
2 symbols in binary – 0,1
1310 = 1.23 + 1.22 + 0.21 + 1.20 = 11012

Addition 01012 Addition rule:-


11112 +
ri + si + ci = ci+1.2 + ti
101002
Column 0 (c0 =0) :- 1 + 1 + 0 -> 1.2 + 0 -> t0 = 0
Column 1 (c1 =1) :- 0 + 1 + 1 -> 1.2 + 0 -> t1 = 0
Column 2 (c2 =1) :- 1 + 1 + 1 -> 1.2 + 1 -> t2 = 1
Column 3 (c3 =1) :- 0 + 1 + 1 -> 1.2 + 0 -> t3 = 0
Column 4 (c4 =1) :- 0 + 0 + 1 -> 0.2 + 1 -> t3 = 1
Column 5 and on :- 0 + 0 + 0 -> 0.2 + 0 -> t3 = 0 14
Hexadecimal – Base 16 – more complex
16 symbols in hex – 0,1,2,3,4,5,6,7,8,9, a, b,c,d,e,f

200110 = ‘7’.162 + ‘D’.161 + ‘1’.160 = 7D116

Addition abc16 Addition rule:-


34716 +
ri + si + ci = ci+1.16 + ti
e0316
A, a = 1010
Column 0 (c0 =0) :- c + 7 + 0 -> 1.16 + 3 -> t0 = 3 B, b = 1110
C, c = 1210
Column 1 (c1 =1) :- b + 4 + 1 -> 1.16 + 0 -> t1 = 0 D, d = 1310
Column 2 (c2 =1) :- a + 3 + 1 -> 0.16 + e -> t2 = e E, e = 1410
F, f = 1510
Column 3 and on :- 0 + 0 + 0 -> 0.16 + 0 -> t3 = 0
15
Ok – let’s check it all works as expected …

Shout out two 2-digit hexadecimal numbers …


Add them together using the previous rule …

Now convert them to binary …


Add the two binary numbers together …
And convert back to hexadecimal …
Do you get the same answer ?

16
Decimal System - Subtraction
34510
27310 -
We all know how to subtract in decimal:
07210
Let digits of 345 be labeled: r2, r1, r0 r2=3, r1 =4, r0 =5

Let digits of 273 be labeled: s2, s1, s0 s2=2, s1 =7, s0 =3

Rule for subtracting columns: ri - si - bi = ti - bi+1.10


ti is the answer digit
bi+1 is the borrow from the next column
bi is the borrow to the column from the right : b0 = 0

Column 0 (b0 =0) :- 5 - 3 - 0 -> 2 -> 2 - 0.10 -> t0 = 2


Column 1 (b1 =0) :- 4 - 7 - 0 -> -3-> 7 - 1.10 -> t1 = 7
Column 2 (b2 =1) :- 3 - 2 - 1 -> 0 -> 0 + 0.10 -> t2 = 0 17
Hexadecimal - Subtraction E4516
27316 -
Subtraction in hexadecimal:
BD216
Let digits of E45 be labeled: r2, r1, r0 r2= E, r1 = 4, r0 = 5

Let digits of 273 be labeled: s2, s1, s0 s2= 2, s1 = 7, s0 = 3


Rule for subtracting columns: ri - si - bi = ti - bi+1.16
ti is the answer digit
bi+1 is the borrow from the next column A, a = 1010
bi is the borrow to the column from the right : b0 = 0 B, b = 1110
C, c = 1210
D, d = 1310
Column 0 (b0 =0) :- 5 - 3 - 0 -> 2 -> 2 - 0.16 -> t0 = 2
E, e = 1410
Column 1 (b1 =0) :- 4 - 7 - 0 -> -3-> D - 1.16 -> t1 = D F, f = 1510
Column 2 (b2 =1) :- E - 2 - 1 -> B ->B + 0.16 -> t2 = B 18
Subtraction – Binary & Base ‘X’

Subtraction rule for binary: ri - si - bi = ti - bi+1.2

Subtraction rule for base ‘X’: ri - si - bi = ti - bi+1.X

19
Negative numbers
Sign-magnitude notation for decimal numbers

In decimal, we simply use the +/- sign to indicate whether the number
is positive or negative.
-234
‘-’ is the sign and ‘234’ is the magnitude

Problem with binary numbers:

-010102

No negative sign within computer registers/memory ...


only 0 and 1 symbols!!

How to represent negative numbers without a negative sign?


20
How to represent negative numbers in
binary?
First method – Use the MSB as the sign bit
• For signed binary numbers the most significant bit (MSB) is used as the sign bit.
• If the MSB is ”0”, this means the number is positive. If the MSB is “1”, then
the number is negative.
• The remaining bits in the number are used to represent the magnitude of the
binary number.
Disadvantages of MSB and sign/magnitude
• Examples: method
• Reduce the range of numbers we can
represent.
-2(n-1) to +2(n-1)

• +5310 - 5310 = 0 in binary = ??!!

• A positive result for zero, +0 or 00002, and a


negative result for zero, -0 or 10002. Both
are valid but which one is correct? 21
How to represent negative numbers in
binary?
Second method – Use the MSB and two’s complement
• The most significant bit (MSB) is used as the sign bit.
• In order to find the two’s complement of a number:
• Invert all of the bits in the number
• Add 1 to the LSB position

• Examples: consider a representation using 4 bits


• +210 = 00102 à 1101 à add 1 to LSB à 11102 -210

• Note that the the remaining bits (excluding the sign bit) are interpreted differently
than for sign/magnitude numbers.

22
Converting from 2’s complement to
decimal
1. Invert all bits
2. Add ‘1’ to the LSB
3. The MSB is the sign bit

Example:
1001à 0110 à 0111 => +710

10012
01112 +
---------
1 0000

Note: When adding N-bit numbers, the carry out of the Nth bit is discarded.

23
Advantages of using the 2’s complement
and MSB in representing negative binary
numbers

• The addition works properly

• Subtraction works by taking the 2’s complement of the second number,


then adding.

• 310 – 410 => 310 + (– 410)

• The representation of zero is always all 0’s

• 0000 à 1111 à 0000 (in a 4-bit representation the 5th bit is


discarded)

24
2s Complement Form - Binary
Replace each symbol si in binary number with complement : 1- si

Add 1 to the resulting binary number

Example: 001011 Each 0 à 1- s à1 Each 1 à 1- s à0

110100 +1 110101

110101 is the 2’s complement of 001011


Remember 110101 is really …1111…1111110101
lots of leading 1s
001011
110101
000000 25
Complement Form - Decimal
(Producing the negative of a number)
Number - 000012310
Replace each digit by its complement using rule: 9- si
0: 9-0 à9 1: 9-1 à8 2: 9-2 à7 3: 9-3 à6
Thus complementing the digits:- And adding +1 gives :-
000012310 à 999987610 999987710

Actually 0000123 has an infinite number of leading zeroes


so 999987710 has an infinite number of leading 9s
Is …999…99999…99987710 really –00012310?

…000…00000…00012310
QED! …999…99999…99987710 +
26
…000…00000…00000010
Complement Form - Hexadecimal
Replace each symbol si in binary number with complement : F- si

Add 1 to the resulting hexadecimal number

Example: 5FAB103416

A054EFCB16 +1 A054EFCC16

A054EFCC16 is the 16’s complement of 5FAB103416


Remember A054EFCC16 is really …FFFF…FFFFA054EFCC16
lots of leading Fs
5FAB1034
Note: in many systems hex numbers are identified A054EFCC
by preceding them with 0x, e.g. 0x5FAB1034,
000000000 27
0x12345678
X’s Complement Forms

Decimal X=10 : replacing each si by (9-si).

Binary X=2 : replacing each si by (1-si).

Hexadecimal X=16 : replacing each si by (F-si).

… AND THEN ADD ONE TO THE RESULT.

28
Questions to think about …
• Why didn’t computers not just use a ‘0’ or ‘1’ at the start of
numbers to indicate positive or negative numbers?
(i.e. a sign bit ... using sign & magnitude format ... similar
to what the LMC simulator does)

• Can we simply convert a negative (2’s complement) binary


number into a negative (16’s complement) hexadecimal
number by forming groups of 4 bits (as before) and using
our binary <-> hex conversion table as before?

• What would happen if our binary number was not a


multiple of 4 bits long in the previous case?

• If we had an 8 bit negative (2’s complement) binary


number – how would we convert it to the equivalent 32 bit
number ? 29
Summary
• This lecture has been about representing and
converting numbers between different bases
(decimal, binary, hexadecimal = “hex”):
– Special quick conversion between binary <-> hex.
– Special meaning of kilo / kibi (K), mega / mebi (M),
giga / gibi (G), etc. in a “binary” context.
• Algorithmic process for adding and subtracting
numbers using different bases.
• The X-complement form to represent negative
numbers in different bases (with 2s- and
16s-complement needed for modern binary
computers) 30
References

• Two's Complement by Thomas Finley, April 2000,


http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.ht
ml
• Two's complement, Wikipedia article,
https://en.wikipedia.org/wiki/Two%27s_complement

31

You might also like