Home » CEEFAX disks » telesoftware6.adl » 15-04-88/T\OSB22

15-04-88/T\OSB22

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 » telesoftware6.adl
Filename: 15-04-88/T\OSB22
Read OK:
File size: 2BCB bytes
Load address: 0000
Exec address: FFFFFFFF
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 22: Floating Point Arithmetic


In the last module we used a fixed point arithmetic system
for calculations.  The result was relatively fast but was
slightly innacurate since very small numbers were not
represented as well as they could have been.

You will remember that we converted from a real number to a
fixed point number by multiplying by a constant factor.  The
decimal example was that 12.34 became 12340 if the constant
factor was 100, and that .0123 became 12 (the last digit
being lost).  All our calculating was then done on the new,
larger numbers, which were integers and so we could use
integer arithmetic.  When we had our integer result it could
be converted back to its proper size by dividing by the
constant factor.  We also needed to know how multilplying
and dividing would be affected by this factor and compensate
for it.

The next logical step in this numbering is to make the
factor variable and to keep track of what it is.  This
could, for example, make 12.34 be represented by any of the
following:

                    1.234 * 10^+1
                    12.34 * 10^+0
                    123.4 * 10^-1
                    1234  * 10^-2

the last digit being a power of 10.  This is the scientific
notation used in the BBC Micro but written as E-2 etc.  This
part of the number is called the EXPONENT.  Since all the
above list are equivalent there is a need to decide on a
normal format for such notation.  There is a standard that
says that the first number (the MANTISSA) nust always be
between 0.1 and 1.  This would lead to 12.34 being written
as 0.1234E2 (meaning multiplied by 10 to the power 2).  The
BBC Micro standard has the mantissa between 1 and 10 so that
12.34 would be 1.234E1 (meaning multiplied by 10 to the
power 1).

Here we have a floating point decimal numbering convention
such that the mantissa is always of a size between two
limits and the exponent is the power of 10 by which you
multiply the mantissa to produce the required number.  You
will notice straight away that the mantissa is not an
integer.  Does this matter?  In fact it does not.

The object with our fixed point system in the last program
modules was to work with integers in the knowledge that our
numbers were in reality 65536 times too large.  But they
were only 65536 times too large if we insisted in thinking
of them as integers in this way.  The alternative way was to
put a ficticious 'decimal' point between the top two bytes
of the number and the bottom two.  This method would have
worked equally well and is essentially the same, it was only
a question of how we visualised the numbers being stored.

The rules for arithmetic for floating point are similar to
those for fixed point, except that the constant factor is
now variable and is known as the exponent.

Addition requires that the exponent of the two numbers be
the same, then the mantissae are added.  You keep the size
of the number the same by multiplying the mantissa by 10 to
the power of the number you subtract from the exponent (or
vice versa).

For example
                1.23E5 + 2.34E3

setting both to have an exponent of E3

             =  123E3 + 2.34E3
             =  125.34E3
             =  1.2534E5  (re-normalised)

It is up to you which number you de-normalise to match
which.  On paper it is often clearer to avoid too many zeros
by de-normalising the larger number.

Subtraction is similar to addition in that the exponents
must be made the same before subtraction, then normalisation
is carried out.

Multiplication is done by multiplying the mantissae and
adding the exponents.

For example
                1.23E5 * 2.34E3
             =  (1.23*2.34)E8
             =  2.8782E8

In this case we don't need to renormalise.

Division is similar to multiplication in that we divide the
mantissae and subtract the exponents.

For example
                1.23E5 / 2.34E3 
             =  (1.23/2.34)E2
             =  0.5256E2
             =  5.256E1   approximately

Incidentally, the habit of working with mantissae between 0
and 1 or between 1 and 10 dates back to the days of slide
rules.  The slide rule would take care of the calculations
of the mantissae but it was up to you to sort out the
position of your decimal point.

BBC BASIC has a convention for floating point arithmetic
which we will use in this module.  Using the exisiting
convention means I can use BBC BASIC to translate from
decimal into floating point instead of writing a conversion
routine.  This could be done by modifying the ASCII to
binary routine in module 12.

Five bytes are used to store a number, four bytes of
mantissa and one of exponent.

The binary equivalent of of 3.25 is 11.01 since, after the
binary point, the first place is 'halves' the second is
'quarters', the third is 'eighths' and so on.

To normalise this number under the BBC BASIC conventionwe
must have the binary point at the head of the number such
that

      3.25 (decimal) = 11.01 (binary) = .1101 x2^2

This gives us a representation in memory like this

  Exponent     Mantissa
  --------------------------------------------------------
  | 00000010 | 11010000 | 00000000 | 00000000 | 00000000 |
  --------------------------------------------------------

since the exponent is 2 (10 in binary) and the mantissa is
aligned to the left.  In this case, like one of our
conventions for decimal floating point, the mantissa is
always between 0 and 1.  In effect you shift the mantissa
until its top bit is set.  This means you can check the
negative flag (which reflects bit 7) of the first mantissa
byte if you want to check for normalisation.

It is vital to remember that, unlike integer numbers, where
the most significant byte is stored at the greatest address,
with floating point in the BBC Micro the exponent is at the
lowest address and the mantissa is stored in the next four
higher bytes, with the most significant byte in the LOWEST
address.

Now, because the first bit of the mantissa is ALWAYS going
to be set, unless the number is zero, BASIC uses that top
bit to denote sign.  This is not a 2's complement notation,
it is purely and simply a sign bit.  If it is set the number
is positive, if it is clear the number is negative.  But
remember that this is using a bit that ought to be set as a
flag and in fact the top bit is actually always set.

One final contrivance of BBC BASIC is to add &80 to the
exponent to aid in calculating, so I have done that below.

Hence, this is the representation of 3.25


  Exponent     Mantissa
  --------------------------------------------------------
  | 10000010 | 01010000 | 00000000 | 00000000 | 00000000 |
  --------------------------------------------------------
    &82        &50        &00        &00        &00

and this of -3.25

  Exponent     Mantissa
  --------------------------------------------------------
  | 10000010 | 11010000 | 00000000 | 00000000 | 00000000 |
  --------------------------------------------------------
    &82        &D0        &00        &00        &00

Zero is stored as five zero bytes.

In calculating with floating point numbers of this format we
have three operations.  Firstly we carry out the calculation
on the mantissae, secondly we carry out the operation on the
exponents and finally we normalise the result.

The program in this module takes a pair of numbers you enter
and calculates the sum and difference of them using modified
versions of the multi-byte arithmetical routines I have
introduced earlier in the series.

I won't go into great detail here about the program since it
is copiously annotated but a few points are worth
mentioning.

The input of the floating point numbers is done using a
pseudo OPT function defined at the end of the program.  It
relies on us knowing where BASIC will store the first
variable beginning with a ` sign (pound).  We can use this
knowledge to both put and retrieve BBC BASIC format floating
point numbers.

Ironically, although having separately signed numbers makes
life easier for multiplication and division (next module) it
actually complicates addition and subtraction more than
somewhat.  I have introduced an extra byte to act as an
overflow with each of the numbers in the workspace so that
we can convert negative numbers to 2's complement and back
again.  In fact the BASIC fp routines also use a rounding
byte to improve precision but that does not seem to be
necessary here.

The main routines for addition and subtraction are
straightforward enough.  They are basically five byte
multiple precision similar to those in earlier modules.  The
curious difference is that, as I mentioned before, the bytes
are stored back to front with the most significant byte in
the lowest memory location.

In the main section of the program there are these
subroutines:

'move_sign_bit' moves the sign bits from the top of the
mantissae and puts them into a separate byte for later use.

'equate_exponents' denormalises the smaller of the two
numbers so that the exponents of the two numbers are the
same.  It does this by rotating the mantissa right (which
makes it smaller) while increasing the exponent until the
exponents match.

'neg_convert' makes any negative numbers into 2's
complement, making use of the extra overflow byte.

Then come the addition or subtraction subroutines.

'neg_convert_back' takes any result that is 2's complement
negative and makes it positive while modifying its sign
byte.

Then we trap a zero result since the zero format is
non-standard and would put the renormalising routine into an
endless loop.

'renormalise' adjusts the result until it is a normalised
number (with the top bit of the mantissa set) as expected by
BASIC.  The exponent is adjusted accordingly.  If there is
anything in the overflow byte then the whole mantissa
(including the overflow byte) is rotated right until the
overflow byte is empty.  The exponent is increased by one
for each rotation.  If, during the course of this increment,
the exponent becomes zero then we have generated a number
too large for the system to handle, and an error is
generated.  It has the number 20 but I have given it a
different message to the standard 'too big'.

If the overflow byte is empty then the rest of the mantissa
is rotated left until the top bit of the mantissa is set. 
The exponent is decreased by one for each rotation.  Of
course if the top bit is already set then the number is
correct.

Finally the routine at 'replace_top_bit' calculates the new
sign bit and replaces the top bit of the mantissa with it.

When you run the program you may have to be in MODE 7
because the assember code is rather long.   You enter a
couple of numbers of virtually any size, and you will be
able to compare the addition and subtraction results from
this machine code and from BASIC.  The machine code here is
almost twice as fast as BASIC although in some circumstances
it is not quite as precise.  This is only likely to show as
a difference of 1 in the smallest significant digit between
the two proffered results.

Next time multiplication and division.
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 42 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 0d 0d 0d 50  61 72 74 20 32 32 3a 20  |.......Part 22: |
00000090  46 6c 6f 61 74 69 6e 67  20 50 6f 69 6e 74 20 41  |Floating Point A|
000000a0  72 69 74 68 6d 65 74 69  63 0d 0d 0d 49 6e 20 74  |rithmetic...In t|
000000b0  68 65 20 6c 61 73 74 20  6d 6f 64 75 6c 65 20 77  |he last module w|
000000c0  65 20 75 73 65 64 20 61  20 66 69 78 65 64 20 70  |e used a fixed p|
000000d0  6f 69 6e 74 20 61 72 69  74 68 6d 65 74 69 63 20  |oint arithmetic |
000000e0  73 79 73 74 65 6d 0d 66  6f 72 20 63 61 6c 63 75  |system.for calcu|
000000f0  6c 61 74 69 6f 6e 73 2e  20 20 54 68 65 20 72 65  |lations.  The re|
00000100  73 75 6c 74 20 77 61 73  20 72 65 6c 61 74 69 76  |sult was relativ|
00000110  65 6c 79 20 66 61 73 74  20 62 75 74 20 77 61 73  |ely fast but was|
00000120  0d 73 6c 69 67 68 74 6c  79 20 69 6e 6e 61 63 75  |.slightly innacu|
00000130  72 61 74 65 20 73 69 6e  63 65 20 76 65 72 79 20  |rate since very |
00000140  73 6d 61 6c 6c 20 6e 75  6d 62 65 72 73 20 77 65  |small numbers we|
00000150  72 65 20 6e 6f 74 0d 72  65 70 72 65 73 65 6e 74  |re not.represent|
00000160  65 64 20 61 73 20 77 65  6c 6c 20 61 73 20 74 68  |ed as well as th|
00000170  65 79 20 63 6f 75 6c 64  20 68 61 76 65 20 62 65  |ey could have be|
00000180  65 6e 2e 0d 0d 59 6f 75  20 77 69 6c 6c 20 72 65  |en...You will re|
00000190  6d 65 6d 62 65 72 20 74  68 61 74 20 77 65 20 63  |member that we c|
000001a0  6f 6e 76 65 72 74 65 64  20 66 72 6f 6d 20 61 20  |onverted from a |
000001b0  72 65 61 6c 20 6e 75 6d  62 65 72 20 74 6f 20 61  |real number to a|
000001c0  0d 66 69 78 65 64 20 70  6f 69 6e 74 20 6e 75 6d  |.fixed point num|
000001d0  62 65 72 20 62 79 20 6d  75 6c 74 69 70 6c 79 69  |ber by multiplyi|
000001e0  6e 67 20 62 79 20 61 20  63 6f 6e 73 74 61 6e 74  |ng by a constant|
000001f0  20 66 61 63 74 6f 72 2e  20 20 54 68 65 0d 64 65  | factor.  The.de|
00000200  63 69 6d 61 6c 20 65 78  61 6d 70 6c 65 20 77 61  |cimal example wa|
00000210  73 20 74 68 61 74 20 31  32 2e 33 34 20 62 65 63  |s that 12.34 bec|
00000220  61 6d 65 20 31 32 33 34  30 20 69 66 20 74 68 65  |ame 12340 if the|
00000230  20 63 6f 6e 73 74 61 6e  74 0d 66 61 63 74 6f 72  | constant.factor|
00000240  20 77 61 73 20 31 30 30  2c 20 61 6e 64 20 74 68  | was 100, and th|
00000250  61 74 20 2e 30 31 32 33  20 62 65 63 61 6d 65 20  |at .0123 became |
00000260  31 32 20 28 74 68 65 20  6c 61 73 74 20 64 69 67  |12 (the last dig|
00000270  69 74 0d 62 65 69 6e 67  20 6c 6f 73 74 29 2e 20  |it.being lost). |
00000280  20 41 6c 6c 20 6f 75 72  20 63 61 6c 63 75 6c 61  | All our calcula|
00000290  74 69 6e 67 20 77 61 73  20 74 68 65 6e 20 64 6f  |ting was then do|
000002a0  6e 65 20 6f 6e 20 74 68  65 20 6e 65 77 2c 0d 6c  |ne on the new,.l|
000002b0  61 72 67 65 72 20 6e 75  6d 62 65 72 73 2c 20 77  |arger numbers, w|
000002c0  68 69 63 68 20 77 65 72  65 20 69 6e 74 65 67 65  |hich were intege|
000002d0  72 73 20 61 6e 64 20 73  6f 20 77 65 20 63 6f 75  |rs and so we cou|
000002e0  6c 64 20 75 73 65 0d 69  6e 74 65 67 65 72 20 61  |ld use.integer a|
000002f0  72 69 74 68 6d 65 74 69  63 2e 20 20 57 68 65 6e  |rithmetic.  When|
00000300  20 77 65 20 68 61 64 20  6f 75 72 20 69 6e 74 65  | we had our inte|
00000310  67 65 72 20 72 65 73 75  6c 74 20 69 74 20 63 6f  |ger result it co|
00000320  75 6c 64 0d 62 65 20 63  6f 6e 76 65 72 74 65 64  |uld.be converted|
00000330  20 62 61 63 6b 20 74 6f  20 69 74 73 20 70 72 6f  | back to its pro|
00000340  70 65 72 20 73 69 7a 65  20 62 79 20 64 69 76 69  |per size by divi|
00000350  64 69 6e 67 20 62 79 20  74 68 65 0d 63 6f 6e 73  |ding by the.cons|
00000360  74 61 6e 74 20 66 61 63  74 6f 72 2e 20 20 57 65  |tant factor.  We|
00000370  20 61 6c 73 6f 20 6e 65  65 64 65 64 20 74 6f 20  | also needed to |
00000380  6b 6e 6f 77 20 68 6f 77  20 6d 75 6c 74 69 6c 70  |know how multilp|
00000390  6c 79 69 6e 67 0d 61 6e  64 20 64 69 76 69 64 69  |lying.and dividi|
000003a0  6e 67 20 77 6f 75 6c 64  20 62 65 20 61 66 66 65  |ng would be affe|
000003b0  63 74 65 64 20 62 79 20  74 68 69 73 20 66 61 63  |cted by this fac|
000003c0  74 6f 72 20 61 6e 64 20  63 6f 6d 70 65 6e 73 61  |tor and compensa|
000003d0  74 65 0d 66 6f 72 20 69  74 2e 0d 0d 54 68 65 20  |te.for it...The |
000003e0  6e 65 78 74 20 6c 6f 67  69 63 61 6c 20 73 74 65  |next logical ste|
000003f0  70 20 69 6e 20 74 68 69  73 20 6e 75 6d 62 65 72  |p in this number|
00000400  69 6e 67 20 69 73 20 74  6f 20 6d 61 6b 65 20 74  |ing is to make t|
00000410  68 65 0d 66 61 63 74 6f  72 20 76 61 72 69 61 62  |he.factor variab|
00000420  6c 65 20 61 6e 64 20 74  6f 20 6b 65 65 70 20 74  |le and to keep t|
00000430  72 61 63 6b 20 6f 66 20  77 68 61 74 20 69 74 20  |rack of what it |
00000440  69 73 2e 20 20 54 68 69  73 0d 63 6f 75 6c 64 2c  |is.  This.could,|
00000450  20 66 6f 72 20 65 78 61  6d 70 6c 65 2c 20 6d 61  | for example, ma|
00000460  6b 65 20 31 32 2e 33 34  20 62 65 20 72 65 70 72  |ke 12.34 be repr|
00000470  65 73 65 6e 74 65 64 20  62 79 20 61 6e 79 20 6f  |esented by any o|
00000480  66 20 74 68 65 0d 66 6f  6c 6c 6f 77 69 6e 67 3a  |f the.following:|
00000490  0d 0d 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |..              |
000004a0  20 20 20 20 20 20 31 2e  32 33 34 20 2a 20 31 30  |      1.234 * 10|
000004b0  5e 2b 31 0d 20 20 20 20  20 20 20 20 20 20 20 20  |^+1.            |
000004c0  20 20 20 20 20 20 20 20  31 32 2e 33 34 20 2a 20  |        12.34 * |
000004d0  31 30 5e 2b 30 0d 20 20  20 20 20 20 20 20 20 20  |10^+0.          |
000004e0  20 20 20 20 20 20 20 20  20 20 31 32 33 2e 34 20  |          123.4 |
000004f0  2a 20 31 30 5e 2d 31 0d  20 20 20 20 20 20 20 20  |* 10^-1.        |
00000500  20 20 20 20 20 20 20 20  20 20 20 20 31 32 33 34  |            1234|
00000510  20 20 2a 20 31 30 5e 2d  32 0d 0d 74 68 65 20 6c  |  * 10^-2..the l|
00000520  61 73 74 20 64 69 67 69  74 20 62 65 69 6e 67 20  |ast digit being |
00000530  61 20 70 6f 77 65 72 20  6f 66 20 31 30 2e 20 20  |a power of 10.  |
00000540  54 68 69 73 20 69 73 20  74 68 65 20 73 63 69 65  |This is the scie|
00000550  6e 74 69 66 69 63 0d 6e  6f 74 61 74 69 6f 6e 20  |ntific.notation |
00000560  75 73 65 64 20 69 6e 20  74 68 65 20 42 42 43 20  |used in the BBC |
00000570  4d 69 63 72 6f 20 62 75  74 20 77 72 69 74 74 65  |Micro but writte|
00000580  6e 20 61 73 20 45 2d 32  20 65 74 63 2e 20 20 54  |n as E-2 etc.  T|
00000590  68 69 73 0d 70 61 72 74  20 6f 66 20 74 68 65 20  |his.part of the |
000005a0  6e 75 6d 62 65 72 20 69  73 20 63 61 6c 6c 65 64  |number is called|
000005b0  20 74 68 65 20 45 58 50  4f 4e 45 4e 54 2e 20 20  | the EXPONENT.  |
000005c0  53 69 6e 63 65 20 61 6c  6c 20 74 68 65 0d 61 62  |Since all the.ab|
000005d0  6f 76 65 20 6c 69 73 74  20 61 72 65 20 65 71 75  |ove list are equ|
000005e0  69 76 61 6c 65 6e 74 20  74 68 65 72 65 20 69 73  |ivalent there is|
000005f0  20 61 20 6e 65 65 64 20  74 6f 20 64 65 63 69 64  | a need to decid|
00000600  65 20 6f 6e 20 61 0d 6e  6f 72 6d 61 6c 20 66 6f  |e on a.normal fo|
00000610  72 6d 61 74 20 66 6f 72  20 73 75 63 68 20 6e 6f  |rmat for such no|
00000620  74 61 74 69 6f 6e 2e 20  20 54 68 65 72 65 20 69  |tation.  There i|
00000630  73 20 61 20 73 74 61 6e  64 61 72 64 20 74 68 61  |s a standard tha|
00000640  74 0d 73 61 79 73 20 74  68 61 74 20 74 68 65 20  |t.says that the |
00000650  66 69 72 73 74 20 6e 75  6d 62 65 72 20 28 74 68  |first number (th|
00000660  65 20 4d 41 4e 54 49 53  53 41 29 20 6e 75 73 74  |e MANTISSA) nust|
00000670  20 61 6c 77 61 79 73 20  62 65 0d 62 65 74 77 65  | always be.betwe|
00000680  65 6e 20 30 2e 31 20 61  6e 64 20 31 2e 20 20 54  |en 0.1 and 1.  T|
00000690  68 69 73 20 77 6f 75 6c  64 20 6c 65 61 64 20 74  |his would lead t|
000006a0  6f 20 31 32 2e 33 34 20  62 65 69 6e 67 20 77 72  |o 12.34 being wr|
000006b0  69 74 74 65 6e 0d 61 73  20 30 2e 31 32 33 34 45  |itten.as 0.1234E|
000006c0  32 20 28 6d 65 61 6e 69  6e 67 20 6d 75 6c 74 69  |2 (meaning multi|
000006d0  70 6c 69 65 64 20 62 79  20 31 30 20 74 6f 20 74  |plied by 10 to t|
000006e0  68 65 20 70 6f 77 65 72  20 32 29 2e 20 20 54 68  |he power 2).  Th|
000006f0  65 0d 42 42 43 20 4d 69  63 72 6f 20 73 74 61 6e  |e.BBC Micro stan|
00000700  64 61 72 64 20 68 61 73  20 74 68 65 20 6d 61 6e  |dard has the man|
00000710  74 69 73 73 61 20 62 65  74 77 65 65 6e 20 31 20  |tissa between 1 |
00000720  61 6e 64 20 31 30 20 73  6f 20 74 68 61 74 0d 31  |and 10 so that.1|
00000730  32 2e 33 34 20 77 6f 75  6c 64 20 62 65 20 31 2e  |2.34 would be 1.|
00000740  32 33 34 45 31 20 28 6d  65 61 6e 69 6e 67 20 6d  |234E1 (meaning m|
00000750  75 6c 74 69 70 6c 69 65  64 20 62 79 20 31 30 20  |ultiplied by 10 |
00000760  74 6f 20 74 68 65 0d 70  6f 77 65 72 20 31 29 2e  |to the.power 1).|
00000770  0d 0d 48 65 72 65 20 77  65 20 68 61 76 65 20 61  |..Here we have a|
00000780  20 66 6c 6f 61 74 69 6e  67 20 70 6f 69 6e 74 20  | floating point |
00000790  64 65 63 69 6d 61 6c 20  6e 75 6d 62 65 72 69 6e  |decimal numberin|
000007a0  67 20 63 6f 6e 76 65 6e  74 69 6f 6e 0d 73 75 63  |g convention.suc|
000007b0  68 20 74 68 61 74 20 74  68 65 20 6d 61 6e 74 69  |h that the manti|
000007c0  73 73 61 20 69 73 20 61  6c 77 61 79 73 20 6f 66  |ssa is always of|
000007d0  20 61 20 73 69 7a 65 20  62 65 74 77 65 65 6e 20  | a size between |
000007e0  74 77 6f 0d 6c 69 6d 69  74 73 20 61 6e 64 20 74  |two.limits and t|
000007f0  68 65 20 65 78 70 6f 6e  65 6e 74 20 69 73 20 74  |he exponent is t|
00000800  68 65 20 70 6f 77 65 72  20 6f 66 20 31 30 20 62  |he power of 10 b|
00000810  79 20 77 68 69 63 68 20  79 6f 75 0d 6d 75 6c 74  |y which you.mult|
00000820  69 70 6c 79 20 74 68 65  20 6d 61 6e 74 69 73 73  |iply the mantiss|
00000830  61 20 74 6f 20 70 72 6f  64 75 63 65 20 74 68 65  |a to produce the|
00000840  20 72 65 71 75 69 72 65  64 20 6e 75 6d 62 65 72  | required number|
00000850  2e 20 20 59 6f 75 0d 77  69 6c 6c 20 6e 6f 74 69  |.  You.will noti|
00000860  63 65 20 73 74 72 61 69  67 68 74 20 61 77 61 79  |ce straight away|
00000870  20 74 68 61 74 20 74 68  65 20 6d 61 6e 74 69 73  | that the mantis|
00000880  73 61 20 69 73 20 6e 6f  74 20 61 6e 0d 69 6e 74  |sa is not an.int|
00000890  65 67 65 72 2e 20 20 44  6f 65 73 20 74 68 69 73  |eger.  Does this|
000008a0  20 6d 61 74 74 65 72 3f  20 20 49 6e 20 66 61 63  | matter?  In fac|
000008b0  74 20 69 74 20 64 6f 65  73 20 6e 6f 74 2e 0d 0d  |t it does not...|
000008c0  54 68 65 20 6f 62 6a 65  63 74 20 77 69 74 68 20  |The object with |
000008d0  6f 75 72 20 66 69 78 65  64 20 70 6f 69 6e 74 20  |our fixed point |
000008e0  73 79 73 74 65 6d 20 69  6e 20 74 68 65 20 6c 61  |system in the la|
000008f0  73 74 20 70 72 6f 67 72  61 6d 0d 6d 6f 64 75 6c  |st program.modul|
00000900  65 73 20 77 61 73 20 74  6f 20 77 6f 72 6b 20 77  |es was to work w|
00000910  69 74 68 20 69 6e 74 65  67 65 72 73 20 69 6e 20  |ith integers in |
00000920  74 68 65 20 6b 6e 6f 77  6c 65 64 67 65 20 74 68  |the knowledge th|
00000930  61 74 20 6f 75 72 0d 6e  75 6d 62 65 72 73 20 77  |at our.numbers w|
00000940  65 72 65 20 69 6e 20 72  65 61 6c 69 74 79 20 36  |ere in reality 6|
00000950  35 35 33 36 20 74 69 6d  65 73 20 74 6f 6f 20 6c  |5536 times too l|
00000960  61 72 67 65 2e 20 20 42  75 74 20 74 68 65 79 0d  |arge.  But they.|
00000970  77 65 72 65 20 6f 6e 6c  79 20 36 35 35 33 36 20  |were only 65536 |
00000980  74 69 6d 65 73 20 74 6f  6f 20 6c 61 72 67 65 20  |times too large |
00000990  69 66 20 77 65 20 69 6e  73 69 73 74 65 64 20 69  |if we insisted i|
000009a0  6e 20 74 68 69 6e 6b 69  6e 67 0d 6f 66 20 74 68  |n thinking.of th|
000009b0  65 6d 20 61 73 20 69 6e  74 65 67 65 72 73 20 69  |em as integers i|
000009c0  6e 20 74 68 69 73 20 77  61 79 2e 20 20 54 68 65  |n this way.  The|
000009d0  20 61 6c 74 65 72 6e 61  74 69 76 65 20 77 61 79  | alternative way|
000009e0  20 77 61 73 20 74 6f 0d  70 75 74 20 61 20 66 69  | was to.put a fi|
000009f0  63 74 69 63 69 6f 75 73  20 27 64 65 63 69 6d 61  |cticious 'decima|
00000a00  6c 27 20 70 6f 69 6e 74  20 62 65 74 77 65 65 6e  |l' point between|
00000a10  20 74 68 65 20 74 6f 70  20 74 77 6f 20 62 79 74  | the top two byt|
00000a20  65 73 0d 6f 66 20 74 68  65 20 6e 75 6d 62 65 72  |es.of the number|
00000a30  20 61 6e 64 20 74 68 65  20 62 6f 74 74 6f 6d 20  | and the bottom |
00000a40  74 77 6f 2e 20 20 54 68  69 73 20 6d 65 74 68 6f  |two.  This metho|
00000a50  64 20 77 6f 75 6c 64 20  68 61 76 65 0d 77 6f 72  |d would have.wor|
00000a60  6b 65 64 20 65 71 75 61  6c 6c 79 20 77 65 6c 6c  |ked equally well|
00000a70  20 61 6e 64 20 69 73 20  65 73 73 65 6e 74 69 61  | and is essentia|
00000a80  6c 6c 79 20 74 68 65 20  73 61 6d 65 2c 20 69 74  |lly the same, it|
00000a90  20 77 61 73 20 6f 6e 6c  79 0d 61 20 71 75 65 73  | was only.a ques|
00000aa0  74 69 6f 6e 20 6f 66 20  68 6f 77 20 77 65 20 76  |tion of how we v|
00000ab0  69 73 75 61 6c 69 73 65  64 20 74 68 65 20 6e 75  |isualised the nu|
00000ac0  6d 62 65 72 73 20 62 65  69 6e 67 20 73 74 6f 72  |mbers being stor|
00000ad0  65 64 2e 0d 0d 54 68 65  20 72 75 6c 65 73 20 66  |ed...The rules f|
00000ae0  6f 72 20 61 72 69 74 68  6d 65 74 69 63 20 66 6f  |or arithmetic fo|
00000af0  72 20 66 6c 6f 61 74 69  6e 67 20 70 6f 69 6e 74  |r floating point|
00000b00  20 61 72 65 20 73 69 6d  69 6c 61 72 20 74 6f 0d  | are similar to.|
00000b10  74 68 6f 73 65 20 66 6f  72 20 66 69 78 65 64 20  |those for fixed |
00000b20  70 6f 69 6e 74 2c 20 65  78 63 65 70 74 20 74 68  |point, except th|
00000b30  61 74 20 74 68 65 20 63  6f 6e 73 74 61 6e 74 20  |at the constant |
00000b40  66 61 63 74 6f 72 20 69  73 0d 6e 6f 77 20 76 61  |factor is.now va|
00000b50  72 69 61 62 6c 65 20 61  6e 64 20 69 73 20 6b 6e  |riable and is kn|
00000b60  6f 77 6e 20 61 73 20 74  68 65 20 65 78 70 6f 6e  |own as the expon|
00000b70  65 6e 74 2e 0d 0d 41 64  64 69 74 69 6f 6e 20 72  |ent...Addition r|
00000b80  65 71 75 69 72 65 73 20  74 68 61 74 20 74 68 65  |equires that the|
00000b90  20 65 78 70 6f 6e 65 6e  74 20 6f 66 20 74 68 65  | exponent of the|
00000ba0  20 74 77 6f 20 6e 75 6d  62 65 72 73 20 62 65 0d  | two numbers be.|
00000bb0  74 68 65 20 73 61 6d 65  2c 20 74 68 65 6e 20 74  |the same, then t|
00000bc0  68 65 20 6d 61 6e 74 69  73 73 61 65 20 61 72 65  |he mantissae are|
00000bd0  20 61 64 64 65 64 2e 20  20 59 6f 75 20 6b 65 65  | added.  You kee|
00000be0  70 20 74 68 65 20 73 69  7a 65 0d 6f 66 20 74 68  |p the size.of th|
00000bf0  65 20 6e 75 6d 62 65 72  20 74 68 65 20 73 61 6d  |e number the sam|
00000c00  65 20 62 79 20 6d 75 6c  74 69 70 6c 79 69 6e 67  |e by multiplying|
00000c10  20 74 68 65 20 6d 61 6e  74 69 73 73 61 20 62 79  | the mantissa by|
00000c20  20 31 30 20 74 6f 0d 74  68 65 20 70 6f 77 65 72  | 10 to.the power|
00000c30  20 6f 66 20 74 68 65 20  6e 75 6d 62 65 72 20 79  | of the number y|
00000c40  6f 75 20 73 75 62 74 72  61 63 74 20 66 72 6f 6d  |ou subtract from|
00000c50  20 74 68 65 20 65 78 70  6f 6e 65 6e 74 20 28 6f  | the exponent (o|
00000c60  72 0d 76 69 63 65 20 76  65 72 73 61 29 2e 0d 0d  |r.vice versa)...|
00000c70  46 6f 72 20 65 78 61 6d  70 6c 65 0d 20 20 20 20  |For example.    |
00000c80  20 20 20 20 20 20 20 20  20 20 20 20 31 2e 32 33  |            1.23|
00000c90  45 35 20 2b 20 32 2e 33  34 45 33 0d 0d 73 65 74  |E5 + 2.34E3..set|
00000ca0  74 69 6e 67 20 62 6f 74  68 20 74 6f 20 68 61 76  |ting both to hav|
00000cb0  65 20 61 6e 20 65 78 70  6f 6e 65 6e 74 20 6f 66  |e an exponent of|
00000cc0  20 45 33 0d 0d 20 20 20  20 20 20 20 20 20 20 20  | E3..           |
00000cd0  20 20 3d 20 20 31 32 33  45 33 20 2b 20 32 2e 33  |  =  123E3 + 2.3|
00000ce0  34 45 33 0d 20 20 20 20  20 20 20 20 20 20 20 20  |4E3.            |
00000cf0  20 3d 20 20 31 32 35 2e  33 34 45 33 0d 20 20 20  | =  125.34E3.   |
00000d00  20 20 20 20 20 20 20 20  20 20 3d 20 20 31 2e 32  |          =  1.2|
00000d10  35 33 34 45 35 20 20 28  72 65 2d 6e 6f 72 6d 61  |534E5  (re-norma|
00000d20  6c 69 73 65 64 29 0d 0d  49 74 20 69 73 20 75 70  |lised)..It is up|
00000d30  20 74 6f 20 79 6f 75 20  77 68 69 63 68 20 6e 75  | to you which nu|
00000d40  6d 62 65 72 20 79 6f 75  20 64 65 2d 6e 6f 72 6d  |mber you de-norm|
00000d50  61 6c 69 73 65 20 74 6f  20 6d 61 74 63 68 0d 77  |alise to match.w|
00000d60  68 69 63 68 2e 20 20 4f  6e 20 70 61 70 65 72 20  |hich.  On paper |
00000d70  69 74 20 69 73 20 6f 66  74 65 6e 20 63 6c 65 61  |it is often clea|
00000d80  72 65 72 20 74 6f 20 61  76 6f 69 64 20 74 6f 6f  |rer to avoid too|
00000d90  20 6d 61 6e 79 20 7a 65  72 6f 73 0d 62 79 20 64  | many zeros.by d|
00000da0  65 2d 6e 6f 72 6d 61 6c  69 73 69 6e 67 20 74 68  |e-normalising th|
00000db0  65 20 6c 61 72 67 65 72  20 6e 75 6d 62 65 72 2e  |e larger number.|
00000dc0  0d 0d 53 75 62 74 72 61  63 74 69 6f 6e 20 69 73  |..Subtraction is|
00000dd0  20 73 69 6d 69 6c 61 72  20 74 6f 20 61 64 64 69  | similar to addi|
00000de0  74 69 6f 6e 20 69 6e 20  74 68 61 74 20 74 68 65  |tion in that the|
00000df0  20 65 78 70 6f 6e 65 6e  74 73 0d 6d 75 73 74 20  | exponents.must |
00000e00  62 65 20 6d 61 64 65 20  74 68 65 20 73 61 6d 65  |be made the same|
00000e10  20 62 65 66 6f 72 65 20  73 75 62 74 72 61 63 74  | before subtract|
00000e20  69 6f 6e 2c 20 74 68 65  6e 20 6e 6f 72 6d 61 6c  |ion, then normal|
00000e30  69 73 61 74 69 6f 6e 0d  69 73 20 63 61 72 72 69  |isation.is carri|
00000e40  65 64 20 6f 75 74 2e 0d  0d 4d 75 6c 74 69 70 6c  |ed out...Multipl|
00000e50  69 63 61 74 69 6f 6e 20  69 73 20 64 6f 6e 65 20  |ication is done |
00000e60  62 79 20 6d 75 6c 74 69  70 6c 79 69 6e 67 20 74  |by multiplying t|
00000e70  68 65 20 6d 61 6e 74 69  73 73 61 65 20 61 6e 64  |he mantissae and|
00000e80  0d 61 64 64 69 6e 67 20  74 68 65 20 65 78 70 6f  |.adding the expo|
00000e90  6e 65 6e 74 73 2e 0d 0d  46 6f 72 20 65 78 61 6d  |nents...For exam|
00000ea0  70 6c 65 0d 20 20 20 20  20 20 20 20 20 20 20 20  |ple.            |
00000eb0  20 20 20 20 31 2e 32 33  45 35 20 2a 20 32 2e 33  |    1.23E5 * 2.3|
00000ec0  34 45 33 0d 20 20 20 20  20 20 20 20 20 20 20 20  |4E3.            |
00000ed0  20 3d 20 20 28 31 2e 32  33 2a 32 2e 33 34 29 45  | =  (1.23*2.34)E|
00000ee0  38 0d 20 20 20 20 20 20  20 20 20 20 20 20 20 3d  |8.             =|
00000ef0  20 20 32 2e 38 37 38 32  45 38 0d 0d 49 6e 20 74  |  2.8782E8..In t|
00000f00  68 69 73 20 63 61 73 65  20 77 65 20 64 6f 6e 27  |his case we don'|
00000f10  74 20 6e 65 65 64 20 74  6f 20 72 65 6e 6f 72 6d  |t need to renorm|
00000f20  61 6c 69 73 65 2e 0d 0d  44 69 76 69 73 69 6f 6e  |alise...Division|
00000f30  20 69 73 20 73 69 6d 69  6c 61 72 20 74 6f 20 6d  | is similar to m|
00000f40  75 6c 74 69 70 6c 69 63  61 74 69 6f 6e 20 69 6e  |ultiplication in|
00000f50  20 74 68 61 74 20 77 65  20 64 69 76 69 64 65 20  | that we divide |
00000f60  74 68 65 0d 6d 61 6e 74  69 73 73 61 65 20 61 6e  |the.mantissae an|
00000f70  64 20 73 75 62 74 72 61  63 74 20 74 68 65 20 65  |d subtract the e|
00000f80  78 70 6f 6e 65 6e 74 73  2e 0d 0d 46 6f 72 20 65  |xponents...For e|
00000f90  78 61 6d 70 6c 65 0d 20  20 20 20 20 20 20 20 20  |xample.         |
00000fa0  20 20 20 20 20 20 20 31  2e 32 33 45 35 20 2f 20  |       1.23E5 / |
00000fb0  32 2e 33 34 45 33 20 0d  20 20 20 20 20 20 20 20  |2.34E3 .        |
00000fc0  20 20 20 20 20 3d 20 20  28 31 2e 32 33 2f 32 2e  |     =  (1.23/2.|
00000fd0  33 34 29 45 32 0d 20 20  20 20 20 20 20 20 20 20  |34)E2.          |
00000fe0  20 20 20 3d 20 20 30 2e  35 32 35 36 45 32 0d 20  |   =  0.5256E2. |
00000ff0  20 20 20 20 20 20 20 20  20 20 20 20 3d 20 20 35  |            =  5|
00001000  2e 32 35 36 45 31 20 20  20 61 70 70 72 6f 78 69  |.256E1   approxi|
00001010  6d 61 74 65 6c 79 0d 0d  49 6e 63 69 64 65 6e 74  |mately..Incident|
00001020  61 6c 6c 79 2c 20 74 68  65 20 68 61 62 69 74 20  |ally, the habit |
00001030  6f 66 20 77 6f 72 6b 69  6e 67 20 77 69 74 68 20  |of working with |
00001040  6d 61 6e 74 69 73 73 61  65 20 62 65 74 77 65 65  |mantissae betwee|
00001050  6e 20 30 0d 61 6e 64 20  31 20 6f 72 20 62 65 74  |n 0.and 1 or bet|
00001060  77 65 65 6e 20 31 20 61  6e 64 20 31 30 20 64 61  |ween 1 and 10 da|
00001070  74 65 73 20 62 61 63 6b  20 74 6f 20 74 68 65 20  |tes back to the |
00001080  64 61 79 73 20 6f 66 20  73 6c 69 64 65 0d 72 75  |days of slide.ru|
00001090  6c 65 73 2e 20 20 54 68  65 20 73 6c 69 64 65 20  |les.  The slide |
000010a0  72 75 6c 65 20 77 6f 75  6c 64 20 74 61 6b 65 20  |rule would take |
000010b0  63 61 72 65 20 6f 66 20  74 68 65 20 63 61 6c 63  |care of the calc|
000010c0  75 6c 61 74 69 6f 6e 73  0d 6f 66 20 74 68 65 20  |ulations.of the |
000010d0  6d 61 6e 74 69 73 73 61  65 20 62 75 74 20 69 74  |mantissae but it|
000010e0  20 77 61 73 20 75 70 20  74 6f 20 79 6f 75 20 74  | was up to you t|
000010f0  6f 20 73 6f 72 74 20 6f  75 74 20 74 68 65 0d 70  |o sort out the.p|
00001100  6f 73 69 74 69 6f 6e 20  6f 66 20 79 6f 75 72 20  |osition of your |
00001110  64 65 63 69 6d 61 6c 20  70 6f 69 6e 74 2e 0d 0d  |decimal point...|
00001120  42 42 43 20 42 41 53 49  43 20 68 61 73 20 61 20  |BBC BASIC has a |
00001130  63 6f 6e 76 65 6e 74 69  6f 6e 20 66 6f 72 20 66  |convention for f|
00001140  6c 6f 61 74 69 6e 67 20  70 6f 69 6e 74 20 61 72  |loating point ar|
00001150  69 74 68 6d 65 74 69 63  0d 77 68 69 63 68 20 77  |ithmetic.which w|
00001160  65 20 77 69 6c 6c 20 75  73 65 20 69 6e 20 74 68  |e will use in th|
00001170  69 73 20 6d 6f 64 75 6c  65 2e 20 20 55 73 69 6e  |is module.  Usin|
00001180  67 20 74 68 65 20 65 78  69 73 69 74 69 6e 67 0d  |g the exisiting.|
00001190  63 6f 6e 76 65 6e 74 69  6f 6e 20 6d 65 61 6e 73  |convention means|
000011a0  20 49 20 63 61 6e 20 75  73 65 20 42 42 43 20 42  | I can use BBC B|
000011b0  41 53 49 43 20 74 6f 20  74 72 61 6e 73 6c 61 74  |ASIC to translat|
000011c0  65 20 66 72 6f 6d 0d 64  65 63 69 6d 61 6c 20 69  |e from.decimal i|
000011d0  6e 74 6f 20 66 6c 6f 61  74 69 6e 67 20 70 6f 69  |nto floating poi|
000011e0  6e 74 20 69 6e 73 74 65  61 64 20 6f 66 20 77 72  |nt instead of wr|
000011f0  69 74 69 6e 67 20 61 20  63 6f 6e 76 65 72 73 69  |iting a conversi|
00001200  6f 6e 0d 72 6f 75 74 69  6e 65 2e 20 20 54 68 69  |on.routine.  Thi|
00001210  73 20 63 6f 75 6c 64 20  62 65 20 64 6f 6e 65 20  |s could be done |
00001220  62 79 20 6d 6f 64 69 66  79 69 6e 67 20 74 68 65  |by modifying the|
00001230  20 41 53 43 49 49 20 74  6f 0d 62 69 6e 61 72 79  | ASCII to.binary|
00001240  20 72 6f 75 74 69 6e 65  20 69 6e 20 6d 6f 64 75  | routine in modu|
00001250  6c 65 20 31 32 2e 0d 0d  46 69 76 65 20 62 79 74  |le 12...Five byt|
00001260  65 73 20 61 72 65 20 75  73 65 64 20 74 6f 20 73  |es are used to s|
00001270  74 6f 72 65 20 61 20 6e  75 6d 62 65 72 2c 20 66  |tore a number, f|
00001280  6f 75 72 20 62 79 74 65  73 20 6f 66 0d 6d 61 6e  |our bytes of.man|
00001290  74 69 73 73 61 20 61 6e  64 20 6f 6e 65 20 6f 66  |tissa and one of|
000012a0  20 65 78 70 6f 6e 65 6e  74 2e 0d 0d 54 68 65 20  | exponent...The |
000012b0  62 69 6e 61 72 79 20 65  71 75 69 76 61 6c 65 6e  |binary equivalen|
000012c0  74 20 6f 66 20 6f 66 20  33 2e 32 35 20 69 73 20  |t of of 3.25 is |
000012d0  31 31 2e 30 31 20 73 69  6e 63 65 2c 20 61 66 74  |11.01 since, aft|
000012e0  65 72 20 74 68 65 0d 62  69 6e 61 72 79 20 70 6f  |er the.binary po|
000012f0  69 6e 74 2c 20 74 68 65  20 66 69 72 73 74 20 70  |int, the first p|
00001300  6c 61 63 65 20 69 73 20  27 68 61 6c 76 65 73 27  |lace is 'halves'|
00001310  20 74 68 65 20 73 65 63  6f 6e 64 20 69 73 0d 27  | the second is.'|
00001320  71 75 61 72 74 65 72 73  27 2c 20 74 68 65 20 74  |quarters', the t|
00001330  68 69 72 64 20 69 73 20  27 65 69 67 68 74 68 73  |hird is 'eighths|
00001340  27 20 61 6e 64 20 73 6f  20 6f 6e 2e 0d 0d 54 6f  |' and so on...To|
00001350  20 6e 6f 72 6d 61 6c 69  73 65 20 74 68 69 73 20  | normalise this |
00001360  6e 75 6d 62 65 72 20 75  6e 64 65 72 20 74 68 65  |number under the|
00001370  20 42 42 43 20 42 41 53  49 43 20 63 6f 6e 76 65  | BBC BASIC conve|
00001380  6e 74 69 6f 6e 77 65 0d  6d 75 73 74 20 68 61 76  |ntionwe.must hav|
00001390  65 20 74 68 65 20 62 69  6e 61 72 79 20 70 6f 69  |e the binary poi|
000013a0  6e 74 20 61 74 20 74 68  65 20 68 65 61 64 20 6f  |nt at the head o|
000013b0  66 20 74 68 65 20 6e 75  6d 62 65 72 20 73 75 63  |f the number suc|
000013c0  68 0d 74 68 61 74 0d 0d  20 20 20 20 20 20 33 2e  |h.that..      3.|
000013d0  32 35 20 28 64 65 63 69  6d 61 6c 29 20 3d 20 31  |25 (decimal) = 1|
000013e0  31 2e 30 31 20 28 62 69  6e 61 72 79 29 20 3d 20  |1.01 (binary) = |
000013f0  2e 31 31 30 31 20 78 32  5e 32 0d 0d 54 68 69 73  |.1101 x2^2..This|
00001400  20 67 69 76 65 73 20 75  73 20 61 20 72 65 70 72  | gives us a repr|
00001410  65 73 65 6e 74 61 74 69  6f 6e 20 69 6e 20 6d 65  |esentation in me|
00001420  6d 6f 72 79 20 6c 69 6b  65 20 74 68 69 73 0d 0d  |mory like this..|
00001430  20 20 45 78 70 6f 6e 65  6e 74 20 20 20 20 20 4d  |  Exponent     M|
00001440  61 6e 74 69 73 73 61 0d  20 20 2d 2d 2d 2d 2d 2d  |antissa.  ------|
00001450  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001480  2d 2d 0d 20 20 7c 20 30  30 30 30 30 30 31 30 20  |--.  | 00000010 |
00001490  7c 20 31 31 30 31 30 30  30 30 20 7c 20 30 30 30  || 11010000 | 000|
000014a0  30 30 30 30 30 20 7c 20  30 30 30 30 30 30 30 30  |00000 | 00000000|
000014b0  20 7c 20 30 30 30 30 30  30 30 30 20 7c 0d 20 20  | | 00000000 |.  |
000014c0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
000014f0  2d 2d 2d 2d 2d 2d 2d 2d  0d 0d 73 69 6e 63 65 20  |--------..since |
00001500  74 68 65 20 65 78 70 6f  6e 65 6e 74 20 69 73 20  |the exponent is |
00001510  32 20 28 31 30 20 69 6e  20 62 69 6e 61 72 79 29  |2 (10 in binary)|
00001520  20 61 6e 64 20 74 68 65  20 6d 61 6e 74 69 73 73  | and the mantiss|
00001530  61 20 69 73 0d 61 6c 69  67 6e 65 64 20 74 6f 20  |a is.aligned to |
00001540  74 68 65 20 6c 65 66 74  2e 20 20 49 6e 20 74 68  |the left.  In th|
00001550  69 73 20 63 61 73 65 2c  20 6c 69 6b 65 20 6f 6e  |is case, like on|
00001560  65 20 6f 66 20 6f 75 72  0d 63 6f 6e 76 65 6e 74  |e of our.convent|
00001570  69 6f 6e 73 20 66 6f 72  20 64 65 63 69 6d 61 6c  |ions for decimal|
00001580  20 66 6c 6f 61 74 69 6e  67 20 70 6f 69 6e 74 2c  | floating point,|
00001590  20 74 68 65 20 6d 61 6e  74 69 73 73 61 20 69 73  | the mantissa is|
000015a0  0d 61 6c 77 61 79 73 20  62 65 74 77 65 65 6e 20  |.always between |
000015b0  30 20 61 6e 64 20 31 2e  20 20 49 6e 20 65 66 66  |0 and 1.  In eff|
000015c0  65 63 74 20 79 6f 75 20  73 68 69 66 74 20 74 68  |ect you shift th|
000015d0  65 20 6d 61 6e 74 69 73  73 61 0d 75 6e 74 69 6c  |e mantissa.until|
000015e0  20 69 74 73 20 74 6f 70  20 62 69 74 20 69 73 20  | its top bit is |
000015f0  73 65 74 2e 20 20 54 68  69 73 20 6d 65 61 6e 73  |set.  This means|
00001600  20 79 6f 75 20 63 61 6e  20 63 68 65 63 6b 20 74  | you can check t|
00001610  68 65 0d 6e 65 67 61 74  69 76 65 20 66 6c 61 67  |he.negative flag|
00001620  20 28 77 68 69 63 68 20  72 65 66 6c 65 63 74 73  | (which reflects|
00001630  20 62 69 74 20 37 29 20  6f 66 20 74 68 65 20 66  | bit 7) of the f|
00001640  69 72 73 74 20 6d 61 6e  74 69 73 73 61 0d 62 79  |irst mantissa.by|
00001650  74 65 20 69 66 20 79 6f  75 20 77 61 6e 74 20 74  |te if you want t|
00001660  6f 20 63 68 65 63 6b 20  66 6f 72 20 6e 6f 72 6d  |o check for norm|
00001670  61 6c 69 73 61 74 69 6f  6e 2e 0d 0d 49 74 20 69  |alisation...It i|
00001680  73 20 76 69 74 61 6c 20  74 6f 20 72 65 6d 65 6d  |s vital to remem|
00001690  62 65 72 20 74 68 61 74  2c 20 75 6e 6c 69 6b 65  |ber that, unlike|
000016a0  20 69 6e 74 65 67 65 72  20 6e 75 6d 62 65 72 73  | integer numbers|
000016b0  2c 20 77 68 65 72 65 0d  74 68 65 20 6d 6f 73 74  |, where.the most|
000016c0  20 73 69 67 6e 69 66 69  63 61 6e 74 20 62 79 74  | significant byt|
000016d0  65 20 69 73 20 73 74 6f  72 65 64 20 61 74 20 74  |e is stored at t|
000016e0  68 65 20 67 72 65 61 74  65 73 74 20 61 64 64 72  |he greatest addr|
000016f0  65 73 73 2c 0d 77 69 74  68 20 66 6c 6f 61 74 69  |ess,.with floati|
00001700  6e 67 20 70 6f 69 6e 74  20 69 6e 20 74 68 65 20  |ng point in the |
00001710  42 42 43 20 4d 69 63 72  6f 20 74 68 65 20 65 78  |BBC Micro the ex|
00001720  70 6f 6e 65 6e 74 20 69  73 20 61 74 20 74 68 65  |ponent is at the|
00001730  0d 6c 6f 77 65 73 74 20  61 64 64 72 65 73 73 20  |.lowest address |
00001740  61 6e 64 20 74 68 65 20  6d 61 6e 74 69 73 73 61  |and the mantissa|
00001750  20 69 73 20 73 74 6f 72  65 64 20 69 6e 20 74 68  | is stored in th|
00001760  65 20 6e 65 78 74 20 66  6f 75 72 0d 68 69 67 68  |e next four.high|
00001770  65 72 20 62 79 74 65 73  2c 20 77 69 74 68 20 74  |er bytes, with t|
00001780  68 65 20 6d 6f 73 74 20  73 69 67 6e 69 66 69 63  |he most signific|
00001790  61 6e 74 20 62 79 74 65  20 69 6e 20 74 68 65 20  |ant byte in the |
000017a0  4c 4f 57 45 53 54 0d 61  64 64 72 65 73 73 2e 0d  |LOWEST.address..|
000017b0  0d 4e 6f 77 2c 20 62 65  63 61 75 73 65 20 74 68  |.Now, because th|
000017c0  65 20 66 69 72 73 74 20  62 69 74 20 6f 66 20 74  |e first bit of t|
000017d0  68 65 20 6d 61 6e 74 69  73 73 61 20 69 73 20 41  |he mantissa is A|
000017e0  4c 57 41 59 53 20 67 6f  69 6e 67 0d 74 6f 20 62  |LWAYS going.to b|
000017f0  65 20 73 65 74 2c 20 75  6e 6c 65 73 73 20 74 68  |e set, unless th|
00001800  65 20 6e 75 6d 62 65 72  20 69 73 20 7a 65 72 6f  |e number is zero|
00001810  2c 20 42 41 53 49 43 20  75 73 65 73 20 74 68 61  |, BASIC uses tha|
00001820  74 20 74 6f 70 0d 62 69  74 20 74 6f 20 64 65 6e  |t top.bit to den|
00001830  6f 74 65 20 73 69 67 6e  2e 20 20 54 68 69 73 20  |ote sign.  This |
00001840  69 73 20 6e 6f 74 20 61  20 32 27 73 20 63 6f 6d  |is not a 2's com|
00001850  70 6c 65 6d 65 6e 74 20  6e 6f 74 61 74 69 6f 6e  |plement notation|
00001860  2c 0d 69 74 20 69 73 20  70 75 72 65 6c 79 20 61  |,.it is purely a|
00001870  6e 64 20 73 69 6d 70 6c  79 20 61 20 73 69 67 6e  |nd simply a sign|
00001880  20 62 69 74 2e 20 20 49  66 20 69 74 20 69 73 20  | bit.  If it is |
00001890  73 65 74 20 74 68 65 20  6e 75 6d 62 65 72 0d 69  |set the number.i|
000018a0  73 20 70 6f 73 69 74 69  76 65 2c 20 69 66 20 69  |s positive, if i|
000018b0  74 20 69 73 20 63 6c 65  61 72 20 74 68 65 20 6e  |t is clear the n|
000018c0  75 6d 62 65 72 20 69 73  20 6e 65 67 61 74 69 76  |umber is negativ|
000018d0  65 2e 20 20 42 75 74 0d  72 65 6d 65 6d 62 65 72  |e.  But.remember|
000018e0  20 74 68 61 74 20 74 68  69 73 20 69 73 20 75 73  | that this is us|
000018f0  69 6e 67 20 61 20 62 69  74 20 74 68 61 74 20 6f  |ing a bit that o|
00001900  75 67 68 74 20 74 6f 20  62 65 20 73 65 74 20 61  |ught to be set a|
00001910  73 20 61 0d 66 6c 61 67  20 61 6e 64 20 69 6e 20  |s a.flag and in |
00001920  66 61 63 74 20 74 68 65  20 74 6f 70 20 62 69 74  |fact the top bit|
00001930  20 69 73 20 61 63 74 75  61 6c 6c 79 20 61 6c 77  | is actually alw|
00001940  61 79 73 20 73 65 74 2e  0d 0d 4f 6e 65 20 66 69  |ays set...One fi|
00001950  6e 61 6c 20 63 6f 6e 74  72 69 76 61 6e 63 65 20  |nal contrivance |
00001960  6f 66 20 42 42 43 20 42  41 53 49 43 20 69 73 20  |of BBC BASIC is |
00001970  74 6f 20 61 64 64 20 26  38 30 20 74 6f 20 74 68  |to add &80 to th|
00001980  65 0d 65 78 70 6f 6e 65  6e 74 20 74 6f 20 61 69  |e.exponent to ai|
00001990  64 20 69 6e 20 63 61 6c  63 75 6c 61 74 69 6e 67  |d in calculating|
000019a0  2c 20 73 6f 20 49 20 68  61 76 65 20 64 6f 6e 65  |, so I have done|
000019b0  20 74 68 61 74 20 62 65  6c 6f 77 2e 0d 0d 48 65  | that below...He|
000019c0  6e 63 65 2c 20 74 68 69  73 20 69 73 20 74 68 65  |nce, this is the|
000019d0  20 72 65 70 72 65 73 65  6e 74 61 74 69 6f 6e 20  | representation |
000019e0  6f 66 20 33 2e 32 35 0d  0d 0d 20 20 45 78 70 6f  |of 3.25...  Expo|
000019f0  6e 65 6e 74 20 20 20 20  20 4d 61 6e 74 69 73 73  |nent     Mantiss|
00001a00  61 0d 20 20 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |a.  ------------|
00001a10  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001a30  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 0d 20 20 7c  |------------.  ||
00001a40  20 31 30 30 30 30 30 31  30 20 7c 20 30 31 30 31  | 10000010 | 0101|
00001a50  30 30 30 30 20 7c 20 30  30 30 30 30 30 30 30 20  |0000 | 00000000 |
00001a60  7c 20 30 30 30 30 30 30  30 30 20 7c 20 30 30 30  || 00000000 | 000|
00001a70  30 30 30 30 30 20 7c 0d  20 20 2d 2d 2d 2d 2d 2d  |00000 |.  ------|
00001a80  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001ab0  2d 2d 0d 20 20 20 20 26  38 32 20 20 20 20 20 20  |--.    &82      |
00001ac0  20 20 26 35 30 20 20 20  20 20 20 20 20 26 30 30  |  &50        &00|
00001ad0  20 20 20 20 20 20 20 20  26 30 30 20 20 20 20 20  |        &00     |
00001ae0  20 20 20 26 30 30 0d 0d  61 6e 64 20 74 68 69 73  |   &00..and this|
00001af0  20 6f 66 20 2d 33 2e 32  35 0d 0d 20 20 45 78 70  | of -3.25..  Exp|
00001b00  6f 6e 65 6e 74 20 20 20  20 20 4d 61 6e 74 69 73  |onent     Mantis|
00001b10  73 61 0d 20 20 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |sa.  -----------|
00001b20  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001b40  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 0d 20 20  |-------------.  |
00001b50  7c 20 31 30 30 30 30 30  31 30 20 7c 20 31 31 30  || 10000010 | 110|
00001b60  31 30 30 30 30 20 7c 20  30 30 30 30 30 30 30 30  |10000 | 00000000|
00001b70  20 7c 20 30 30 30 30 30  30 30 30 20 7c 20 30 30  | | 00000000 | 00|
00001b80  30 30 30 30 30 30 20 7c  0d 20 20 2d 2d 2d 2d 2d  |000000 |.  -----|
00001b90  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001bc0  2d 2d 2d 0d 20 20 20 20  26 38 32 20 20 20 20 20  |---.    &82     |
00001bd0  20 20 20 26 44 30 20 20  20 20 20 20 20 20 26 30  |   &D0        &0|
00001be0  30 20 20 20 20 20 20 20  20 26 30 30 20 20 20 20  |0        &00    |
00001bf0  20 20 20 20 26 30 30 0d  0d 5a 65 72 6f 20 69 73  |    &00..Zero is|
00001c00  20 73 74 6f 72 65 64 20  61 73 20 66 69 76 65 20  | stored as five |
00001c10  7a 65 72 6f 20 62 79 74  65 73 2e 0d 0d 49 6e 20  |zero bytes...In |
00001c20  63 61 6c 63 75 6c 61 74  69 6e 67 20 77 69 74 68  |calculating with|
00001c30  20 66 6c 6f 61 74 69 6e  67 20 70 6f 69 6e 74 20  | floating point |
00001c40  6e 75 6d 62 65 72 73 20  6f 66 20 74 68 69 73 20  |numbers of this |
00001c50  66 6f 72 6d 61 74 20 77  65 0d 68 61 76 65 20 74  |format we.have t|
00001c60  68 72 65 65 20 6f 70 65  72 61 74 69 6f 6e 73 2e  |hree operations.|
00001c70  20 20 46 69 72 73 74 6c  79 20 77 65 20 63 61 72  |  Firstly we car|
00001c80  72 79 20 6f 75 74 20 74  68 65 20 63 61 6c 63 75  |ry out the calcu|
00001c90  6c 61 74 69 6f 6e 0d 6f  6e 20 74 68 65 20 6d 61  |lation.on the ma|
00001ca0  6e 74 69 73 73 61 65 2c  20 73 65 63 6f 6e 64 6c  |ntissae, secondl|
00001cb0  79 20 77 65 20 63 61 72  72 79 20 6f 75 74 20 74  |y we carry out t|
00001cc0  68 65 20 6f 70 65 72 61  74 69 6f 6e 20 6f 6e 20  |he operation on |
00001cd0  74 68 65 0d 65 78 70 6f  6e 65 6e 74 73 20 61 6e  |the.exponents an|
00001ce0  64 20 66 69 6e 61 6c 6c  79 20 77 65 20 6e 6f 72  |d finally we nor|
00001cf0  6d 61 6c 69 73 65 20 74  68 65 20 72 65 73 75 6c  |malise the resul|
00001d00  74 2e 0d 0d 54 68 65 20  70 72 6f 67 72 61 6d 20  |t...The program |
00001d10  69 6e 20 74 68 69 73 20  6d 6f 64 75 6c 65 20 74  |in this module t|
00001d20  61 6b 65 73 20 61 20 70  61 69 72 20 6f 66 20 6e  |akes a pair of n|
00001d30  75 6d 62 65 72 73 20 79  6f 75 20 65 6e 74 65 72  |umbers you enter|
00001d40  0d 61 6e 64 20 63 61 6c  63 75 6c 61 74 65 73 20  |.and calculates |
00001d50  74 68 65 20 73 75 6d 20  61 6e 64 20 64 69 66 66  |the sum and diff|
00001d60  65 72 65 6e 63 65 20 6f  66 20 74 68 65 6d 20 75  |erence of them u|
00001d70  73 69 6e 67 20 6d 6f 64  69 66 69 65 64 0d 76 65  |sing modified.ve|
00001d80  72 73 69 6f 6e 73 20 6f  66 20 74 68 65 20 6d 75  |rsions of the mu|
00001d90  6c 74 69 2d 62 79 74 65  20 61 72 69 74 68 6d 65  |lti-byte arithme|
00001da0  74 69 63 61 6c 20 72 6f  75 74 69 6e 65 73 20 49  |tical routines I|
00001db0  20 68 61 76 65 0d 69 6e  74 72 6f 64 75 63 65 64  | have.introduced|
00001dc0  20 65 61 72 6c 69 65 72  20 69 6e 20 74 68 65 20  | earlier in the |
00001dd0  73 65 72 69 65 73 2e 0d  0d 49 20 77 6f 6e 27 74  |series...I won't|
00001de0  20 67 6f 20 69 6e 74 6f  20 67 72 65 61 74 20 64  | go into great d|
00001df0  65 74 61 69 6c 20 68 65  72 65 20 61 62 6f 75 74  |etail here about|
00001e00  20 74 68 65 20 70 72 6f  67 72 61 6d 20 73 69 6e  | the program sin|
00001e10  63 65 20 69 74 0d 69 73  20 63 6f 70 69 6f 75 73  |ce it.is copious|
00001e20  6c 79 20 61 6e 6e 6f 74  61 74 65 64 20 62 75 74  |ly annotated but|
00001e30  20 61 20 66 65 77 20 70  6f 69 6e 74 73 20 61 72  | a few points ar|
00001e40  65 20 77 6f 72 74 68 0d  6d 65 6e 74 69 6f 6e 69  |e worth.mentioni|
00001e50  6e 67 2e 0d 0d 54 68 65  20 69 6e 70 75 74 20 6f  |ng...The input o|
00001e60  66 20 74 68 65 20 66 6c  6f 61 74 69 6e 67 20 70  |f the floating p|
00001e70  6f 69 6e 74 20 6e 75 6d  62 65 72 73 20 69 73 20  |oint numbers is |
00001e80  64 6f 6e 65 20 75 73 69  6e 67 20 61 0d 70 73 65  |done using a.pse|
00001e90  75 64 6f 20 4f 50 54 20  66 75 6e 63 74 69 6f 6e  |udo OPT function|
00001ea0  20 64 65 66 69 6e 65 64  20 61 74 20 74 68 65 20  | defined at the |
00001eb0  65 6e 64 20 6f 66 20 74  68 65 20 70 72 6f 67 72  |end of the progr|
00001ec0  61 6d 2e 20 20 49 74 0d  72 65 6c 69 65 73 20 6f  |am.  It.relies o|
00001ed0  6e 20 75 73 20 6b 6e 6f  77 69 6e 67 20 77 68 65  |n us knowing whe|
00001ee0  72 65 20 42 41 53 49 43  20 77 69 6c 6c 20 73 74  |re BASIC will st|
00001ef0  6f 72 65 20 74 68 65 20  66 69 72 73 74 0d 76 61  |ore the first.va|
00001f00  72 69 61 62 6c 65 20 62  65 67 69 6e 6e 69 6e 67  |riable beginning|
00001f10  20 77 69 74 68 20 61 20  60 20 73 69 67 6e 20 28  | with a ` sign (|
00001f20  70 6f 75 6e 64 29 2e 20  20 57 65 20 63 61 6e 20  |pound).  We can |
00001f30  75 73 65 20 74 68 69 73  0d 6b 6e 6f 77 6c 65 64  |use this.knowled|
00001f40  67 65 20 74 6f 20 62 6f  74 68 20 70 75 74 20 61  |ge to both put a|
00001f50  6e 64 20 72 65 74 72 69  65 76 65 20 42 42 43 20  |nd retrieve BBC |
00001f60  42 41 53 49 43 20 66 6f  72 6d 61 74 20 66 6c 6f  |BASIC format flo|
00001f70  61 74 69 6e 67 0d 70 6f  69 6e 74 20 6e 75 6d 62  |ating.point numb|
00001f80  65 72 73 2e 0d 0d 49 72  6f 6e 69 63 61 6c 6c 79  |ers...Ironically|
00001f90  2c 20 61 6c 74 68 6f 75  67 68 20 68 61 76 69 6e  |, although havin|
00001fa0  67 20 73 65 70 61 72 61  74 65 6c 79 20 73 69 67  |g separately sig|
00001fb0  6e 65 64 20 6e 75 6d 62  65 72 73 20 6d 61 6b 65  |ned numbers make|
00001fc0  73 0d 6c 69 66 65 20 65  61 73 69 65 72 20 66 6f  |s.life easier fo|
00001fd0  72 20 6d 75 6c 74 69 70  6c 69 63 61 74 69 6f 6e  |r multiplication|
00001fe0  20 61 6e 64 20 64 69 76  69 73 69 6f 6e 20 28 6e  | and division (n|
00001ff0  65 78 74 20 6d 6f 64 75  6c 65 29 20 69 74 0d 61  |ext module) it.a|
00002000  63 74 75 61 6c 6c 79 20  63 6f 6d 70 6c 69 63 61  |ctually complica|
00002010  74 65 73 20 61 64 64 69  74 69 6f 6e 20 61 6e 64  |tes addition and|
00002020  20 73 75 62 74 72 61 63  74 69 6f 6e 20 6d 6f 72  | subtraction mor|
00002030  65 20 74 68 61 6e 0d 73  6f 6d 65 77 68 61 74 2e  |e than.somewhat.|
00002040  20 20 49 20 68 61 76 65  20 69 6e 74 72 6f 64 75  |  I have introdu|
00002050  63 65 64 20 61 6e 20 65  78 74 72 61 20 62 79 74  |ced an extra byt|
00002060  65 20 74 6f 20 61 63 74  20 61 73 20 61 6e 0d 6f  |e to act as an.o|
00002070  76 65 72 66 6c 6f 77 20  77 69 74 68 20 65 61 63  |verflow with eac|
00002080  68 20 6f 66 20 74 68 65  20 6e 75 6d 62 65 72 73  |h of the numbers|
00002090  20 69 6e 20 74 68 65 20  77 6f 72 6b 73 70 61 63  | in the workspac|
000020a0  65 20 73 6f 20 74 68 61  74 0d 77 65 20 63 61 6e  |e so that.we can|
000020b0  20 63 6f 6e 76 65 72 74  20 6e 65 67 61 74 69 76  | convert negativ|
000020c0  65 20 6e 75 6d 62 65 72  73 20 74 6f 20 32 27 73  |e numbers to 2's|
000020d0  20 63 6f 6d 70 6c 65 6d  65 6e 74 20 61 6e 64 20  | complement and |
000020e0  62 61 63 6b 0d 61 67 61  69 6e 2e 20 20 49 6e 20  |back.again.  In |
000020f0  66 61 63 74 20 74 68 65  20 42 41 53 49 43 20 66  |fact the BASIC f|
00002100  70 20 72 6f 75 74 69 6e  65 73 20 61 6c 73 6f 20  |p routines also |
00002110  75 73 65 20 61 20 72 6f  75 6e 64 69 6e 67 0d 62  |use a rounding.b|
00002120  79 74 65 20 74 6f 20 69  6d 70 72 6f 76 65 20 70  |yte to improve p|
00002130  72 65 63 69 73 69 6f 6e  20 62 75 74 20 74 68 61  |recision but tha|
00002140  74 20 64 6f 65 73 20 6e  6f 74 20 73 65 65 6d 20  |t does not seem |
00002150  74 6f 20 62 65 0d 6e 65  63 65 73 73 61 72 79 20  |to be.necessary |
00002160  68 65 72 65 2e 0d 0d 54  68 65 20 6d 61 69 6e 20  |here...The main |
00002170  72 6f 75 74 69 6e 65 73  20 66 6f 72 20 61 64 64  |routines for add|
00002180  69 74 69 6f 6e 20 61 6e  64 20 73 75 62 74 72 61  |ition and subtra|
00002190  63 74 69 6f 6e 20 61 72  65 0d 73 74 72 61 69 67  |ction are.straig|
000021a0  68 74 66 6f 72 77 61 72  64 20 65 6e 6f 75 67 68  |htforward enough|
000021b0  2e 20 20 54 68 65 79 20  61 72 65 20 62 61 73 69  |.  They are basi|
000021c0  63 61 6c 6c 79 20 66 69  76 65 20 62 79 74 65 0d  |cally five byte.|
000021d0  6d 75 6c 74 69 70 6c 65  20 70 72 65 63 69 73 69  |multiple precisi|
000021e0  6f 6e 20 73 69 6d 69 6c  61 72 20 74 6f 20 74 68  |on similar to th|
000021f0  6f 73 65 20 69 6e 20 65  61 72 6c 69 65 72 20 6d  |ose in earlier m|
00002200  6f 64 75 6c 65 73 2e 20  20 54 68 65 0d 63 75 72  |odules.  The.cur|
00002210  69 6f 75 73 20 64 69 66  66 65 72 65 6e 63 65 20  |ious difference |
00002220  69 73 20 74 68 61 74 2c  20 61 73 20 49 20 6d 65  |is that, as I me|
00002230  6e 74 69 6f 6e 65 64 20  62 65 66 6f 72 65 2c 20  |ntioned before, |
00002240  74 68 65 20 62 79 74 65  73 0d 61 72 65 20 73 74  |the bytes.are st|
00002250  6f 72 65 64 20 62 61 63  6b 20 74 6f 20 66 72 6f  |ored back to fro|
00002260  6e 74 20 77 69 74 68 20  74 68 65 20 6d 6f 73 74  |nt with the most|
00002270  20 73 69 67 6e 69 66 69  63 61 6e 74 20 62 79 74  | significant byt|
00002280  65 20 69 6e 0d 74 68 65  20 6c 6f 77 65 73 74 20  |e in.the lowest |
00002290  6d 65 6d 6f 72 79 20 6c  6f 63 61 74 69 6f 6e 2e  |memory location.|
000022a0  0d 0d 49 6e 20 74 68 65  20 6d 61 69 6e 20 73 65  |..In the main se|
000022b0  63 74 69 6f 6e 20 6f 66  20 74 68 65 20 70 72 6f  |ction of the pro|
000022c0  67 72 61 6d 20 74 68 65  72 65 20 61 72 65 20 74  |gram there are t|
000022d0  68 65 73 65 0d 73 75 62  72 6f 75 74 69 6e 65 73  |hese.subroutines|
000022e0  3a 0d 0d 27 6d 6f 76 65  5f 73 69 67 6e 5f 62 69  |:..'move_sign_bi|
000022f0  74 27 20 6d 6f 76 65 73  20 74 68 65 20 73 69 67  |t' moves the sig|
00002300  6e 20 62 69 74 73 20 66  72 6f 6d 20 74 68 65 20  |n bits from the |
00002310  74 6f 70 20 6f 66 20 74  68 65 0d 6d 61 6e 74 69  |top of the.manti|
00002320  73 73 61 65 20 61 6e 64  20 70 75 74 73 20 74 68  |ssae and puts th|
00002330  65 6d 20 69 6e 74 6f 20  61 20 73 65 70 61 72 61  |em into a separa|
00002340  74 65 20 62 79 74 65 20  66 6f 72 20 6c 61 74 65  |te byte for late|
00002350  72 20 75 73 65 2e 0d 0d  27 65 71 75 61 74 65 5f  |r use...'equate_|
00002360  65 78 70 6f 6e 65 6e 74  73 27 20 64 65 6e 6f 72  |exponents' denor|
00002370  6d 61 6c 69 73 65 73 20  74 68 65 20 73 6d 61 6c  |malises the smal|
00002380  6c 65 72 20 6f 66 20 74  68 65 20 74 77 6f 0d 6e  |ler of the two.n|
00002390  75 6d 62 65 72 73 20 73  6f 20 74 68 61 74 20 74  |umbers so that t|
000023a0  68 65 20 65 78 70 6f 6e  65 6e 74 73 20 6f 66 20  |he exponents of |
000023b0  74 68 65 20 74 77 6f 20  6e 75 6d 62 65 72 73 20  |the two numbers |
000023c0  61 72 65 20 74 68 65 0d  73 61 6d 65 2e 20 20 49  |are the.same.  I|
000023d0  74 20 64 6f 65 73 20 74  68 69 73 20 62 79 20 72  |t does this by r|
000023e0  6f 74 61 74 69 6e 67 20  74 68 65 20 6d 61 6e 74  |otating the mant|
000023f0  69 73 73 61 20 72 69 67  68 74 20 28 77 68 69 63  |issa right (whic|
00002400  68 0d 6d 61 6b 65 73 20  69 74 20 73 6d 61 6c 6c  |h.makes it small|
00002410  65 72 29 20 77 68 69 6c  65 20 69 6e 63 72 65 61  |er) while increa|
00002420  73 69 6e 67 20 74 68 65  20 65 78 70 6f 6e 65 6e  |sing the exponen|
00002430  74 20 75 6e 74 69 6c 20  74 68 65 0d 65 78 70 6f  |t until the.expo|
00002440  6e 65 6e 74 73 20 6d 61  74 63 68 2e 0d 0d 27 6e  |nents match...'n|
00002450  65 67 5f 63 6f 6e 76 65  72 74 27 20 6d 61 6b 65  |eg_convert' make|
00002460  73 20 61 6e 79 20 6e 65  67 61 74 69 76 65 20 6e  |s any negative n|
00002470  75 6d 62 65 72 73 20 69  6e 74 6f 20 32 27 73 0d  |umbers into 2's.|
00002480  63 6f 6d 70 6c 65 6d 65  6e 74 2c 20 6d 61 6b 69  |complement, maki|
00002490  6e 67 20 75 73 65 20 6f  66 20 74 68 65 20 65 78  |ng use of the ex|
000024a0  74 72 61 20 6f 76 65 72  66 6c 6f 77 20 62 79 74  |tra overflow byt|
000024b0  65 2e 0d 0d 54 68 65 6e  20 63 6f 6d 65 20 74 68  |e...Then come th|
000024c0  65 20 61 64 64 69 74 69  6f 6e 20 6f 72 20 73 75  |e addition or su|
000024d0  62 74 72 61 63 74 69 6f  6e 20 73 75 62 72 6f 75  |btraction subrou|
000024e0  74 69 6e 65 73 2e 0d 0d  27 6e 65 67 5f 63 6f 6e  |tines...'neg_con|
000024f0  76 65 72 74 5f 62 61 63  6b 27 20 74 61 6b 65 73  |vert_back' takes|
00002500  20 61 6e 79 20 72 65 73  75 6c 74 20 74 68 61 74  | any result that|
00002510  20 69 73 20 32 27 73 20  63 6f 6d 70 6c 65 6d 65  | is 2's compleme|
00002520  6e 74 0d 6e 65 67 61 74  69 76 65 20 61 6e 64 20  |nt.negative and |
00002530  6d 61 6b 65 73 20 69 74  20 70 6f 73 69 74 69 76  |makes it positiv|
00002540  65 20 77 68 69 6c 65 20  6d 6f 64 69 66 79 69 6e  |e while modifyin|
00002550  67 20 69 74 73 20 73 69  67 6e 0d 62 79 74 65 2e  |g its sign.byte.|
00002560  0d 0d 54 68 65 6e 20 77  65 20 74 72 61 70 20 61  |..Then we trap a|
00002570  20 7a 65 72 6f 20 72 65  73 75 6c 74 20 73 69 6e  | zero result sin|
00002580  63 65 20 74 68 65 20 7a  65 72 6f 20 66 6f 72 6d  |ce the zero form|
00002590  61 74 20 69 73 0d 6e 6f  6e 2d 73 74 61 6e 64 61  |at is.non-standa|
000025a0  72 64 20 61 6e 64 20 77  6f 75 6c 64 20 70 75 74  |rd and would put|
000025b0  20 74 68 65 20 72 65 6e  6f 72 6d 61 6c 69 73 69  | the renormalisi|
000025c0  6e 67 20 72 6f 75 74 69  6e 65 20 69 6e 74 6f 20  |ng routine into |
000025d0  61 6e 0d 65 6e 64 6c 65  73 73 20 6c 6f 6f 70 2e  |an.endless loop.|
000025e0  0d 0d 27 72 65 6e 6f 72  6d 61 6c 69 73 65 27 20  |..'renormalise' |
000025f0  61 64 6a 75 73 74 73 20  74 68 65 20 72 65 73 75  |adjusts the resu|
00002600  6c 74 20 75 6e 74 69 6c  20 69 74 20 69 73 20 61  |lt until it is a|
00002610  20 6e 6f 72 6d 61 6c 69  73 65 64 0d 6e 75 6d 62  | normalised.numb|
00002620  65 72 20 28 77 69 74 68  20 74 68 65 20 74 6f 70  |er (with the top|
00002630  20 62 69 74 20 6f 66 20  74 68 65 20 6d 61 6e 74  | bit of the mant|
00002640  69 73 73 61 20 73 65 74  29 20 61 73 20 65 78 70  |issa set) as exp|
00002650  65 63 74 65 64 20 62 79  0d 42 41 53 49 43 2e 20  |ected by.BASIC. |
00002660  20 54 68 65 20 65 78 70  6f 6e 65 6e 74 20 69 73  | The exponent is|
00002670  20 61 64 6a 75 73 74 65  64 20 61 63 63 6f 72 64  | adjusted accord|
00002680  69 6e 67 6c 79 2e 20 20  49 66 20 74 68 65 72 65  |ingly.  If there|
00002690  20 69 73 0d 61 6e 79 74  68 69 6e 67 20 69 6e 20  | is.anything in |
000026a0  74 68 65 20 6f 76 65 72  66 6c 6f 77 20 62 79 74  |the overflow byt|
000026b0  65 20 74 68 65 6e 20 74  68 65 20 77 68 6f 6c 65  |e then the whole|
000026c0  20 6d 61 6e 74 69 73 73  61 0d 28 69 6e 63 6c 75  | mantissa.(inclu|
000026d0  64 69 6e 67 20 74 68 65  20 6f 76 65 72 66 6c 6f  |ding the overflo|
000026e0  77 20 62 79 74 65 29 20  69 73 20 72 6f 74 61 74  |w byte) is rotat|
000026f0  65 64 20 72 69 67 68 74  20 75 6e 74 69 6c 20 74  |ed right until t|
00002700  68 65 0d 6f 76 65 72 66  6c 6f 77 20 62 79 74 65  |he.overflow byte|
00002710  20 69 73 20 65 6d 70 74  79 2e 20 20 54 68 65 20  | is empty.  The |
00002720  65 78 70 6f 6e 65 6e 74  20 69 73 20 69 6e 63 72  |exponent is incr|
00002730  65 61 73 65 64 20 62 79  20 6f 6e 65 0d 66 6f 72  |eased by one.for|
00002740  20 65 61 63 68 20 72 6f  74 61 74 69 6f 6e 2e 20  | each rotation. |
00002750  20 49 66 2c 20 64 75 72  69 6e 67 20 74 68 65 20  | If, during the |
00002760  63 6f 75 72 73 65 20 6f  66 20 74 68 69 73 20 69  |course of this i|
00002770  6e 63 72 65 6d 65 6e 74  2c 0d 74 68 65 20 65 78  |ncrement,.the ex|
00002780  70 6f 6e 65 6e 74 20 62  65 63 6f 6d 65 73 20 7a  |ponent becomes z|
00002790  65 72 6f 20 74 68 65 6e  20 77 65 20 68 61 76 65  |ero then we have|
000027a0  20 67 65 6e 65 72 61 74  65 64 20 61 20 6e 75 6d  | generated a num|
000027b0  62 65 72 0d 74 6f 6f 20  6c 61 72 67 65 20 66 6f  |ber.too large fo|
000027c0  72 20 74 68 65 20 73 79  73 74 65 6d 20 74 6f 20  |r the system to |
000027d0  68 61 6e 64 6c 65 2c 20  61 6e 64 20 61 6e 20 65  |handle, and an e|
000027e0  72 72 6f 72 20 69 73 0d  67 65 6e 65 72 61 74 65  |rror is.generate|
000027f0  64 2e 20 20 49 74 20 68  61 73 20 74 68 65 20 6e  |d.  It has the n|
00002800  75 6d 62 65 72 20 32 30  20 62 75 74 20 49 20 68  |umber 20 but I h|
00002810  61 76 65 20 67 69 76 65  6e 20 69 74 20 61 0d 64  |ave given it a.d|
00002820  69 66 66 65 72 65 6e 74  20 6d 65 73 73 61 67 65  |ifferent message|
00002830  20 74 6f 20 74 68 65 20  73 74 61 6e 64 61 72 64  | to the standard|
00002840  20 27 74 6f 6f 20 62 69  67 27 2e 0d 0d 49 66 20  | 'too big'...If |
00002850  74 68 65 20 6f 76 65 72  66 6c 6f 77 20 62 79 74  |the overflow byt|
00002860  65 20 69 73 20 65 6d 70  74 79 20 74 68 65 6e 20  |e is empty then |
00002870  74 68 65 20 72 65 73 74  20 6f 66 20 74 68 65 20  |the rest of the |
00002880  6d 61 6e 74 69 73 73 61  0d 69 73 20 72 6f 74 61  |mantissa.is rota|
00002890  74 65 64 20 6c 65 66 74  20 75 6e 74 69 6c 20 74  |ted left until t|
000028a0  68 65 20 74 6f 70 20 62  69 74 20 6f 66 20 74 68  |he top bit of th|
000028b0  65 20 6d 61 6e 74 69 73  73 61 20 69 73 20 73 65  |e mantissa is se|
000028c0  74 2e 20 0d 54 68 65 20  65 78 70 6f 6e 65 6e 74  |t. .The exponent|
000028d0  20 69 73 20 64 65 63 72  65 61 73 65 64 20 62 79  | is decreased by|
000028e0  20 6f 6e 65 20 66 6f 72  20 65 61 63 68 20 72 6f  | one for each ro|
000028f0  74 61 74 69 6f 6e 2e 20  20 4f 66 0d 63 6f 75 72  |tation.  Of.cour|
00002900  73 65 20 69 66 20 74 68  65 20 74 6f 70 20 62 69  |se if the top bi|
00002910  74 20 69 73 20 61 6c 72  65 61 64 79 20 73 65 74  |t is already set|
00002920  20 74 68 65 6e 20 74 68  65 20 6e 75 6d 62 65 72  | then the number|
00002930  20 69 73 0d 63 6f 72 72  65 63 74 2e 0d 0d 46 69  | is.correct...Fi|
00002940  6e 61 6c 6c 79 20 74 68  65 20 72 6f 75 74 69 6e  |nally the routin|
00002950  65 20 61 74 20 27 72 65  70 6c 61 63 65 5f 74 6f  |e at 'replace_to|
00002960  70 5f 62 69 74 27 20 63  61 6c 63 75 6c 61 74 65  |p_bit' calculate|
00002970  73 20 74 68 65 20 6e 65  77 0d 73 69 67 6e 20 62  |s the new.sign b|
00002980  69 74 20 61 6e 64 20 72  65 70 6c 61 63 65 73 20  |it and replaces |
00002990  74 68 65 20 74 6f 70 20  62 69 74 20 6f 66 20 74  |the top bit of t|
000029a0  68 65 20 6d 61 6e 74 69  73 73 61 20 77 69 74 68  |he mantissa with|
000029b0  20 69 74 2e 0d 0d 57 68  65 6e 20 79 6f 75 20 72  | it...When you r|
000029c0  75 6e 20 74 68 65 20 70  72 6f 67 72 61 6d 20 79  |un the program y|
000029d0  6f 75 20 6d 61 79 20 68  61 76 65 20 74 6f 20 62  |ou may have to b|
000029e0  65 20 69 6e 20 4d 4f 44  45 20 37 0d 62 65 63 61  |e in MODE 7.beca|
000029f0  75 73 65 20 74 68 65 20  61 73 73 65 6d 62 65 72  |use the assember|
00002a00  20 63 6f 64 65 20 69 73  20 72 61 74 68 65 72 20  | code is rather |
00002a10  6c 6f 6e 67 2e 20 20 20  59 6f 75 20 65 6e 74 65  |long.   You ente|
00002a20  72 20 61 0d 63 6f 75 70  6c 65 20 6f 66 20 6e 75  |r a.couple of nu|
00002a30  6d 62 65 72 73 20 6f 66  20 76 69 72 74 75 61 6c  |mbers of virtual|
00002a40  6c 79 20 61 6e 79 20 73  69 7a 65 2c 20 61 6e 64  |ly any size, and|
00002a50  20 79 6f 75 20 77 69 6c  6c 20 62 65 0d 61 62 6c  | you will be.abl|
00002a60  65 20 74 6f 20 63 6f 6d  70 61 72 65 20 74 68 65  |e to compare the|
00002a70  20 61 64 64 69 74 69 6f  6e 20 61 6e 64 20 73 75  | addition and su|
00002a80  62 74 72 61 63 74 69 6f  6e 20 72 65 73 75 6c 74  |btraction result|
00002a90  73 20 66 72 6f 6d 0d 74  68 69 73 20 6d 61 63 68  |s from.this mach|
00002aa0  69 6e 65 20 63 6f 64 65  20 61 6e 64 20 66 72 6f  |ine code and fro|
00002ab0  6d 20 42 41 53 49 43 2e  20 20 54 68 65 20 6d 61  |m BASIC.  The ma|
00002ac0  63 68 69 6e 65 20 63 6f  64 65 20 68 65 72 65 20  |chine code here |
00002ad0  69 73 0d 61 6c 6d 6f 73  74 20 74 77 69 63 65 20  |is.almost twice |
00002ae0  61 73 20 66 61 73 74 20  61 73 20 42 41 53 49 43  |as fast as BASIC|
00002af0  20 61 6c 74 68 6f 75 67  68 20 69 6e 20 73 6f 6d  | although in som|
00002b00  65 20 63 69 72 63 75 6d  73 74 61 6e 63 65 73 0d  |e circumstances.|
00002b10  69 74 20 69 73 20 6e 6f  74 20 71 75 69 74 65 20  |it is not quite |
00002b20  61 73 20 70 72 65 63 69  73 65 2e 20 20 54 68 69  |as precise.  Thi|
00002b30  73 20 69 73 20 6f 6e 6c  79 20 6c 69 6b 65 6c 79  |s is only likely|
00002b40  20 74 6f 20 73 68 6f 77  20 61 73 0d 61 20 64 69  | to show as.a di|
00002b50  66 66 65 72 65 6e 63 65  20 6f 66 20 31 20 69 6e  |fference of 1 in|
00002b60  20 74 68 65 20 73 6d 61  6c 6c 65 73 74 20 73 69  | the smallest si|
00002b70  67 6e 69 66 69 63 61 6e  74 20 64 69 67 69 74 20  |gnificant digit |
00002b80  62 65 74 77 65 65 6e 0d  74 68 65 20 74 77 6f 20  |between.the two |
00002b90  70 72 6f 66 66 65 72 65  64 20 72 65 73 75 6c 74  |proffered result|
00002ba0  73 2e 0d 0d 4e 65 78 74  20 74 69 6d 65 20 6d 75  |s...Next time mu|
00002bb0  6c 74 69 70 6c 69 63 61  74 69 6f 6e 20 61 6e 64  |ltiplication and|
00002bc0  20 64 69 76 69 73 69 6f  6e 2e 0d                 | division..|
00002bcb
15-04-88/T\OSB22.m0
15-04-88/T\OSB22.m1
15-04-88/T\OSB22.m2
15-04-88/T\OSB22.m4
15-04-88/T\OSB22.m5