Home » CEEFAX disks » telesoftware3.adl » 01_11_87/T\OSB02

01_11_87/T\OSB02

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » CEEFAX disks » telesoftware3.adl
Filename: 01_11_87/T\OSB02
Read OK:
File size: 1ED6 bytes
Load address: 0000
Exec address: 0000
Duplicates

There is 1 duplicate copy of this file in the archive:

File contents
OSBITS - An Exploration of the BBC Micro at Machine Level

by Programmer

............................................................


Part 2:  Counting Two By Two


Computers count in twos.  Just like we have ten fingers and
count in tens an electrical circuit in a computer is either
on (called set) or off (called clear).  An individual
circuit represents a binary digit, called a bit, and the
6502 microprocessor inside the BBC Micro works with numbers
made up of eight bits, each called a byte.

These eight bits of a byte give us a method of representing
a number by building it from powers of 2 .... like this

Bit      7      6      5      4      3      2      1      0
Value  128     64     32     16      8      4      2      1

By having each bit of this byte either set (on) or clear
(off) you can represent any number between 0 and 255 (which
is 2^8 -1).

This way the binary number 1001 equals 9 in decimal
(2^3+2^0) and 101101 is equal to 45 (2^5+2^3+2^2+2^0).

Several bytes can be strung together to make a word.  For
integers BBC BASIC has words that are 32 bits long.  You
might say that BBC BASIC calculates integers using 32 bit
words while the 6502 microprocessor in it calculates in 8
bit words.  There are microprocessors that work with 16 and
even 32 bit words.  (Hence all the talk of '16 bit
technology' etc.)

With a 6502 words are made by stringing bytes together with
the so-called Least Significant Byte (LSB) in the lowest
address and the Most significant Byte (MSB) in the highest. 
As an example, in memory it would look like this:

      Address           Number stored in this byte
       &2173                     13
       &2172                      5
       &2171                    126
       &2170                     99

So the value stored in the 32 bit (4 byte) word starting at
address &2170 is 99 + 256*126 + 256*256*5 + 256*256*256*13. 
In BBC BASIC you would look at !&2170 to get this number. 
As you can see, just as each higher, and more significant,
bit is 2 times the value of its neighbour so each more
significant byte is 2^8 or 256 times the value of its
neighbour.

Two things to notice about this table.  Firstly the lower
memory addresses are at the bottom and secondly the
addresses are given in hexadecimal (hex).  The ampersand (&)
tells us, and the computer, that the number is in hex.  Both
of these are conventions for the BBC Micro. Other computers
may use other conventions.

Hexadecimal is numbers with a base of 16 (decimal is base 10
and binary base 2).  Hex gives us a neat shorthand for the
values of bytes since one hex digit represents four bits.
Four bits can hold any value between 0 and 1111 (binary) or
0 and 15 (decimal) which is between 0 and F in hex.  A full
byte, holding 255 in decimal is FF in hex.  We use the
numbers from 0 to 9 and the letters A to F as the 16 numbers
in hex.

Once we get past arithmetic you will find it increasingly
useful to think in hex when writing assembler.

That's how the numbers are written down, so what can we do
with them?

Addition is straightforward.  Adding 1 to 0 or 0 to 1
produces 1 and adding 0 to 0 produces 0.  Adding 1 to 1
produces 10 since 1 and 1 is 0 carry 1 and we add that carry
to 0 to give the left hand 1.

                    10111010
                    11010111
                   ---------
                   110010001

Now our two numbers, 186 and 215, both fit in a byte because
they are less than 256.  But 186+215 does not fit and, as
you can see, it has overflowed to the right.  If our
calculations only used one byte that overflow would be lost
and 186+215 would produce only 145.  This idea of carrying
over from a byte addition and finally overflowing your word
is very important.

There is an additional complication caused by our need for
negative numbers.  The computer doesn't understand a
negative number, a number is either there or it isn't, so we
have to invent a convention.  This uses the top bit of your
word to indicate whether the number is negative or not.  If
we apply this to a byte you get this result:

+ve numbers  0 to  127 are &00 to &7F (00000000 to 01111111)
-ve numbers -1 to -128 are &FF to &80 (11111111 to 10000000)

So negative numbers have their top bit set.  Note that
zero is positive.  There is method in this seeming madness
since addition automatically takes account of the signs of
the numbers being added, and the same happens with
subtraction.  In fact the overflow out of the word helps
with subtraction.

Let's add -1 and -1, which gives -2:

                     11111111       &FF
                     11111111       &FF
                     --------       ---
                     11111110       &FE

There is a way of checking that you don't accidentally turn
a byte negative by adding together two positive numbers both
over 64, that is something called the overflow flag.  A
carry, which you can think of as being the '9th bit' of a
byte, sets another flag called the carry flag.  (More on
flags in another module.)

The relationship between a number and its negative is that
each bit of the number is reversed (or inverted, 0 becomes 1
and 1 becomes 0) and then one is added to it.  This means
that 1 (00000001) becomes 11111110 + 1 = 11111111.  When you
invert the bits of a number you are said to be taking its
complement and when you add one you get its 2's complement.

Subtraction is carried out similarly to addition.  If you
take 0 from 0 you get 0, 0 from 1 is 1 and 1 from 1 is 0. 
To take 1 from 0 you borrow from the next highest bit and so
take 1 from 10, which is 1.

                      10100101
                    - 01110110
                      --------
                      00101111

It's more difficult to get the hang of binary subtraction
than addition but, in the end, it doesn't really matter. 
Apart, that is, from the idea of borrowing since it is this
that enables you to subtract numbers larger than a single
byte.  Carrying does this job in addition and in both cases
the carry flag is used.

To add two bytes (stored in the addresses labelled byte_1 and
byte_2) and store the result at address byte_3 you do this:

CLear the Carry flag             CLC
LoaD A with the first number     LDA byte_1
ADd the second (with Carry)      ADC byte_2
STore the result (from A)        STA byte_3

A, the accumulator, is the part of the microprocessor where
you do your arithmetic.  ADC adds a number to the value in
the accumulator and adds the value of the carry flag to
that.  The result is in the accumulator.

To subtract 2 bytes you have to start by setting the carry
flag:

SEt Carry                        SEC
LoaD A with the first number     LDA byte_1
SuBtract the second (with Carry) SBC byte_2
STore the result (from A)        STA byte_3

In both cases, if you are adding or subtracting numbers made
up of many bytes, you only explicitly clear or set the carry
at the beginning.  After that it serves its proper purpose
and carries across or borrows across if needed.  Look at
this weeks assembler program B/osb02 which adds and
subtracts two four byte numbers.  I have used bits of BASIC
to enable you to get the numbers into and out of the machine
but eventually we will use machine code to do this as well. 
We could extend that code to work with numbers of any size
but BASIC's integers are only 4 bytes in size so we will
stick to 4 bytes for the moment.

Note that B/osb02 will give an incorrect result if you add
together numbers such that their sum is greater than
2147483647 (and similarly for negative numbers).  This is
because the overflow between the top two bits is not handled
correctly.  BASIC would trap this as a Too Big error but
this machine code does not.  To do so uses another of the
microprocessor's flags called the overflow flag.

We'll look at the flags in more detail next week and use
them to get the computer to make some decisions.
00000000  4f 53 42 49 54 53 20 2d  20 41 6e 20 45 78 70 6c  |OSBITS - An Expl|
00000010  6f 72 61 74 69 6f 6e 20  6f 66 20 74 68 65 20 42  |oration of the B|
00000020  42 43 20 4d 69 63 72 6f  20 61 74 20 4d 61 63 68  |BC Micro at Mach|
00000030  69 6e 65 20 4c 65 76 65  6c 0d 0d 62 79 20 50 72  |ine Level..by Pr|
00000040  6f 67 72 61 6d 6d 65 72  0d 0d 2e 2e 2e 2e 2e 2e  |ogrammer........|
00000050  2e 2e 2e 2e 2e 2e 2e 2e  2e 2e 2e 2e 2e 2e 2e 2e  |................|
*
00000080  2e 2e 2e 2e 2e 2e 0d 0d  0d 50 61 72 74 20 32 3a  |.........Part 2:|
00000090  20 20 43 6f 75 6e 74 69  6e 67 20 54 77 6f 20 42  |  Counting Two B|
000000a0  79 20 54 77 6f 0d 0d 0d  43 6f 6d 70 75 74 65 72  |y Two...Computer|
000000b0  73 20 63 6f 75 6e 74 20  69 6e 20 74 77 6f 73 2e  |s count in twos.|
000000c0  20 20 4a 75 73 74 20 6c  69 6b 65 20 77 65 20 68  |  Just like we h|
000000d0  61 76 65 20 74 65 6e 20  66 69 6e 67 65 72 73 20  |ave ten fingers |
000000e0  61 6e 64 0d 63 6f 75 6e  74 20 69 6e 20 74 65 6e  |and.count in ten|
000000f0  73 20 61 6e 20 65 6c 65  63 74 72 69 63 61 6c 20  |s an electrical |
00000100  63 69 72 63 75 69 74 20  69 6e 20 61 20 63 6f 6d  |circuit in a com|
00000110  70 75 74 65 72 20 69 73  20 65 69 74 68 65 72 0d  |puter is either.|
00000120  6f 6e 20 28 63 61 6c 6c  65 64 20 73 65 74 29 20  |on (called set) |
00000130  6f 72 20 6f 66 66 20 28  63 61 6c 6c 65 64 20 63  |or off (called c|
00000140  6c 65 61 72 29 2e 20 20  41 6e 20 69 6e 64 69 76  |lear).  An indiv|
00000150  69 64 75 61 6c 0d 63 69  72 63 75 69 74 20 72 65  |idual.circuit re|
00000160  70 72 65 73 65 6e 74 73  20 61 20 62 69 6e 61 72  |presents a binar|
00000170  79 20 64 69 67 69 74 2c  20 63 61 6c 6c 65 64 20  |y digit, called |
00000180  61 20 62 69 74 2c 20 61  6e 64 20 74 68 65 0d 36  |a bit, and the.6|
00000190  35 30 32 20 6d 69 63 72  6f 70 72 6f 63 65 73 73  |502 microprocess|
000001a0  6f 72 20 69 6e 73 69 64  65 20 74 68 65 20 42 42  |or inside the BB|
000001b0  43 20 4d 69 63 72 6f 20  77 6f 72 6b 73 20 77 69  |C Micro works wi|
000001c0  74 68 20 6e 75 6d 62 65  72 73 0d 6d 61 64 65 20  |th numbers.made |
000001d0  75 70 20 6f 66 20 65 69  67 68 74 20 62 69 74 73  |up of eight bits|
000001e0  2c 20 65 61 63 68 20 63  61 6c 6c 65 64 20 61 20  |, each called a |
000001f0  62 79 74 65 2e 0d 0d 54  68 65 73 65 20 65 69 67  |byte...These eig|
00000200  68 74 20 62 69 74 73 20  6f 66 20 61 20 62 79 74  |ht bits of a byt|
00000210  65 20 67 69 76 65 20 75  73 20 61 20 6d 65 74 68  |e give us a meth|
00000220  6f 64 20 6f 66 20 72 65  70 72 65 73 65 6e 74 69  |od of representi|
00000230  6e 67 0d 61 20 6e 75 6d  62 65 72 20 62 79 20 62  |ng.a number by b|
00000240  75 69 6c 64 69 6e 67 20  69 74 20 66 72 6f 6d 20  |uilding it from |
00000250  70 6f 77 65 72 73 20 6f  66 20 32 20 2e 2e 2e 2e  |powers of 2 ....|
00000260  20 6c 69 6b 65 20 74 68  69 73 0d 0d 42 69 74 20  | like this..Bit |
00000270  20 20 20 20 20 37 20 20  20 20 20 20 36 20 20 20  |     7      6   |
00000280  20 20 20 35 20 20 20 20  20 20 34 20 20 20 20 20  |   5      4     |
00000290  20 33 20 20 20 20 20 20  32 20 20 20 20 20 20 31  | 3      2      1|
000002a0  20 20 20 20 20 20 30 0d  56 61 6c 75 65 20 20 31  |      0.Value  1|
000002b0  32 38 20 20 20 20 20 36  34 20 20 20 20 20 33 32  |28     64     32|
000002c0  20 20 20 20 20 31 36 20  20 20 20 20 20 38 20 20  |     16      8  |
000002d0  20 20 20 20 34 20 20 20  20 20 20 32 20 20 20 20  |    4      2    |
000002e0  20 20 31 0d 0d 42 79 20  68 61 76 69 6e 67 20 65  |  1..By having e|
000002f0  61 63 68 20 62 69 74 20  6f 66 20 74 68 69 73 20  |ach bit of this |
00000300  62 79 74 65 20 65 69 74  68 65 72 20 73 65 74 20  |byte either set |
00000310  28 6f 6e 29 20 6f 72 20  63 6c 65 61 72 0d 28 6f  |(on) or clear.(o|
00000320  66 66 29 20 79 6f 75 20  63 61 6e 20 72 65 70 72  |ff) you can repr|
00000330  65 73 65 6e 74 20 61 6e  79 20 6e 75 6d 62 65 72  |esent any number|
00000340  20 62 65 74 77 65 65 6e  20 30 20 61 6e 64 20 32  | between 0 and 2|
00000350  35 35 20 28 77 68 69 63  68 0d 69 73 20 32 5e 38  |55 (which.is 2^8|
00000360  20 2d 31 29 2e 0d 0d 54  68 69 73 20 77 61 79 20  | -1)...This way |
00000370  74 68 65 20 62 69 6e 61  72 79 20 6e 75 6d 62 65  |the binary numbe|
00000380  72 20 31 30 30 31 20 65  71 75 61 6c 73 20 39 20  |r 1001 equals 9 |
00000390  69 6e 20 64 65 63 69 6d  61 6c 0d 28 32 5e 33 2b  |in decimal.(2^3+|
000003a0  32 5e 30 29 20 61 6e 64  20 31 30 31 31 30 31 20  |2^0) and 101101 |
000003b0  69 73 20 65 71 75 61 6c  20 74 6f 20 34 35 20 28  |is equal to 45 (|
000003c0  32 5e 35 2b 32 5e 33 2b  32 5e 32 2b 32 5e 30 29  |2^5+2^3+2^2+2^0)|
000003d0  2e 0d 0d 53 65 76 65 72  61 6c 20 62 79 74 65 73  |...Several bytes|
000003e0  20 63 61 6e 20 62 65 20  73 74 72 75 6e 67 20 74  | can be strung t|
000003f0  6f 67 65 74 68 65 72 20  74 6f 20 6d 61 6b 65 20  |ogether to make |
00000400  61 20 77 6f 72 64 2e 20  20 46 6f 72 0d 69 6e 74  |a word.  For.int|
00000410  65 67 65 72 73 20 42 42  43 20 42 41 53 49 43 20  |egers BBC BASIC |
00000420  68 61 73 20 77 6f 72 64  73 20 74 68 61 74 20 61  |has words that a|
00000430  72 65 20 33 32 20 62 69  74 73 20 6c 6f 6e 67 2e  |re 32 bits long.|
00000440  20 20 59 6f 75 0d 6d 69  67 68 74 20 73 61 79 20  |  You.might say |
00000450  74 68 61 74 20 42 42 43  20 42 41 53 49 43 20 63  |that BBC BASIC c|
00000460  61 6c 63 75 6c 61 74 65  73 20 69 6e 74 65 67 65  |alculates intege|
00000470  72 73 20 75 73 69 6e 67  20 33 32 20 62 69 74 0d  |rs using 32 bit.|
00000480  77 6f 72 64 73 20 77 68  69 6c 65 20 74 68 65 20  |words while the |
00000490  36 35 30 32 20 6d 69 63  72 6f 70 72 6f 63 65 73  |6502 microproces|
000004a0  73 6f 72 20 69 6e 20 69  74 20 63 61 6c 63 75 6c  |sor in it calcul|
000004b0  61 74 65 73 20 69 6e 20  38 0d 62 69 74 20 77 6f  |ates in 8.bit wo|
000004c0  72 64 73 2e 20 20 54 68  65 72 65 20 61 72 65 20  |rds.  There are |
000004d0  6d 69 63 72 6f 70 72 6f  63 65 73 73 6f 72 73 20  |microprocessors |
000004e0  74 68 61 74 20 77 6f 72  6b 20 77 69 74 68 20 31  |that work with 1|
000004f0  36 20 61 6e 64 0d 65 76  65 6e 20 33 32 20 62 69  |6 and.even 32 bi|
00000500  74 20 77 6f 72 64 73 2e  20 20 28 48 65 6e 63 65  |t words.  (Hence|
00000510  20 61 6c 6c 20 74 68 65  20 74 61 6c 6b 20 6f 66  | all the talk of|
00000520  20 27 31 36 20 62 69 74  0d 74 65 63 68 6e 6f 6c  | '16 bit.technol|
00000530  6f 67 79 27 20 65 74 63  2e 29 0d 0d 57 69 74 68  |ogy' etc.)..With|
00000540  20 61 20 36 35 30 32 20  77 6f 72 64 73 20 61 72  | a 6502 words ar|
00000550  65 20 6d 61 64 65 20 62  79 20 73 74 72 69 6e 67  |e made by string|
00000560  69 6e 67 20 62 79 74 65  73 20 74 6f 67 65 74 68  |ing bytes togeth|
00000570  65 72 20 77 69 74 68 0d  74 68 65 20 73 6f 2d 63  |er with.the so-c|
00000580  61 6c 6c 65 64 20 4c 65  61 73 74 20 53 69 67 6e  |alled Least Sign|
00000590  69 66 69 63 61 6e 74 20  42 79 74 65 20 28 4c 53  |ificant Byte (LS|
000005a0  42 29 20 69 6e 20 74 68  65 20 6c 6f 77 65 73 74  |B) in the lowest|
000005b0  0d 61 64 64 72 65 73 73  20 61 6e 64 20 74 68 65  |.address and the|
000005c0  20 4d 6f 73 74 20 73 69  67 6e 69 66 69 63 61 6e  | Most significan|
000005d0  74 20 42 79 74 65 20 28  4d 53 42 29 20 69 6e 20  |t Byte (MSB) in |
000005e0  74 68 65 20 68 69 67 68  65 73 74 2e 20 0d 41 73  |the highest. .As|
000005f0  20 61 6e 20 65 78 61 6d  70 6c 65 2c 20 69 6e 20  | an example, in |
00000600  6d 65 6d 6f 72 79 20 69  74 20 77 6f 75 6c 64 20  |memory it would |
00000610  6c 6f 6f 6b 20 6c 69 6b  65 20 74 68 69 73 3a 0d  |look like this:.|
00000620  0d 20 20 20 20 20 20 41  64 64 72 65 73 73 20 20  |.      Address  |
00000630  20 20 20 20 20 20 20 20  20 4e 75 6d 62 65 72 20  |         Number |
00000640  73 74 6f 72 65 64 20 69  6e 20 74 68 69 73 20 62  |stored in this b|
00000650  79 74 65 0d 20 20 20 20  20 20 20 26 32 31 37 33  |yte.       &2173|
00000660  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000670  20 20 20 20 20 31 33 0d  20 20 20 20 20 20 20 26  |     13.       &|
00000680  32 31 37 32 20 20 20 20  20 20 20 20 20 20 20 20  |2172            |
00000690  20 20 20 20 20 20 20 20  20 20 35 0d 20 20 20 20  |          5.    |
000006a0  20 20 20 26 32 31 37 31  20 20 20 20 20 20 20 20  |   &2171        |
000006b0  20 20 20 20 20 20 20 20  20 20 20 20 31 32 36 0d  |            126.|
000006c0  20 20 20 20 20 20 20 26  32 31 37 30 20 20 20 20  |       &2170    |
000006d0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
000006e0  20 39 39 0d 0d 53 6f 20  74 68 65 20 76 61 6c 75  | 99..So the valu|
000006f0  65 20 73 74 6f 72 65 64  20 69 6e 20 74 68 65 20  |e stored in the |
00000700  33 32 20 62 69 74 20 28  34 20 62 79 74 65 29 20  |32 bit (4 byte) |
00000710  77 6f 72 64 20 73 74 61  72 74 69 6e 67 20 61 74  |word starting at|
00000720  0d 61 64 64 72 65 73 73  20 26 32 31 37 30 20 69  |.address &2170 i|
00000730  73 20 39 39 20 2b 20 32  35 36 2a 31 32 36 20 2b  |s 99 + 256*126 +|
00000740  20 32 35 36 2a 32 35 36  2a 35 20 2b 20 32 35 36  | 256*256*5 + 256|
00000750  2a 32 35 36 2a 32 35 36  2a 31 33 2e 20 0d 49 6e  |*256*256*13. .In|
00000760  20 42 42 43 20 42 41 53  49 43 20 79 6f 75 20 77  | BBC BASIC you w|
00000770  6f 75 6c 64 20 6c 6f 6f  6b 20 61 74 20 21 26 32  |ould look at !&2|
00000780  31 37 30 20 74 6f 20 67  65 74 20 74 68 69 73 20  |170 to get this |
00000790  6e 75 6d 62 65 72 2e 20  0d 41 73 20 79 6f 75 20  |number. .As you |
000007a0  63 61 6e 20 73 65 65 2c  20 6a 75 73 74 20 61 73  |can see, just as|
000007b0  20 65 61 63 68 20 68 69  67 68 65 72 2c 20 61 6e  | each higher, an|
000007c0  64 20 6d 6f 72 65 20 73  69 67 6e 69 66 69 63 61  |d more significa|
000007d0  6e 74 2c 0d 62 69 74 20  69 73 20 32 20 74 69 6d  |nt,.bit is 2 tim|
000007e0  65 73 20 74 68 65 20 76  61 6c 75 65 20 6f 66 20  |es the value of |
000007f0  69 74 73 20 6e 65 69 67  68 62 6f 75 72 20 73 6f  |its neighbour so|
00000800  20 65 61 63 68 20 6d 6f  72 65 0d 73 69 67 6e 69  | each more.signi|
00000810  66 69 63 61 6e 74 20 62  79 74 65 20 69 73 20 32  |ficant byte is 2|
00000820  5e 38 20 6f 72 20 32 35  36 20 74 69 6d 65 73 20  |^8 or 256 times |
00000830  74 68 65 20 76 61 6c 75  65 20 6f 66 20 69 74 73  |the value of its|
00000840  0d 6e 65 69 67 68 62 6f  75 72 2e 0d 0d 54 77 6f  |.neighbour...Two|
00000850  20 74 68 69 6e 67 73 20  74 6f 20 6e 6f 74 69 63  | things to notic|
00000860  65 20 61 62 6f 75 74 20  74 68 69 73 20 74 61 62  |e about this tab|
00000870  6c 65 2e 20 20 46 69 72  73 74 6c 79 20 74 68 65  |le.  Firstly the|
00000880  20 6c 6f 77 65 72 0d 6d  65 6d 6f 72 79 20 61 64  | lower.memory ad|
00000890  64 72 65 73 73 65 73 20  61 72 65 20 61 74 20 74  |dresses are at t|
000008a0  68 65 20 62 6f 74 74 6f  6d 20 61 6e 64 20 73 65  |he bottom and se|
000008b0  63 6f 6e 64 6c 79 20 74  68 65 0d 61 64 64 72 65  |condly the.addre|
000008c0  73 73 65 73 20 61 72 65  20 67 69 76 65 6e 20 69  |sses are given i|
000008d0  6e 20 68 65 78 61 64 65  63 69 6d 61 6c 20 28 68  |n hexadecimal (h|
000008e0  65 78 29 2e 20 20 54 68  65 20 61 6d 70 65 72 73  |ex).  The ampers|
000008f0  61 6e 64 20 28 26 29 0d  74 65 6c 6c 73 20 75 73  |and (&).tells us|
00000900  2c 20 61 6e 64 20 74 68  65 20 63 6f 6d 70 75 74  |, and the comput|
00000910  65 72 2c 20 74 68 61 74  20 74 68 65 20 6e 75 6d  |er, that the num|
00000920  62 65 72 20 69 73 20 69  6e 20 68 65 78 2e 20 20  |ber is in hex.  |
00000930  42 6f 74 68 0d 6f 66 20  74 68 65 73 65 20 61 72  |Both.of these ar|
00000940  65 20 63 6f 6e 76 65 6e  74 69 6f 6e 73 20 66 6f  |e conventions fo|
00000950  72 20 74 68 65 20 42 42  43 20 4d 69 63 72 6f 2e  |r the BBC Micro.|
00000960  20 4f 74 68 65 72 20 63  6f 6d 70 75 74 65 72 73  | Other computers|
00000970  0d 6d 61 79 20 75 73 65  20 6f 74 68 65 72 20 63  |.may use other c|
00000980  6f 6e 76 65 6e 74 69 6f  6e 73 2e 0d 0d 48 65 78  |onventions...Hex|
00000990  61 64 65 63 69 6d 61 6c  20 69 73 20 6e 75 6d 62  |adecimal is numb|
000009a0  65 72 73 20 77 69 74 68  20 61 20 62 61 73 65 20  |ers with a base |
000009b0  6f 66 20 31 36 20 28 64  65 63 69 6d 61 6c 20 69  |of 16 (decimal i|
000009c0  73 20 62 61 73 65 20 31  30 0d 61 6e 64 20 62 69  |s base 10.and bi|
000009d0  6e 61 72 79 20 62 61 73  65 20 32 29 2e 20 20 48  |nary base 2).  H|
000009e0  65 78 20 67 69 76 65 73  20 75 73 20 61 20 6e 65  |ex gives us a ne|
000009f0  61 74 20 73 68 6f 72 74  68 61 6e 64 20 66 6f 72  |at shorthand for|
00000a00  20 74 68 65 0d 76 61 6c  75 65 73 20 6f 66 20 62  | the.values of b|
00000a10  79 74 65 73 20 73 69 6e  63 65 20 6f 6e 65 20 68  |ytes since one h|
00000a20  65 78 20 64 69 67 69 74  20 72 65 70 72 65 73 65  |ex digit represe|
00000a30  6e 74 73 20 66 6f 75 72  20 62 69 74 73 2e 0d 46  |nts four bits..F|
00000a40  6f 75 72 20 62 69 74 73  20 63 61 6e 20 68 6f 6c  |our bits can hol|
00000a50  64 20 61 6e 79 20 76 61  6c 75 65 20 62 65 74 77  |d any value betw|
00000a60  65 65 6e 20 30 20 61 6e  64 20 31 31 31 31 20 28  |een 0 and 1111 (|
00000a70  62 69 6e 61 72 79 29 20  6f 72 0d 30 20 61 6e 64  |binary) or.0 and|
00000a80  20 31 35 20 28 64 65 63  69 6d 61 6c 29 20 77 68  | 15 (decimal) wh|
00000a90  69 63 68 20 69 73 20 62  65 74 77 65 65 6e 20 30  |ich is between 0|
00000aa0  20 61 6e 64 20 46 20 69  6e 20 68 65 78 2e 20 20  | and F in hex.  |
00000ab0  41 20 66 75 6c 6c 0d 62  79 74 65 2c 20 68 6f 6c  |A full.byte, hol|
00000ac0  64 69 6e 67 20 32 35 35  20 69 6e 20 64 65 63 69  |ding 255 in deci|
00000ad0  6d 61 6c 20 69 73 20 46  46 20 69 6e 20 68 65 78  |mal is FF in hex|
00000ae0  2e 20 20 57 65 20 75 73  65 20 74 68 65 0d 6e 75  |.  We use the.nu|
00000af0  6d 62 65 72 73 20 66 72  6f 6d 20 30 20 74 6f 20  |mbers from 0 to |
00000b00  39 20 61 6e 64 20 74 68  65 20 6c 65 74 74 65 72  |9 and the letter|
00000b10  73 20 41 20 74 6f 20 46  20 61 73 20 74 68 65 20  |s A to F as the |
00000b20  31 36 20 6e 75 6d 62 65  72 73 0d 69 6e 20 68 65  |16 numbers.in he|
00000b30  78 2e 0d 0d 4f 6e 63 65  20 77 65 20 67 65 74 20  |x...Once we get |
00000b40  70 61 73 74 20 61 72 69  74 68 6d 65 74 69 63 20  |past arithmetic |
00000b50  79 6f 75 20 77 69 6c 6c  20 66 69 6e 64 20 69 74  |you will find it|
00000b60  20 69 6e 63 72 65 61 73  69 6e 67 6c 79 0d 75 73  | increasingly.us|
00000b70  65 66 75 6c 20 74 6f 20  74 68 69 6e 6b 20 69 6e  |eful to think in|
00000b80  20 68 65 78 20 77 68 65  6e 20 77 72 69 74 69 6e  | hex when writin|
00000b90  67 20 61 73 73 65 6d 62  6c 65 72 2e 0d 0d 54 68  |g assembler...Th|
00000ba0  61 74 27 73 20 68 6f 77  20 74 68 65 20 6e 75 6d  |at's how the num|
00000bb0  62 65 72 73 20 61 72 65  20 77 72 69 74 74 65 6e  |bers are written|
00000bc0  20 64 6f 77 6e 2c 20 73  6f 20 77 68 61 74 20 63  | down, so what c|
00000bd0  61 6e 20 77 65 20 64 6f  0d 77 69 74 68 20 74 68  |an we do.with th|
00000be0  65 6d 3f 0d 0d 41 64 64  69 74 69 6f 6e 20 69 73  |em?..Addition is|
00000bf0  20 73 74 72 61 69 67 68  74 66 6f 72 77 61 72 64  | straightforward|
00000c00  2e 20 20 41 64 64 69 6e  67 20 31 20 74 6f 20 30  |.  Adding 1 to 0|
00000c10  20 6f 72 20 30 20 74 6f  20 31 0d 70 72 6f 64 75  | or 0 to 1.produ|
00000c20  63 65 73 20 31 20 61 6e  64 20 61 64 64 69 6e 67  |ces 1 and adding|
00000c30  20 30 20 74 6f 20 30 20  70 72 6f 64 75 63 65 73  | 0 to 0 produces|
00000c40  20 30 2e 20 20 41 64 64  69 6e 67 20 31 20 74 6f  | 0.  Adding 1 to|
00000c50  20 31 0d 70 72 6f 64 75  63 65 73 20 31 30 20 73  | 1.produces 10 s|
00000c60  69 6e 63 65 20 31 20 61  6e 64 20 31 20 69 73 20  |ince 1 and 1 is |
00000c70  30 20 63 61 72 72 79 20  31 20 61 6e 64 20 77 65  |0 carry 1 and we|
00000c80  20 61 64 64 20 74 68 61  74 20 63 61 72 72 79 0d  | add that carry.|
00000c90  74 6f 20 30 20 74 6f 20  67 69 76 65 20 74 68 65  |to 0 to give the|
00000ca0  20 6c 65 66 74 20 68 61  6e 64 20 31 2e 0d 0d 20  | left hand 1... |
00000cb0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000cc0  20 20 20 31 30 31 31 31  30 31 30 0d 20 20 20 20  |   10111010.    |
00000cd0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000ce0  31 31 30 31 30 31 31 31  0d 20 20 20 20 20 20 20  |11010111.       |
00000cf0  20 20 20 20 20 20 20 20  20 20 20 20 2d 2d 2d 2d  |            ----|
00000d00  2d 2d 2d 2d 2d 0d 20 20  20 20 20 20 20 20 20 20  |-----.          |
00000d10  20 20 20 20 20 20 20 20  20 31 31 30 30 31 30 30  |         1100100|
00000d20  30 31 0d 0d 4e 6f 77 20  6f 75 72 20 74 77 6f 20  |01..Now our two |
00000d30  6e 75 6d 62 65 72 73 2c  20 31 38 36 20 61 6e 64  |numbers, 186 and|
00000d40  20 32 31 35 2c 20 62 6f  74 68 20 66 69 74 20 69  | 215, both fit i|
00000d50  6e 20 61 20 62 79 74 65  20 62 65 63 61 75 73 65  |n a byte because|
00000d60  0d 74 68 65 79 20 61 72  65 20 6c 65 73 73 20 74  |.they are less t|
00000d70  68 61 6e 20 32 35 36 2e  20 20 42 75 74 20 31 38  |han 256.  But 18|
00000d80  36 2b 32 31 35 20 64 6f  65 73 20 6e 6f 74 20 66  |6+215 does not f|
00000d90  69 74 20 61 6e 64 2c 20  61 73 0d 79 6f 75 20 63  |it and, as.you c|
00000da0  61 6e 20 73 65 65 2c 20  69 74 20 68 61 73 20 6f  |an see, it has o|
00000db0  76 65 72 66 6c 6f 77 65  64 20 74 6f 20 74 68 65  |verflowed to the|
00000dc0  20 72 69 67 68 74 2e 20  20 49 66 20 6f 75 72 0d  | right.  If our.|
00000dd0  63 61 6c 63 75 6c 61 74  69 6f 6e 73 20 6f 6e 6c  |calculations onl|
00000de0  79 20 75 73 65 64 20 6f  6e 65 20 62 79 74 65 20  |y used one byte |
00000df0  74 68 61 74 20 6f 76 65  72 66 6c 6f 77 20 77 6f  |that overflow wo|
00000e00  75 6c 64 20 62 65 20 6c  6f 73 74 0d 61 6e 64 20  |uld be lost.and |
00000e10  31 38 36 2b 32 31 35 20  77 6f 75 6c 64 20 70 72  |186+215 would pr|
00000e20  6f 64 75 63 65 20 6f 6e  6c 79 20 31 34 35 2e 20  |oduce only 145. |
00000e30  20 54 68 69 73 20 69 64  65 61 20 6f 66 20 63 61  | This idea of ca|
00000e40  72 72 79 69 6e 67 0d 6f  76 65 72 20 66 72 6f 6d  |rrying.over from|
00000e50  20 61 20 62 79 74 65 20  61 64 64 69 74 69 6f 6e  | a byte addition|
00000e60  20 61 6e 64 20 66 69 6e  61 6c 6c 79 20 6f 76 65  | and finally ove|
00000e70  72 66 6c 6f 77 69 6e 67  20 79 6f 75 72 20 77 6f  |rflowing your wo|
00000e80  72 64 0d 69 73 20 76 65  72 79 20 69 6d 70 6f 72  |rd.is very impor|
00000e90  74 61 6e 74 2e 0d 0d 54  68 65 72 65 20 69 73 20  |tant...There is |
00000ea0  61 6e 20 61 64 64 69 74  69 6f 6e 61 6c 20 63 6f  |an additional co|
00000eb0  6d 70 6c 69 63 61 74 69  6f 6e 20 63 61 75 73 65  |mplication cause|
00000ec0  64 20 62 79 20 6f 75 72  20 6e 65 65 64 20 66 6f  |d by our need fo|
00000ed0  72 0d 6e 65 67 61 74 69  76 65 20 6e 75 6d 62 65  |r.negative numbe|
00000ee0  72 73 2e 20 20 54 68 65  20 63 6f 6d 70 75 74 65  |rs.  The compute|
00000ef0  72 20 64 6f 65 73 6e 27  74 20 75 6e 64 65 72 73  |r doesn't unders|
00000f00  74 61 6e 64 20 61 0d 6e  65 67 61 74 69 76 65 20  |tand a.negative |
00000f10  6e 75 6d 62 65 72 2c 20  61 20 6e 75 6d 62 65 72  |number, a number|
00000f20  20 69 73 20 65 69 74 68  65 72 20 74 68 65 72 65  | is either there|
00000f30  20 6f 72 20 69 74 20 69  73 6e 27 74 2c 20 73 6f  | or it isn't, so|
00000f40  20 77 65 0d 68 61 76 65  20 74 6f 20 69 6e 76 65  | we.have to inve|
00000f50  6e 74 20 61 20 63 6f 6e  76 65 6e 74 69 6f 6e 2e  |nt a convention.|
00000f60  20 20 54 68 69 73 20 75  73 65 73 20 74 68 65 20  |  This uses the |
00000f70  74 6f 70 20 62 69 74 20  6f 66 20 79 6f 75 72 0d  |top bit of your.|
00000f80  77 6f 72 64 20 74 6f 20  69 6e 64 69 63 61 74 65  |word to indicate|
00000f90  20 77 68 65 74 68 65 72  20 74 68 65 20 6e 75 6d  | whether the num|
00000fa0  62 65 72 20 69 73 20 6e  65 67 61 74 69 76 65 20  |ber is negative |
00000fb0  6f 72 20 6e 6f 74 2e 20  20 49 66 0d 77 65 20 61  |or not.  If.we a|
00000fc0  70 70 6c 79 20 74 68 69  73 20 74 6f 20 61 20 62  |pply this to a b|
00000fd0  79 74 65 20 79 6f 75 20  67 65 74 20 74 68 69 73  |yte you get this|
00000fe0  20 72 65 73 75 6c 74 3a  0d 0d 2b 76 65 20 6e 75  | result:..+ve nu|
00000ff0  6d 62 65 72 73 20 20 30  20 74 6f 20 20 31 32 37  |mbers  0 to  127|
00001000  20 61 72 65 20 26 30 30  20 74 6f 20 26 37 46 20  | are &00 to &7F |
00001010  28 30 30 30 30 30 30 30  30 20 74 6f 20 30 31 31  |(00000000 to 011|
00001020  31 31 31 31 31 29 0d 2d  76 65 20 6e 75 6d 62 65  |11111).-ve numbe|
00001030  72 73 20 2d 31 20 74 6f  20 2d 31 32 38 20 61 72  |rs -1 to -128 ar|
00001040  65 20 26 46 46 20 74 6f  20 26 38 30 20 28 31 31  |e &FF to &80 (11|
00001050  31 31 31 31 31 31 20 74  6f 20 31 30 30 30 30 30  |111111 to 100000|
00001060  30 30 29 0d 0d 53 6f 20  6e 65 67 61 74 69 76 65  |00)..So negative|
00001070  20 6e 75 6d 62 65 72 73  20 68 61 76 65 20 74 68  | numbers have th|
00001080  65 69 72 20 74 6f 70 20  62 69 74 20 73 65 74 2e  |eir top bit set.|
00001090  20 20 4e 6f 74 65 20 74  68 61 74 0d 7a 65 72 6f  |  Note that.zero|
000010a0  20 69 73 20 70 6f 73 69  74 69 76 65 2e 20 20 54  | is positive.  T|
000010b0  68 65 72 65 20 69 73 20  6d 65 74 68 6f 64 20 69  |here is method i|
000010c0  6e 20 74 68 69 73 20 73  65 65 6d 69 6e 67 20 6d  |n this seeming m|
000010d0  61 64 6e 65 73 73 0d 73  69 6e 63 65 20 61 64 64  |adness.since add|
000010e0  69 74 69 6f 6e 20 61 75  74 6f 6d 61 74 69 63 61  |ition automatica|
000010f0  6c 6c 79 20 74 61 6b 65  73 20 61 63 63 6f 75 6e  |lly takes accoun|
00001100  74 20 6f 66 20 74 68 65  20 73 69 67 6e 73 20 6f  |t of the signs o|
00001110  66 0d 74 68 65 20 6e 75  6d 62 65 72 73 20 62 65  |f.the numbers be|
00001120  69 6e 67 20 61 64 64 65  64 2c 20 61 6e 64 20 74  |ing added, and t|
00001130  68 65 20 73 61 6d 65 20  68 61 70 70 65 6e 73 20  |he same happens |
00001140  77 69 74 68 0d 73 75 62  74 72 61 63 74 69 6f 6e  |with.subtraction|
00001150  2e 20 20 49 6e 20 66 61  63 74 20 74 68 65 20 6f  |.  In fact the o|
00001160  76 65 72 66 6c 6f 77 20  6f 75 74 20 6f 66 20 74  |verflow out of t|
00001170  68 65 20 77 6f 72 64 20  68 65 6c 70 73 0d 77 69  |he word helps.wi|
00001180  74 68 20 73 75 62 74 72  61 63 74 69 6f 6e 2e 0d  |th subtraction..|
00001190  0d 4c 65 74 27 73 20 61  64 64 20 2d 31 20 61 6e  |.Let's add -1 an|
000011a0  64 20 2d 31 2c 20 77 68  69 63 68 20 67 69 76 65  |d -1, which give|
000011b0  73 20 2d 32 3a 0d 0d 20  20 20 20 20 20 20 20 20  |s -2:..         |
000011c0  20 20 20 20 20 20 20 20  20 20 20 20 31 31 31 31  |            1111|
000011d0  31 31 31 31 20 20 20 20  20 20 20 26 46 46 0d 20  |1111       &FF. |
000011e0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
000011f0  20 20 20 20 31 31 31 31  31 31 31 31 20 20 20 20  |    11111111    |
00001200  20 20 20 26 46 46 0d 20  20 20 20 20 20 20 20 20  |   &FF.         |
00001210  20 20 20 20 20 20 20 20  20 20 20 20 2d 2d 2d 2d  |            ----|
00001220  2d 2d 2d 2d 20 20 20 20  20 20 20 2d 2d 2d 0d 20  |----       ---. |
00001230  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00001240  20 20 20 20 31 31 31 31  31 31 31 30 20 20 20 20  |    11111110    |
00001250  20 20 20 26 46 45 0d 0d  54 68 65 72 65 20 69 73  |   &FE..There is|
00001260  20 61 20 77 61 79 20 6f  66 20 63 68 65 63 6b 69  | a way of checki|
00001270  6e 67 20 74 68 61 74 20  79 6f 75 20 64 6f 6e 27  |ng that you don'|
00001280  74 20 61 63 63 69 64 65  6e 74 61 6c 6c 79 20 74  |t accidentally t|
00001290  75 72 6e 0d 61 20 62 79  74 65 20 6e 65 67 61 74  |urn.a byte negat|
000012a0  69 76 65 20 62 79 20 61  64 64 69 6e 67 20 74 6f  |ive by adding to|
000012b0  67 65 74 68 65 72 20 74  77 6f 20 70 6f 73 69 74  |gether two posit|
000012c0  69 76 65 20 6e 75 6d 62  65 72 73 20 62 6f 74 68  |ive numbers both|
000012d0  0d 6f 76 65 72 20 36 34  2c 20 74 68 61 74 20 69  |.over 64, that i|
000012e0  73 20 73 6f 6d 65 74 68  69 6e 67 20 63 61 6c 6c  |s something call|
000012f0  65 64 20 74 68 65 20 6f  76 65 72 66 6c 6f 77 20  |ed the overflow |
00001300  66 6c 61 67 2e 20 20 41  0d 63 61 72 72 79 2c 20  |flag.  A.carry, |
00001310  77 68 69 63 68 20 79 6f  75 20 63 61 6e 20 74 68  |which you can th|
00001320  69 6e 6b 20 6f 66 20 61  73 20 62 65 69 6e 67 20  |ink of as being |
00001330  74 68 65 20 27 39 74 68  20 62 69 74 27 20 6f 66  |the '9th bit' of|
00001340  20 61 0d 62 79 74 65 2c  20 73 65 74 73 20 61 6e  | a.byte, sets an|
00001350  6f 74 68 65 72 20 66 6c  61 67 20 63 61 6c 6c 65  |other flag calle|
00001360  64 20 74 68 65 20 63 61  72 72 79 20 66 6c 61 67  |d the carry flag|
00001370  2e 20 20 28 4d 6f 72 65  20 6f 6e 0d 66 6c 61 67  |.  (More on.flag|
00001380  73 20 69 6e 20 61 6e 6f  74 68 65 72 20 6d 6f 64  |s in another mod|
00001390  75 6c 65 2e 29 0d 0d 54  68 65 20 72 65 6c 61 74  |ule.)..The relat|
000013a0  69 6f 6e 73 68 69 70 20  62 65 74 77 65 65 6e 20  |ionship between |
000013b0  61 20 6e 75 6d 62 65 72  20 61 6e 64 20 69 74 73  |a number and its|
000013c0  20 6e 65 67 61 74 69 76  65 20 69 73 20 74 68 61  | negative is tha|
000013d0  74 0d 65 61 63 68 20 62  69 74 20 6f 66 20 74 68  |t.each bit of th|
000013e0  65 20 6e 75 6d 62 65 72  20 69 73 20 72 65 76 65  |e number is reve|
000013f0  72 73 65 64 20 28 6f 72  20 69 6e 76 65 72 74 65  |rsed (or inverte|
00001400  64 2c 20 30 20 62 65 63  6f 6d 65 73 20 31 0d 61  |d, 0 becomes 1.a|
00001410  6e 64 20 31 20 62 65 63  6f 6d 65 73 20 30 29 20  |nd 1 becomes 0) |
00001420  61 6e 64 20 74 68 65 6e  20 6f 6e 65 20 69 73 20  |and then one is |
00001430  61 64 64 65 64 20 74 6f  20 69 74 2e 20 20 54 68  |added to it.  Th|
00001440  69 73 20 6d 65 61 6e 73  0d 74 68 61 74 20 31 20  |is means.that 1 |
00001450  28 30 30 30 30 30 30 30  31 29 20 62 65 63 6f 6d  |(00000001) becom|
00001460  65 73 20 31 31 31 31 31  31 31 30 20 2b 20 31 20  |es 11111110 + 1 |
00001470  3d 20 31 31 31 31 31 31  31 31 2e 20 20 57 68 65  |= 11111111.  Whe|
00001480  6e 20 79 6f 75 0d 69 6e  76 65 72 74 20 74 68 65  |n you.invert the|
00001490  20 62 69 74 73 20 6f 66  20 61 20 6e 75 6d 62 65  | bits of a numbe|
000014a0  72 20 79 6f 75 20 61 72  65 20 73 61 69 64 20 74  |r you are said t|
000014b0  6f 20 62 65 20 74 61 6b  69 6e 67 20 69 74 73 0d  |o be taking its.|
000014c0  63 6f 6d 70 6c 65 6d 65  6e 74 20 61 6e 64 20 77  |complement and w|
000014d0  68 65 6e 20 79 6f 75 20  61 64 64 20 6f 6e 65 20  |hen you add one |
000014e0  79 6f 75 20 67 65 74 20  69 74 73 20 32 27 73 20  |you get its 2's |
000014f0  63 6f 6d 70 6c 65 6d 65  6e 74 2e 0d 0d 53 75 62  |complement...Sub|
00001500  74 72 61 63 74 69 6f 6e  20 69 73 20 63 61 72 72  |traction is carr|
00001510  69 65 64 20 6f 75 74 20  73 69 6d 69 6c 61 72 6c  |ied out similarl|
00001520  79 20 74 6f 20 61 64 64  69 74 69 6f 6e 2e 20 20  |y to addition.  |
00001530  49 66 20 79 6f 75 0d 74  61 6b 65 20 30 20 66 72  |If you.take 0 fr|
00001540  6f 6d 20 30 20 79 6f 75  20 67 65 74 20 30 2c 20  |om 0 you get 0, |
00001550  30 20 66 72 6f 6d 20 31  20 69 73 20 31 20 61 6e  |0 from 1 is 1 an|
00001560  64 20 31 20 66 72 6f 6d  20 31 20 69 73 20 30 2e  |d 1 from 1 is 0.|
00001570  20 0d 54 6f 20 74 61 6b  65 20 31 20 66 72 6f 6d  | .To take 1 from|
00001580  20 30 20 79 6f 75 20 62  6f 72 72 6f 77 20 66 72  | 0 you borrow fr|
00001590  6f 6d 20 74 68 65 20 6e  65 78 74 20 68 69 67 68  |om the next high|
000015a0  65 73 74 20 62 69 74 20  61 6e 64 20 73 6f 0d 74  |est bit and so.t|
000015b0  61 6b 65 20 31 20 66 72  6f 6d 20 31 30 2c 20 77  |ake 1 from 10, w|
000015c0  68 69 63 68 20 69 73 20  31 2e 0d 0d 20 20 20 20  |hich is 1...    |
000015d0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
000015e0  20 20 31 30 31 30 30 31  30 31 0d 20 20 20 20 20  |  10100101.     |
000015f0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 2d  |               -|
00001600  20 30 31 31 31 30 31 31  30 0d 20 20 20 20 20 20  | 01110110.      |
00001610  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00001620  2d 2d 2d 2d 2d 2d 2d 2d  0d 20 20 20 20 20 20 20  |--------.       |
00001630  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 30  |               0|
00001640  30 31 30 31 31 31 31 0d  0d 49 74 27 73 20 6d 6f  |0101111..It's mo|
00001650  72 65 20 64 69 66 66 69  63 75 6c 74 20 74 6f 20  |re difficult to |
00001660  67 65 74 20 74 68 65 20  68 61 6e 67 20 6f 66 20  |get the hang of |
00001670  62 69 6e 61 72 79 20 73  75 62 74 72 61 63 74 69  |binary subtracti|
00001680  6f 6e 0d 74 68 61 6e 20  61 64 64 69 74 69 6f 6e  |on.than addition|
00001690  20 62 75 74 2c 20 69 6e  20 74 68 65 20 65 6e 64  | but, in the end|
000016a0  2c 20 69 74 20 64 6f 65  73 6e 27 74 20 72 65 61  |, it doesn't rea|
000016b0  6c 6c 79 20 6d 61 74 74  65 72 2e 20 0d 41 70 61  |lly matter. .Apa|
000016c0  72 74 2c 20 74 68 61 74  20 69 73 2c 20 66 72 6f  |rt, that is, fro|
000016d0  6d 20 74 68 65 20 69 64  65 61 20 6f 66 20 62 6f  |m the idea of bo|
000016e0  72 72 6f 77 69 6e 67 20  73 69 6e 63 65 20 69 74  |rrowing since it|
000016f0  20 69 73 20 74 68 69 73  0d 74 68 61 74 20 65 6e  | is this.that en|
00001700  61 62 6c 65 73 20 79 6f  75 20 74 6f 20 73 75 62  |ables you to sub|
00001710  74 72 61 63 74 20 6e 75  6d 62 65 72 73 20 6c 61  |tract numbers la|
00001720  72 67 65 72 20 74 68 61  6e 20 61 20 73 69 6e 67  |rger than a sing|
00001730  6c 65 0d 62 79 74 65 2e  20 20 43 61 72 72 79 69  |le.byte.  Carryi|
00001740  6e 67 20 64 6f 65 73 20  74 68 69 73 20 6a 6f 62  |ng does this job|
00001750  20 69 6e 20 61 64 64 69  74 69 6f 6e 20 61 6e 64  | in addition and|
00001760  20 69 6e 20 62 6f 74 68  20 63 61 73 65 73 0d 74  | in both cases.t|
00001770  68 65 20 63 61 72 72 79  20 66 6c 61 67 20 69 73  |he carry flag is|
00001780  20 75 73 65 64 2e 0d 0d  54 6f 20 61 64 64 20 74  | used...To add t|
00001790  77 6f 20 62 79 74 65 73  20 28 73 74 6f 72 65 64  |wo bytes (stored|
000017a0  20 69 6e 20 74 68 65 20  61 64 64 72 65 73 73 65  | in the addresse|
000017b0  73 20 6c 61 62 65 6c 6c  65 64 20 62 79 74 65 5f  |s labelled byte_|
000017c0  31 20 61 6e 64 0d 62 79  74 65 5f 32 29 20 61 6e  |1 and.byte_2) an|
000017d0  64 20 73 74 6f 72 65 20  74 68 65 20 72 65 73 75  |d store the resu|
000017e0  6c 74 20 61 74 20 61 64  64 72 65 73 73 20 62 79  |lt at address by|
000017f0  74 65 5f 33 20 79 6f 75  20 64 6f 20 74 68 69 73  |te_3 you do this|
00001800  3a 0d 0d 43 4c 65 61 72  20 74 68 65 20 43 61 72  |:..CLear the Car|
00001810  72 79 20 66 6c 61 67 20  20 20 20 20 20 20 20 20  |ry flag         |
00001820  20 20 20 20 43 4c 43 0d  4c 6f 61 44 20 41 20 77  |    CLC.LoaD A w|
00001830  69 74 68 20 74 68 65 20  66 69 72 73 74 20 6e 75  |ith the first nu|
00001840  6d 62 65 72 20 20 20 20  20 4c 44 41 20 62 79 74  |mber     LDA byt|
00001850  65 5f 31 0d 41 44 64 20  74 68 65 20 73 65 63 6f  |e_1.ADd the seco|
00001860  6e 64 20 28 77 69 74 68  20 43 61 72 72 79 29 20  |nd (with Carry) |
00001870  20 20 20 20 20 41 44 43  20 62 79 74 65 5f 32 0d  |     ADC byte_2.|
00001880  53 54 6f 72 65 20 74 68  65 20 72 65 73 75 6c 74  |STore the result|
00001890  20 28 66 72 6f 6d 20 41  29 20 20 20 20 20 20 20  | (from A)       |
000018a0  20 53 54 41 20 62 79 74  65 5f 33 0d 0d 41 2c 20  | STA byte_3..A, |
000018b0  74 68 65 20 61 63 63 75  6d 75 6c 61 74 6f 72 2c  |the accumulator,|
000018c0  20 69 73 20 74 68 65 20  70 61 72 74 20 6f 66 20  | is the part of |
000018d0  74 68 65 20 6d 69 63 72  6f 70 72 6f 63 65 73 73  |the microprocess|
000018e0  6f 72 20 77 68 65 72 65  0d 79 6f 75 20 64 6f 20  |or where.you do |
000018f0  79 6f 75 72 20 61 72 69  74 68 6d 65 74 69 63 2e  |your arithmetic.|
00001900  20 20 41 44 43 20 61 64  64 73 20 61 20 6e 75 6d  |  ADC adds a num|
00001910  62 65 72 20 74 6f 20 74  68 65 20 76 61 6c 75 65  |ber to the value|
00001920  20 69 6e 0d 74 68 65 20  61 63 63 75 6d 75 6c 61  | in.the accumula|
00001930  74 6f 72 20 61 6e 64 20  61 64 64 73 20 74 68 65  |tor and adds the|
00001940  20 76 61 6c 75 65 20 6f  66 20 74 68 65 20 63 61  | value of the ca|
00001950  72 72 79 20 66 6c 61 67  20 74 6f 0d 74 68 61 74  |rry flag to.that|
00001960  2e 20 20 54 68 65 20 72  65 73 75 6c 74 20 69 73  |.  The result is|
00001970  20 69 6e 20 74 68 65 20  61 63 63 75 6d 75 6c 61  | in the accumula|
00001980  74 6f 72 2e 0d 0d 54 6f  20 73 75 62 74 72 61 63  |tor...To subtrac|
00001990  74 20 32 20 62 79 74 65  73 20 79 6f 75 20 68 61  |t 2 bytes you ha|
000019a0  76 65 20 74 6f 20 73 74  61 72 74 20 62 79 20 73  |ve to start by s|
000019b0  65 74 74 69 6e 67 20 74  68 65 20 63 61 72 72 79  |etting the carry|
000019c0  0d 66 6c 61 67 3a 0d 0d  53 45 74 20 43 61 72 72  |.flag:..SEt Carr|
000019d0  79 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |y               |
000019e0  20 20 20 20 20 20 20 20  20 53 45 43 0d 4c 6f 61  |         SEC.Loa|
000019f0  44 20 41 20 77 69 74 68  20 74 68 65 20 66 69 72  |D A with the fir|
00001a00  73 74 20 6e 75 6d 62 65  72 20 20 20 20 20 4c 44  |st number     LD|
00001a10  41 20 62 79 74 65 5f 31  0d 53 75 42 74 72 61 63  |A byte_1.SuBtrac|
00001a20  74 20 74 68 65 20 73 65  63 6f 6e 64 20 28 77 69  |t the second (wi|
00001a30  74 68 20 43 61 72 72 79  29 20 53 42 43 20 62 79  |th Carry) SBC by|
00001a40  74 65 5f 32 0d 53 54 6f  72 65 20 74 68 65 20 72  |te_2.STore the r|
00001a50  65 73 75 6c 74 20 28 66  72 6f 6d 20 41 29 20 20  |esult (from A)  |
00001a60  20 20 20 20 20 20 53 54  41 20 62 79 74 65 5f 33  |      STA byte_3|
00001a70  0d 0d 49 6e 20 62 6f 74  68 20 63 61 73 65 73 2c  |..In both cases,|
00001a80  20 69 66 20 79 6f 75 20  61 72 65 20 61 64 64 69  | if you are addi|
00001a90  6e 67 20 6f 72 20 73 75  62 74 72 61 63 74 69 6e  |ng or subtractin|
00001aa0  67 20 6e 75 6d 62 65 72  73 20 6d 61 64 65 0d 75  |g numbers made.u|
00001ab0  70 20 6f 66 20 6d 61 6e  79 20 62 79 74 65 73 2c  |p of many bytes,|
00001ac0  20 79 6f 75 20 6f 6e 6c  79 20 65 78 70 6c 69 63  | you only explic|
00001ad0  69 74 6c 79 20 63 6c 65  61 72 20 6f 72 20 73 65  |itly clear or se|
00001ae0  74 20 74 68 65 20 63 61  72 72 79 0d 61 74 20 74  |t the carry.at t|
00001af0  68 65 20 62 65 67 69 6e  6e 69 6e 67 2e 20 20 41  |he beginning.  A|
00001b00  66 74 65 72 20 74 68 61  74 20 69 74 20 73 65 72  |fter that it ser|
00001b10  76 65 73 20 69 74 73 20  70 72 6f 70 65 72 20 70  |ves its proper p|
00001b20  75 72 70 6f 73 65 0d 61  6e 64 20 63 61 72 72 69  |urpose.and carri|
00001b30  65 73 20 61 63 72 6f 73  73 20 6f 72 20 62 6f 72  |es across or bor|
00001b40  72 6f 77 73 20 61 63 72  6f 73 73 20 69 66 20 6e  |rows across if n|
00001b50  65 65 64 65 64 2e 20 20  4c 6f 6f 6b 20 61 74 0d  |eeded.  Look at.|
00001b60  74 68 69 73 20 77 65 65  6b 73 20 61 73 73 65 6d  |this weeks assem|
00001b70  62 6c 65 72 20 70 72 6f  67 72 61 6d 20 42 2f 6f  |bler program B/o|
00001b80  73 62 30 32 20 77 68 69  63 68 20 61 64 64 73 20  |sb02 which adds |
00001b90  61 6e 64 0d 73 75 62 74  72 61 63 74 73 20 74 77  |and.subtracts tw|
00001ba0  6f 20 66 6f 75 72 20 62  79 74 65 20 6e 75 6d 62  |o four byte numb|
00001bb0  65 72 73 2e 20 20 49 20  68 61 76 65 20 75 73 65  |ers.  I have use|
00001bc0  64 20 62 69 74 73 20 6f  66 20 42 41 53 49 43 0d  |d bits of BASIC.|
00001bd0  74 6f 20 65 6e 61 62 6c  65 20 79 6f 75 20 74 6f  |to enable you to|
00001be0  20 67 65 74 20 74 68 65  20 6e 75 6d 62 65 72 73  | get the numbers|
00001bf0  20 69 6e 74 6f 20 61 6e  64 20 6f 75 74 20 6f 66  | into and out of|
00001c00  20 74 68 65 20 6d 61 63  68 69 6e 65 0d 62 75 74  | the machine.but|
00001c10  20 65 76 65 6e 74 75 61  6c 6c 79 20 77 65 20 77  | eventually we w|
00001c20  69 6c 6c 20 75 73 65 20  6d 61 63 68 69 6e 65 20  |ill use machine |
00001c30  63 6f 64 65 20 74 6f 20  64 6f 20 74 68 69 73 20  |code to do this |
00001c40  61 73 20 77 65 6c 6c 2e  20 0d 57 65 20 63 6f 75  |as well. .We cou|
00001c50  6c 64 20 65 78 74 65 6e  64 20 74 68 61 74 20 63  |ld extend that c|
00001c60  6f 64 65 20 74 6f 20 77  6f 72 6b 20 77 69 74 68  |ode to work with|
00001c70  20 6e 75 6d 62 65 72 73  20 6f 66 20 61 6e 79 20  | numbers of any |
00001c80  73 69 7a 65 0d 62 75 74  20 42 41 53 49 43 27 73  |size.but BASIC's|
00001c90  20 69 6e 74 65 67 65 72  73 20 61 72 65 20 6f 6e  | integers are on|
00001ca0  6c 79 20 34 20 62 79 74  65 73 20 69 6e 20 73 69  |ly 4 bytes in si|
00001cb0  7a 65 20 73 6f 20 77 65  20 77 69 6c 6c 0d 73 74  |ze so we will.st|
00001cc0  69 63 6b 20 74 6f 20 34  20 62 79 74 65 73 20 66  |ick to 4 bytes f|
00001cd0  6f 72 20 74 68 65 20 6d  6f 6d 65 6e 74 2e 0d 0d  |or the moment...|
00001ce0  4e 6f 74 65 20 74 68 61  74 20 42 2f 6f 73 62 30  |Note that B/osb0|
00001cf0  32 20 77 69 6c 6c 20 67  69 76 65 20 61 6e 20 69  |2 will give an i|
00001d00  6e 63 6f 72 72 65 63 74  20 72 65 73 75 6c 74 20  |ncorrect result |
00001d10  69 66 20 79 6f 75 20 61  64 64 0d 74 6f 67 65 74  |if you add.toget|
00001d20  68 65 72 20 6e 75 6d 62  65 72 73 20 73 75 63 68  |her numbers such|
00001d30  20 74 68 61 74 20 74 68  65 69 72 20 73 75 6d 20  | that their sum |
00001d40  69 73 20 67 72 65 61 74  65 72 20 74 68 61 6e 0d  |is greater than.|
00001d50  32 31 34 37 34 38 33 36  34 37 20 28 61 6e 64 20  |2147483647 (and |
00001d60  73 69 6d 69 6c 61 72 6c  79 20 66 6f 72 20 6e 65  |similarly for ne|
00001d70  67 61 74 69 76 65 20 6e  75 6d 62 65 72 73 29 2e  |gative numbers).|
00001d80  20 20 54 68 69 73 20 69  73 0d 62 65 63 61 75 73  |  This is.becaus|
00001d90  65 20 74 68 65 20 6f 76  65 72 66 6c 6f 77 20 62  |e the overflow b|
00001da0  65 74 77 65 65 6e 20 74  68 65 20 74 6f 70 20 74  |etween the top t|
00001db0  77 6f 20 62 69 74 73 20  69 73 20 6e 6f 74 20 68  |wo bits is not h|
00001dc0  61 6e 64 6c 65 64 0d 63  6f 72 72 65 63 74 6c 79  |andled.correctly|
00001dd0  2e 20 20 42 41 53 49 43  20 77 6f 75 6c 64 20 74  |.  BASIC would t|
00001de0  72 61 70 20 74 68 69 73  20 61 73 20 61 20 54 6f  |rap this as a To|
00001df0  6f 20 42 69 67 20 65 72  72 6f 72 20 62 75 74 0d  |o Big error but.|
00001e00  74 68 69 73 20 6d 61 63  68 69 6e 65 20 63 6f 64  |this machine cod|
00001e10  65 20 64 6f 65 73 20 6e  6f 74 2e 20 20 54 6f 20  |e does not.  To |
00001e20  64 6f 20 73 6f 20 75 73  65 73 20 61 6e 6f 74 68  |do so uses anoth|
00001e30  65 72 20 6f 66 20 74 68  65 0d 6d 69 63 72 6f 70  |er of the.microp|
00001e40  72 6f 63 65 73 73 6f 72  27 73 20 66 6c 61 67 73  |rocessor's flags|
00001e50  20 63 61 6c 6c 65 64 20  74 68 65 20 6f 76 65 72  | called the over|
00001e60  66 6c 6f 77 20 66 6c 61  67 2e 0d 0d 57 65 27 6c  |flow flag...We'l|
00001e70  6c 20 6c 6f 6f 6b 20 61  74 20 74 68 65 20 66 6c  |l look at the fl|
00001e80  61 67 73 20 69 6e 20 6d  6f 72 65 20 64 65 74 61  |ags in more deta|
00001e90  69 6c 20 6e 65 78 74 20  77 65 65 6b 20 61 6e 64  |il next week and|
00001ea0  20 75 73 65 0d 74 68 65  6d 20 74 6f 20 67 65 74  | use.them to get|
00001eb0  20 74 68 65 20 63 6f 6d  70 75 74 65 72 20 74 6f  | the computer to|
00001ec0  20 6d 61 6b 65 20 73 6f  6d 65 20 64 65 63 69 73  | make some decis|
00001ed0  69 6f 6e 73 2e 0d                                 |ions..|
00001ed6
01_11_87/T\OSB02.m0
01_11_87/T\OSB02.m1
01_11_87/T\OSB02.m2
01_11_87/T\OSB02.m4
01_11_87/T\OSB02.m5