D75P 34R – HNC Computer Architecture

Download Report

Transcript D75P 34R – HNC Computer Architecture

D75P 34R - HNC Computer
Architecture
Week 3
Arithmetic Operations in Binary
and Hex
© C Nyssen/Aberdeen College 2004
All images © C Nyssen /Aberdeen College unless otherwise stated
Prepared 22/9/04
We saw in week 1 that Computers can only “understand”
one language - binary.
Everything a computer stores or manipulates is reduced
to a series of switches, which are either
ON -
-1
or
OFF -
-0
We have already learned how numbers are stored as
combinations of on/off, high voltage/ground, magnetized/not
magnetized, and so on, using the digits 1 and 0 to represent
these values.
Program execution and the
manipulation of data
depend on the
combinations of 1s and 0s
changing.
In reality this happens billions of times per second typically 1 x 109 times per second for a 1 GHz
processor.
(We look at this in much greater detail in Outcome 2).
One of the simplest ways
to understand how this
happens is to learn how to
add and subtract numbers
in binary.
In a moment we will learn to add two binary
numbers. But first, let’s recap adding in base 10.....
Let’s begin by adding two numbers in base 10!
1000’s
This is pretty easy ....
100’s
3
6
10’s
1
2
1’s
5
4
3+6 =
1+2 =
4+5 =
9
3
9
This one’s harder...
Carry 1 here
1000’s
Carry 1 here
Carry 1 here
100’s
4
6
10’s
9
9
1’s
5
6
As we only have digits that run from 0 -9, the
moment the count reaches 10, we have to move
one column to the left...
1
4+6+1= 11
Carry 1
remainder
9+9 +1 = 19
carry 1
6+5 = 11
carry 1
remainder
remainder
1
9
1
Adding in binary works in exactly the same way.
You may find it useful to make up a wee table of binary
addition values before you start...
0+0=0
0+1=1
1 + 1 = 0 carry 1
1 + 1 + 1 = 1 carry 1
Adding the following numbers is easy...
1
0
1
1
1
0
0
1
1
0
0
0
0
0
0
0
1
0
0
1
1
1
All we have to do is add 1’s and zeroes!
1
0
1
1
1
0
1
1
This one is harder...
carry 1 carry 1 carry 1 carry 1 carry 1
carry 1
0
0
1
1
1
1
0
0
1
0
0
1
0
0
1
1
1
0
1
1
1+1
=0
carry
1+1
=0
carry
1+1
=0
carry
1
1
1
0
0
1
0
1+1+1
=1
carry 1
1
1+1
=0
carry
1+1
=0
carry
1
1
0
1
1
0
1
You will be required to perform both binary and
hexadecimal additions as part of your
assessment material.
Example – what is the value of
0
1
1
0
1
1
0
1
1
0
0
0
1
0
0
0
0
added to ...
1
1
0
1
1
0
The answer is...
0
1
0
0
0
1
1
You will have noticed that the last addition
between the two 10-digit numbers gave us
an 11-digit answer!
The whole answer was 10100011000.
If only 10 digits had been set aside to hold
the answer, this calculation would have
resulted in an “overflow” - trying to fit too
large a number into too small a space!
Likewise, you will also be required to
perform simple addition and subtraction
in a hexadecimal base.
4096’s 256’s 16’s
1
6
B
3
1’s
A
0
C
A
9
Again, this is pretty easy...
B or 11 + 1 = 12 or
6+3=
A or 10 + 0 = 10 or
C
9
A
This one is harder...
carry 1
4096’s
1
carry 1
carry 1
256’s
5
A
16’s
D
7
1’s
F
9
0
5
8
F or 15 + 9 = 24
Carry 1 lot of 16
A or 10 + 5 + 1 = 16
Carry 1 lot of 16
D or 13 + 7 + 1 = 21 Remainder 8
remainder 0
Carry 1 lot of 16
remainder 5
One for you to try....
4096’s
256’s
6
A
16’s
0
E
1’s
F
6
1
0
F
5
Did you manage to get 10F5h ?
Subtracting the second number from the first in binary can be
pretty awkward sometimes...
0
1
1
0
1
1
0
1
1
1
0
0
1
0
0
1
0
0
0
1
This sum is not too bad as we are just subtracting zeroes,
or 1’s, from 1’s.
0
1
0
0
1
0
0
1
1
0
But what about ...
0
2
1
0
1
1
1
1
0
0
0
2
0
1
0
1
0
1
0
0
1
2
1
minus
0
Same thing...
0
1
1
0 - 1 can’t do! So we
“borrow” 1 from the right hand
column., which then becomes
0... And now we can take 1
from 2, leaving 1
Same thing...
0
0
1
1
0
0
0
1
0
A wee one for you to try....
0
1
0
0
1
0
0
0
1
0
1
0
1
0
0
0
0
1
0
0
0
0
0
0
1
minus
0
0
You should have
0
0
1
Don’t worry if you didn’t get it all... We will be looking at
another method next week!
Subtracting in hex is easier ...
4096’s 256’s 16’s
A
9
4
4
6
5
1’s
F
6
F or 15 - 6 = 9
9
A or 10 - 4 = 6
9-4=5
That was pretty straightforward!
But what about....
2 bytes (16 bits) of chocolate!
4096’s 256’s 16’s 1’s
1 0
4 319 3 2 0 16
18
9
7
C
A
9 from 3 can’t do!
“Borrow” 16 from
the next column 19 less 9 = 10 or
A
B
4
7 from 2 can’t do!
“Borrow” 16 from
the next column 18 less 7 = 11 or
B
C or 12 from 0
can’t do!
“Borrow” 16 from
the next column 16 less 12 = 4
One for you to try....
4096’s 256’s 16’s
1
F
3
C
7
1
2
C
Did you get 12C7h?
1’s
A
3
7
Summary.






When working in binary, we use only the digits 0 and 1!
When an addition becomes 2 or more, one has to be
carried to the column on the left.
When a subtraction cannot be done, one has to be
borrowed from the column on the left.
When working in hex, we use the digits 0 - F.
When an addition becomes 16 or more, one has to be
carried to the column on the left.
When a subtraction cannot be done, 16 has to be
borrowed from the column on the left.