understanding computers


PhizzyB COMPUTERS
Understanding Computers
By Clive  Max Maxfield and Alvin Brown
Bonus Article: Signed and Unsigned Binary Numbers
Welcome to this bonus article, which augments Part 3 of our PhizzyB Computers series (Part 3 ap-
peared in the January 1999 issue of EPE Online) . These articles are of interest to anyone who wants to
know how computers perform their magic, because they use a unique mix of hardware and software to
explain how computers work in a fun and interesting way.
This series doesn't assume any great technical knowledge, although an understanding of fundamental
electronic concepts would certainly be an advantage. You do need, though, to have had some experience
at assembling components onto a printed circuit board. You should also be moderately familiar with using
a PC-compatible computer.
The term  binary digit is engineers do have a sense of
BITS AND TRITS
usually abbreviated to  bit. As humor (just not a very sophisti-
As we know, today s digital
we discussed in earlier articles, cated one).
computers are composed of
the PhizzyB s databus is 8-bits
In the early days of comput-
large numbers of simple logic
wide. For a number or reasons,
ing, everyone pretty much
gates. As fate would have it, it s
groupings of 8-bits are common
made up their own definitions
relatively easy to create logic
in computers, so they are given
and standards, so different com-
gates that can generate, and
the special name of  byte,
panies felt free to use the term
respond to, two distinct voltage
while 4-bit groups are given the
byte to refer to 6-bit, 7-bit, 8-bit,
levels. For this reason, digital
name  nybble (or  nibble ).
and even 9-bit  chunks of data.
computers internally use the
Thus, two nybbles make a byte,
More recently, however,  byte
binary (base-2) number system.
which shows that computer
has come to be universally
accepted as referring only to
an 8-bit value.
Also for your interest, some
experimental work has been
performed with tertiary logic,
which refers to logic gates that
can generate, and respond to,
three distinct voltage levels.
The digits used in the tertiary
(base-3) number system are
called  trits. Working with ter-
tiary systems makes one s brain
ache, so you can be thankful
that we re not going to discuss
these concepts any further here.
The PhizzyB Simulator interface,
which simulates a real PhizzyB
interconnected by ribbon cables to a
variety of expansion boards.
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 1
Maxfield & Montrose Interactive Inc
Special Projects
Suffice it to say that all of
27 = 128's column
today s existing systems are
26 = 64's column
based on binary logic and, for a
25 = 32's column
number of reasons, we aren t
24 = 16's column
going to see commercial sys-
23 = 8's column
tems based on tertiary logic any
time soon, for which we can all
22 = 4's column
be truly thankful. (Logic gates,
21 = 2's column
alternative number systems,
20 = 1's column
and a rather good seafood
gumbo recipe are presented in
detail in our book Bebop to the
bit bit bit bit bit bit bit bit
Boolean Boogie (An Unconven-
7 6 5 4 3 2 1 0
tional Guide to Electronics),
which is available from the
EPE Online Store.
Most-significant bit (MSB) Least-significant bit (LSB)
UNSIGNED BINARY
Fig.1. The PhizzyB s 8-bit bus can be used to represent
unsigned binary numbers.
NUMBERS
A single bit can be used to
One interesting point to ponder Due to the fact that these
represent two values: 0 and 1.
is that these values are really values are all positive, we refer
Two bits can be used to repre-
just different patterns of 0s and to this form of representation as
sent 22 = 2 x 2 = 4 values: 00,
1s, and we can use these pat-  unsigned binary numbers. An-
01, 10, and 11. Three bits can
terns to represent anything we other way to view an unsigned
be used to represent 23 = 2 x 2
wish, including letters of the al- binary number is that each col-
x 2 = 8 values, and so forth.
phabet, colors of the spectrum, umn has a different  weight as-
Thus, the PhizzyB s 8-bit
musical notes, and so forth. sociated with it, where these
databus can be used to repre-
 weights are different powers of
Of course we often want to
sent 28 = 256 different values.
two. So bit 0 represents the
use these patterns to represent
one s column, bit 1 represents
numbers. In this case there are
Many people find working
the two s column, bit 2 repre-
a variety of schemes we might
with binary numbers a little
sents the four s column, and so
use, and one common tech-
difficult at first, but it s really
forth (Fig.1).
nique is to use our 256 patterns
rather easy. For example, when
of 0s and 1s to represent Thus, it s easy to convert a
we were young we all had to
positive values in the range binary value into its decimal
learn our  twelve times tables,
0 to 255: equivalent by simply combining
which took quite a lot of effort
each bit with its associated col-
when you come to look back on
Binary Decimal
umn weight. For example, an
it. In the case of unsigned
00000000 0
unsigned binary value of
binary numbers, the multipli-
00000001 1
11001010 equates to (1 × 128)
cation table is as follows:
00000010 2
+ (1 × 64) + (0 × 32) + (0 × 16)
00000011 3
0 × 0 = 0
+ (1 × 8) + (0 × 4) + (1 × 2)
00000100 4
0 × 1 = 0
+ (0 × 1) = 202 in decimal. Note
00000101 5
1 × 0 = 0 that the left-most bit is referred
00000110 6
to as the most-significant bit
1 × 1 = 1
00000111 7
(MSB), because it represents
00001000 8
And that s it. You ve
the largest (most significant)
: :
essentially learned the entire
11111011 251 value. Similarly, the right-most
binary multiplication table.
11111100 252 bit is referred to as the least-
Phew! Perhaps you d better
11111101 253
significant bit (LSB), because
have a cup of hot, sweet tea to
11111110 254
& & well we ll leave you to work
steady your nerves!
11111111 255
this one out for yourself.
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 2
Maxfield & Montrose Interactive Inc
Special Projects
quite simple, and with just a
BBBBBBBB
ADDING BINARY
little practice you ll be a
iiiiiiii
NUMBERS tttttttt  black belt.
76543210
Before we tackle binary,
let s first remind ourselves as to
00110010 (50)
EXPERIMENT 1:
how we add decimal numbers
+00110100 (52)
Additions and the carry flag
together, for example:
=01100110 (102)
Start up your PhizzyB Simu-
635
lator, activate the assembler,
+272
First we add 0 + 0 in the
and enter the program shown
bit 0 position (the right-most
in listing 1.
=907
column) to give 0. Next we add
As we see, this is really a
1 + 0 in the bit 1 column to give
We commence with the
rather simple program. Follow-
1, followed by 0 + 1 in the bit 2
least-significant digits (the right-
ing our constant label declara-
column to give 1, followed in
most column) and add the 5
tions, the first thing we do is to
turn by 0 + 0 in the bit 3 column
and 2 to give 7, so no major
load the accumulator with what-
to give 0 again. Things start to
surprises here.
ever value is on the 8-bit switch
get a little more interesting
device connected to the input
Next we add the 3 and 7
when we add 1 + 1 in the bit 4
port at address $F010. Next we
from the next column, which
column, because this gives us
add the whatever value is being
equals 10, and which we think
10 in binary (2 in decimal),
presented to the external input
of as a result of  0 with a  1 to
which we can think of as a
port at address $F011 to the
carry forward to the next col-
result of  0 with a  1 to carry
current contents of the accumu-
umn.
forward to the next column.
lator. Then we store the result to
Similarly, when we come to
So when we get to the third
the output port driving the 8-bit
the bit 5 column, we now have
column we add the 6 and 2 and
LED device at address $F030
1 + 1 + 1 (where the last 1 was
the 1 carried from the previous
and also to the external output
the carry from the previous
column to give a result of 9.
port at address $F031. Finally,
column). This gives us 11 in
we jump back to the label
binary (3 in decimal), which
Well things work almost ex-
LOOP and do the whole thing
we can think of as a result of
actly the same in binary, in that
all over again.
 0 with a  1 to carry forward
we start with the right-most col-
to the bit 6 column. Use the assembler s File ->
umn and work our way to the
Save As command to save this
left. The only difference is that
Although this may seem
file as baexp1.asm, then use
we have fewer digits to play
tricky the first time you see it,
the File -> Assemble command
with. To see how this goes,
the thing to remember is that
let s look at a simple example: to generate the corresponding
binary mathematics is really
Listing 1
INPORT0: .EQU $F010 #Assign a label to input port $F010
INPORT1: .EQU $F011 #Assign a label to input port $F011
OUTPORT0: .EQU $F030 #Assign a label to output port $F030
OUTPORT1: .EQU $F031 #Assign a label to output port $F031
.ORG $4000 #Set start address to $4000
LOOP: LDA [INPORT0] #Load accumulator from I/P port 0
ADD [INPORT1] #Add value from I/P port 1
STA [OUTPORT0] #Store result to O/P port 0
STA [OUTPORT1] #Store same value to O/P port 1
JMP [LOOP] #Jump to LOOP and do it all again
.END
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 3
Maxfield & Montrose Interactive Inc
Special Projects
Use your mouse to How can this be? Surely
select the binary field on adding 234 to 161 should result
the external input device in 395, not the 139 indicated
(the binary field is the one by the simulator!
at the top, and is indicated
The problem is that our 8-bit
by the % character).
accumulator can only represent
Enter the binary value unsigned binary values in the
00000001 and click the range 00000000 to 11111111
device s Set button, which (0 to 255 in decimal), so it
formally presents this simply can t hold a value of
value to the simulator s 395. But all is not lost, because
input port. the CPU has a status flag called
the C ( Carry ) flag, which is
So your program is
used to indicate just such an
now adding the 00000001
eventuality.
value on the switches to
Generic input device at port $F011
the 00000001 value on Click the simulator s St
the external port, giving a ( Step ) button to drop down
baexp1.ram machine code file.
grand result of 00000010 (or into step mode, and note the
Now click the simulator s Power
two in decimal). values on the status register
button to power everything up,
(the one annotated with
Feel free to experiment with
use the Memory -> Load RAM
 IONCZ ). Observe that both the
other values, but for the mo-
command to load baexp1.ram
N ( Negative ) and C ( Carry )
ment only apply values in the
into the simulator s memory,
flags are active (lit).
range 00000000 to 01111111
and click the Ru ( Run ) button
(0 to 127 in decimal) to both the From last month s article we
to set the program running.
8-bit switch and the external know that the N flag is simply a
Your program is now racing
input device. Adding these copy of the most-significant bit
around doing its funky thing.
values together will return re- (bit 7) in the accumulator (more
The reason nothing appears to
sults in the range 00000000 to on this later). But it s the C flag
be happening is that you re cur-
11111110 (0 to 254 in decimal). that s of interest here, because
rently adding 0 to 0 to give 0,
the fact that it is lit reflects the
Now set the 8-bit switch to
which is intrinsically somewhat
fact that a carry condition has
11101010 (234 in decimal) and
boring.
occurred.
set the binary field of the exter-
Click the right-most switch
nal input port to 10100001 (161 What does this mean?
on the 8-bit switch device. The
in decimal)  don t forget to Well, in the case of an ADD
resulting pattern of 00000001
click the port s Set button. instruction, the carry flag can
on the switches corresponds to
Eeeeek! Look at the result dis- be considered to be an extra
a value of 1 in decimal. (If the
played on the 8-bit LEDs and 1-bit storage location that is
red part of the switch is towards
the external output port: attached to the end of the
the bottom of the screen, this
10001011 (139 in decimal). accumulator (Fig.2).
corresponds to a logic 0 or off.
Similarly, if the red part of the
bit bit bit bit bit bit bit bit
switch is towards the top of the
7 6 5 4 3 2 1 0
screen, this corresponds to a
logic 1 or on.)
Your program is now adding 1 1 0 0 0 1 0 1 1
this value of 1 from the switch
device to a value of 0 from the
external input port at address
Carry Accumulator
$F011, so the answer displayed
on the 8-bit LED and the exter-
Flag
nal output port is, not surpris-
ingly, 00000001 in binary or 1
Fig.2. In the case of an ADD, the carry flag can be viewed
in decimal.
as an extra bit attached to the end of the accumulator.
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 4
Maxfield & Montrose Interactive Inc
Special Projects
One way to visualize this is code file to the PhizzyB, and Set button. Note that the result-
that the carry flag represents a verify that this experiment (and ing values shown on the 8-bit
28 = 256 s column, in which the carry flag) work the same LED display and the external
case the 9-bit unsigned binary way in the real world. output port are 01001001 (73 in
value 110001011 does indeed decimal). This is obviously what
VERY IMPORTANT
equate to 395 in decimal. we d expect when subtracting
Once you ve finished playing
33 from 106, so there are aren t
Another way to view this is
with the real PhizzyB, it s very
any surprises here, or are
that the carry flag represents an
important that you reset it
there ......?
error condition. That is, a 0 in
and then close down the
the carry flag following an ADD Click the simulator s St
PBLink utility. This is be-
instruction indicates that the ( Step ) button, which drops into
cause the PBLink spends a
ADD was performed without any the step mode and activates all
lot of time bouncing signals
problems, while a 1 in the carry of the displays. Observe that
back and forth to the
flag indicates that the result the status register s C ( Carry )
PhizzyB, which can signifi-
from the ADD was too big to fit flag is lit indicating a 1 value.
cantly impact the speed of
in the accumulator. Doesn t this indicate a problem?
the PhizzyB Simulator.
Well actually it doesn t, because
In the same way that we
the carry flag is used to repre-
have conditional jump instruc-
sent  borrow conditions in the
tions for the N and Z flags, we
EXPERIMENT 2:
case of subtraction operations,
also have the JC ( jump if
Subtractions and the
and borrows work in the oppo-
carry ) and JNC ( jump if not
borrow flag site way to carries. Thus, a bor-
carry ) instructions, which can
row=1 indicates that our SUB
be used to test the state of the The good news is that - like
instruction was performed
carry flag and then jump (or not) every other computer on the
without any problems.
accordingly. face of the planet -- the PhizzyB
Now click the simulator s Ru
doesn t actually have a borrow
In addition to the ADD
( Run ) button to return us to the
flag. The bad news is that the
instruction, which adds a new
run mode, then exchange the
carry flag doubles as a borrow
value to whatever value is cur-
values on the 8-bit switches and
in the case of subtractions.
rently stored in the accumulator,
the external input device. That
we also have an ADDC ( add
Use the assembler to open
is, set the 8-bit switches to a
with carry ). Like the ADD, the your original baexp1.asm file,
value of 00100001 (33 in deci-
ADDC also adds a new value to save this out as baexp2.asm,
mal) and the external input de-
whatever value is currently
change the ADD instruction to
vice to a value of 01101010
stored in the accumulator, but it
a SUB (don t forget to change
(106 in decimal). So our pro-
also adds whatever value is cur- the comment), and assemble
gram is now attempting to sub-
rently held in the carry flag (the
this program to generate a
tract 106 from 33. What do you
value in the carry flag is then
baexp2.ram machine code file.
expect the result to be? Well, in
overwritten with the result from
It should come as no great sur-
the real world we might hope for
the ADDC). The ADDC instruc- prise to learn that our program
something vaguely in the region
tion is of use in multi-byte addi- now loads the accumulator with
of -73, but instead our 8-bit LED
tions (see also the Further
whatever value is on the 8-bit
display and external output de-
Reading section at the end of switches and then subtracts
vice are unashamedly display-
this article). whatever value is on the
ing 10110111 (183 in decimal).
external input port.
If you have built a real
So what s the problem?
PhizzyB (as described in the Load the contents of
Well earlier we stated that un-
November 1998 issue), and baexp2.ram into the simulator s
signed binary numbers can only
also the simple input and output memory and then set this pro-
be used to represent positive
devices discussed by Alan Win- gram running. Now set the 8-bit
values. But we re currently try-
stanley in the December 1998 switches to 01101010 (106 in
ing to subtract a  bigger value
issue, then this would be a good decimal) and set the binary field
from a  smaller one, and this
time to click the Rs ( Reset ) on the external input device to
generates a negative result that
button on the simulator, down- 00100001 (33 in decimal) 
we simply can t represent using
load your baexp1.ram machine don't forget to click the device s
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 5
Maxfield & Montrose Interactive Inc
Special Projects
Sign bit (0 = +ve, 1 = -ve)
our unsigned format. Just to
prove that this is the problem,
26 = 64's column
click the simulator s St ( Step )
25 = 32's column
button to return us to the step
24 = 16's column
mode, and observe that the
carry flag (acting as a borrow) is
23 = 8's column
off, thereby indicating that there
22 = 4's column
was a problem with the result
21 = 2's column
from this subtraction. (Once
20 = 1's column
again we ll ignore the N flag for
the nonce.) When you re ready
to proceed, click the simulator s
bit bit bit bit bit bit bit bit
Rs ( Reset ) button.
7 6 5 4 3 2 1 0
As before, if you have a real
PhizzyB, now would be a good
time to repeat this experiment
in the real world.
Most-significant bit (MSB) Least-significant bit (LSB)
The PhizzyB supports both Fig.3. In a sign-magnitude representation, the MSB
SUB and SUBC instructions. In
could be used to represent the sign of the number
addition to subtracting a byte
from the current contents in the
positive). This is known as a it intrinsically easy to under-
accumulator, the SUBC also
 sign-magnitude representa- stand. But there are significant
subtracts the current value in
tion, because it comprised both disadvantages, including the
the carry flag.
a sign and a magnitude (value). fact that this format supports
both +0 and -0 (which can be
Note that as the carry flag is
So one possibility would be
a real pain), and also that we
acting as a borrow in the case
to use one of our bits to indicate
would have to treat sign bits in
of a subtraction, the SUBC
the sign of a number. In the
a different manner to the other
should more properly be called
case of the PhizzyB s 8-bit
bits when performing
SUBB. Different microprocessor
databus, for example, we could
calculations.
instruction sets may use either
use bit 7 to indicate the sign of
of these mnemonics.
a value (Fig.3). Suffice it to say that some
clever computing rapscallions
NOTE that we DO NOT
put their heads together and
use this scheme  this portion
SIGNED BINARY came up with another scheme
of our discussion is intended
that we call & wait for it, wait
NUMBERS only to explore different possi-
for it &  signed binary num-
bilities. In this case, the most-
As we ve just discovered to
bers. In this case, the most
significant bit would be called
our cost, the fact that unsigned
significant bit of the PhizzyB s
the  sign bit, while the remain-
binary numbers can only repre-
8-bit databus doesn t simply
ing bits could be used to
sent positive values means that
represent a sign, but instead it
represent values in the range
we can t subtract a bigger value
represents a negative quantity
0000000 to 1111111 (0 to 127
from a smaller one if we re us-
of  128 (Fig.4).
in decimal). So if we were to
ing this format. This is obviously
decide that a 0 in the sign bit This means that the least-
somewhat limiting, so we need
indicated positive values and a significant 7 bits are used to
a cunning solution.
1 indicated negative values, represent positive values in the
When we re using decimal
then 00000001 would represent range 0000000 to 1111111
numbers, we can indicate posi-
+1, while 10000001 would (0 to 127 in decimal).
tive or negative values by sim-
represent -1.
However, a 0 in the sign bit
ply prefixing the number with a
The main advantage of this equates to (0 x  128) = 0, while
sign; for example, +33 versus -
sign-magnitude format is that it a 1 in the sign bit equates to
33 (by convention, a number
mirrors the way we work with (1 x  128) = -128. Thus, the
without a sign is assumed to be
decimal numbers, which makes final value of a signed binary
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 6
Maxfield & Montrose Interactive Inc
Special Projects
(Sign bit) - 27 = -128's column number is to commence with
the least-significant bit, copy
26 = 64's column
every bit up to and including the
25 = 32's column
first 1, and to then invert the re-
24 = 16's column
maining bits by exchanging 0s
23 = 8's column
for 1s and vice versa (Fig.6).
22 = 4's column
As we see, the two s com-
21 = 2's column
plement of a number is the neg-
20 = 1's column ative of that number; that is, the
two s complement of 01100100
(+100 in decimal) is 10011100
bit bit bit bit bit bit bit bit
(-100 in decimal). Similarly,
7 6 5 4 3 2 1 0
if we were to take the two s
complement of 10011100 (-100
in decimal), we d end up back
where we started with 01100100
Most-significant bit (MSB) Least-significant bit (LSB)
(+100 in decimal).
One huge advantage of
Fig.4. In the case of signed binary numbers,
signed binary numbers (which
the MSB actually represents a negative quantity!
are also known as  two s com-
plement numbers ) is that if we
number is calculated by adding
TWO S
wish to add two values together,
the value represented by the
we can simply perform a stan-
COMPLEMENTS
sign bit to the value of the other
dard binary addition, irrespec-
bits. For example, consider the
 Good grief! you cry,  What
tive of whether or not the values
signed binary value 11100101
raving drongo came up with this
represent positive or negative
shown in Fig.5.
scheme? Well admittedly the
quantities.
The 1 in the sign bit repre- signed binary representation
For example, consider the
does seem unduly complex the
sents -128, while the remaining
following four permutations of
first time you run into it, but
bits represent +101, so the end
adding positive and negative
there is reason behind this
result is -128 + 101 = -27. This
values together:
means that an 8-bit signed bi- apparent madness.
nary number can represent val-
00111001 (+57)
First of all, every number
ues in the range -128 to +127 + 00011110 (+30)
system has what is known as
as follows:
its  radix complement (where
= 01010111 (+87)
 radix comes from the Latin,
00000000 (0)
meaning  root ). In the case of
00000001 (1)
the binary (base-2) number sys-
00111001 (+57)
00000010 (2)
tem, the radix complement is
+ 11100010 (-30)
00000011 (3)
called the two s complement.
00000100 (4)
A simple way to generate the
= 00011011 (+27)
: two s complement of a binary
01111110 (126)
01111111 (127)
1 1 1 0 0 1 0 1
10000000 (-128)
10000001 (-127)
10000010 (-126)
10000011 (-125)
:
-128 + 101 = -27
11111100 (-4)
11111101 (-3)
Fig.5. The final value of a signed binary number is calculated
11111110 (-2)
by adding the negative (-ve) value of the sign bit to the
11111111 (-1)
positive (+ve) value of the other bits.
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 7
Maxfield & Montrose Interactive Inc
Special Projects
11000111 (-57)
discussed in greater detail in ing with unsigned values. For
+ 00011110 (+30)
our book Bebop BYTES Back example, consider the following
(An Unconventional Guide to operation assuming unsigned
= 11100101 (-27)
Computers), which is also avail- values:
able from the EPE Online Store
01010011 (+83)
at www.epemag.com.
11000111 (-57) + 00111110 (+62)
+ 11100010 (-30)
= 10010001 (+145)
EXPERIMENT 3:
= 10101001 (-87)
Obviously the carry flag
The overflow flag
In fact life gets even
isn t going to be set in this in-
One final advantage of the
sweeter, because using this
stance, because an unsigned
signed binary format is that it
signed binary format means that
8-bit value can represent num-
allows us to perform addition
we never actually have to per-
bers in the range 0 to 255,
and subtraction operations in
form a subtraction. For exam-
which means that the result of
exactly the same way as for
ple, we know that 10 - 3 = 7 in
+145 is perfectly acceptable.
unsigned binary numbers. This
decimal (or at least it did when I
But now consider what happens
means that at any particular
was a younger man), and that
if we assume signed values:
time, we [the programmers] can
another way to write this is
decide to regard a value as be- 01010011 (+83)
10 + (-3).
ing either signed or unsigned  + 00111110 (+62)
it all depends what we want to
Similarly, if we wish to
= 10010001 (-111)
use that value for at the time.
subtract a binary value B from
another value A, all we actually
Of course there is one fly in
The bit patterns are identi-
have to do is to take the two s
the ointment, but there always is
cal in both cases, but now we
complement of B and add it to
isn t there? As we already know,
have a problem, because it
A. In fact this is the way the
the carry flag is used to show
appears that 82 + 62 = -111.
CPU actually performs subtrac-
when the result from an opera-
The problem of course is that
tions in hardware  pretty cool!
tion exceeds the maximum
a signed 8-bit value can only
value that can be stored inside
represent numbers in the range
Note that radix comple-
the accumulator. But the way
-128 to +127, and adding 82
ments, diminished radix com-
the carry flag works is based on
and 62 together gives a result
plements, and complement
the assumption that we re play-
that exceeds this range.
techniques in general are
The solution used by com-
MSB LSB
puter designers is an additional
status bit called the O ( Over-
flow ) flag. This flag becomes
active if the result from an oper-
= +100
0 1 1 0 0 1 0 0 ation on signed binary values
exceeds the permitted range
(the overflow flag is generated
Copy up to and
as an exclusive-OR (XOR) of
including the first 1
any carry into and out of the
most-significant bit of the
result).
. . . . . 1 0 0
The important point to
understand is that the CPU
Invert any doesn t really have a clue what
remaining bits we re doing here. That is, the
CPU doesn t know whether
we re regarding the values as
= -128 + 28 = -100
1 0 0 1 1 1 0 0 being signed or unsigned. The
cunning part of all of this is that
Fig.6. A simple way to generate a two s complement. the Carry flag assumes we re
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 8
Maxfield & Montrose Interactive Inc
Special Projects
using unsigned binary numbers, flag the negative flag, because In our January article we
while the Overflow flag as- it s a copy of the most- noted that as binary is a base-2
sumes that the numbers are significant bit in the accumula- number system, shifting a value
signed. Thus, it s up to us [the tor. Thus, if we re considering one bit to the left has the same
programmers] to decide how we the value in the accumulator to effect as multiplying it by two.
wish to treat the result from an be signed, then the N flag is a For example, if we load the ac-
operation. Speaking of which, copy of the sign bit, and a 1 in cumulator with 00001111 (15 in
we also have JO ( jump if over- the sign bit (which causes the N decimal) and execute a SHL,
flow ) and JNO ( jump if not flag to light up) indicates a neg- the accumulator ends up con-
overflow ) conditional jump in- ative value. Phew! taining 00011110 (30 in deci-
mal). One thing we didn t men-
structions available to us.
OK, before we move
tion, however, is that this works
on, reset the simulator, down-
In order to see the Overflow
for both signed and unsigned
load this program to your
flag work, use the assembler to
binary numbers. Also, in the
PhizzyB (if you have one), and
open the baexp1.asm file we
case of signed numbers, it
repeat this experiment in the
created earlier, save this file as
works for both positive and neg-
real world (don t forget to reset
baexp3.asm, and assemble it to
ative values. For example
your PhizzyB when you ve fin-
generate the corresponding
(assuming that we re playing
baexp3.ram file. As you will re- ished).
with signed binary numbers), if
call, this program simply added
we were to load the accumula-
the value on the 8-bit switches
THE SHIFT tor with say 11100110 (-26 in
to the value on the external in-
decimal ), then shifting this
put port at address $F011. INSTRUCTIONS
value one bit to the left would
Load the baexp3.ram file
In the January 1999 issue
give 11001100 (-52 in decimal).
into the simulator s memory and
of EPE Online we introduced
Note that this only works until
set this program running. Now
the PhizzyB s rotate and shift
the value in the sign bit changes
set the 8-bit switches to a value
instructions. At that time, we
from a 1 to a 0 or vice versa, at
of 01010011 (83 in decimal)
noted that the PhizzyB supports
which point we ve just exceeded
and the external input device to
two shift instructions called
the range of signed binary
a value of 00111110 (62 in
SHL ( shift left ) and SHR
values that this 8-bit field
decimal)  don t forget to click
( shift right ). can represent.
the device s Set button.
First let s consider the SHL, Now turn your attention to
Observe that the 8-bit LEDs
which shifts the contents of the the SHR instruction, which shifts
and the external output device
accumulator one bit to the left the contents of the accumulator
display a value of 10010001 --
and shifts a 0 into the LSB of one bit to the right (Fig.7b).
note specially that the decimal
the accumulator (Figure 7a). Once again, due to the fact
field of the output display shows
a value of 145, because these
Carry flag MSB LSB
displays always assume
unsigned values (we had to
choose one or the other, and 0
we decided to use unsigned
values). However, if you now
(a) SHL
click the St ( Step ) button to
drop the simulator into its step
mode, you ll see that the O
( Overflow ) status flag is lit up,
thereby indicating that if we re
considering these values to be
signed, then an overflow condi-
(b) SHR
tion has occurred.
Also note that the N
( Negative ) flag is lit. It now be-
comes clear why we call this
Fig.7. The (a) SHL and (b) SHR instructions.
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 9
Maxfield & Montrose Interactive Inc
Special Projects
that binary is a base-2 number and easy to understand, so we for unsigned numbers or  128 to
system, shifting a value one bit decided to offer an arithmetic +127 for signed numbers).
to the right has the same effect shift right only. Note, however,
One solution to this is to use
as dividing it by two. that it s easy to replicate the
multiple bytes to represent each
effects of a logical shift right if
For example, if we were to value. For example, if we decided
you so desire, because all you
load the accumulator with to use two bytes to represent a
have to do is to follow the arith-
00011110 (30 in decimal), shift- number, we now have 16 bits
metic shift right with an AND
ing it one bit to the right would which can represent 216 different
%01111111 instruction. This
result in it containing 00001111 patterns of 0s and 1s. Thus, our
 masking operation will force
(15 in decimal). Note that a fur- 2-byte field could represent val-
the MSB of the accumulator to
ther shift to the right would re- ues in the range 0 to 65,535 for
contain 0 (while leaving the re-
sult in 00000111 (7 in decimal). unsigned numbers or  32,768 to
maining bits in the accumulator
+32,767 for signed numbers.
Obviously 15/2 equals 7.5
untouched).
in the real world, but we don t However, we now have a new
have any way to represent the problem, which is that the
SHR BUG!
remainder from this division PhizzyB s data bus, accumulator,
using the unsigned and signed Sad to relate, reader Don and instruction set are geared up
representations we ve discussed McBrien in Ireland has rooted out to process 1-byte chunks of data.
thus far, so the result is auto- a bug in the PhizzyB Simulator. Thus, we have to perform multi-
matically truncated to the inte- Much to our embarrassment, Don byte additions byte-by-byte.
ger portion only. (However, the
discovered that instead of per- Similarly for subtractions and
carry flag will now contain the other mathematical operations.
forming an  arithmetic shift (in
1 that  fell off the end, and we
which the MSB is copied back
If you use the File -> Open
could use this to detect that
into itself), the simulator s SHR
command on the PhizzyB s as-
this condition had occurred.)
actually performs a  logical shift,
sembler, you ll see a set of files
which means that a 0 is shifted
However, the really interest-
called add16.asm, sub16.asm,
into the MSB.
ing point about our SHR instruc-
and so forth. These files contain
tion is the fact that we shift a
Happily, the real PhizzyB the source code for multi-byte
copy of the MSB of the accumu-
does perform an arithmetic shift arithmetic operations.
lator back into itself. This is
as documented above, but this
In fact, we ll be using one or
known as an  arithmetic shift
doesn t alter the fact that the
two of these subroutines in our
right, because it s intended to
actions of the simulator and the
PhizzyB article in the February
support arithmetic operations on
PhizzyB differ in this regard.
1999 issue of EPE Online. In the
signed binary numbers. For ex-
However all is not lost, because
meantime, if you want to discover
ample, suppose that the accu-
we have a cunning plan (we ll call
more about these little rascals,
mulator originally contained
this  Plan A so that no one gets
you ll find that they are discussed
11001100 (-52 in decimal). In
confused), which is documented
in detail in our book Bebop
this case, shifting the accumula-
at the PhizzyB web site:
BYTES Back (An Unconventional
tor one bit to the right using the
www.maxmon.com/phizzyb Guide to Computers), which can
PhizzyB s SHR instruction will
be purchased from the EPE On-
result in 11100110 (-26 in deci-
line Store at www.epemag.com.
mal), and this only works be-
cause the accumulator s MSB
FURTHER
is copied back into itself.
READING
Some CPUs support an-
other type of shift right called a
Earlier in this article we
 logical shift right, which means
mentioned  multi-byte addi-
that the shift causes the MSB to
tions. What did we mean
be loaded with a 0 (some CPUs
by this? Well one of the prob-
supports both logical and arith-
lems we face is that the range
metic shifts). In the case of the
of numbers we can represent
PhizzyB we wanted to keep the
with the PhizzyB s 8-bit ACC is
instruction set relatively small
somewhat restricted (0 to 255
Copyright © 1998 Wimborne Publishing Ltd and
EPE Online, January 1999 - www.epemag.com - 10
Maxfield & Montrose Interactive Inc


Wyszukiwarka

Podobne podstrony:
Advantages and disadvantages of computers
Computer Emulatoren Fremdsysteme auf dem PC nachgebildet
Dorst GA the Framework 4 Geom Computing (2002) [sharethefiles com]
2008 11 Maximum Math Free Computer Algebra with Maxima
Computerspieler Jargon
trip computer
5 Computer Aided Manufacturing jego funkcje
understanding peerpoints
Computers and Biotechnology Gałkowski
trip computer
Stupid Computer Buyers
understanding switching?sics
Door & Legs Computer Desk
Understanding and Managing Polymorphic Viruses
trip computer
understanding peerpoints
Keyboard Umschalter Eine Tastatur fuer viele Computer
understanding peerpoints

więcej podobnych podstron