[net.micro.cbm] c64 floating point, part 1

miller@uiucdcs.UUCP (01/28/84)

#N:uiucdcs:36100032:000:3404
uiucdcs!miller    Jan 28 01:01:00 1984


     Well, here is the first installment of how to use the c64 floating point
routines from assembly language.  When using these routines, keep in mind that
they reside in the c64's ROM.  Hence, if you flip that out to expose the RAM at
those addresses, you will be unable to access these routines.
     This time, I'll go over the format of floating point numbers as used in
the machine.  Later, I'll describe which routines are available and how to
call them.  (By the way, since the VIC20's Basic interpretor is almost exactly
the same as on the c64, most of what I say here applies to that machine too.
Some of the addresses may have to be changed though.)
     C64 Basic floating point numbers are broken up into three contiguous
sections: the exponent (one byte) followed by the mantissa (four bytes)
followed by the sign of the number (one byte).  I'll go over each of the parts
in turn.
     The exponent is stored in "excess 128" which means it has a value 128
higher than the true exponent of the number.  That is, exponents can have a
value of -128 to 127 but are stored as 0 to 255.  (The purpose of "excess n" is
to avoid having to deal with negative exponents.  It makes comparisons for
size much easier.)  What the exponent tells you is how many bits the mantissa
must be shifted from its stored value to give you its true value.
     The mantissa is stored in a normalized format, i.e., the binary point is
assumed to be to the left of the value and the leftmost bit is always a 1.
Hence the 32 bit values stored here represent numbers between zero and one.
     If the high order bit of the sign byte is on then the number is negative.
If it is off then the number is positive.
     So to compute the true value of the number stored you can use the follow-
ing equation:
                  0.mantissa * 2 ** (exponent - 128)
(Multiplying by powers of two are equivalent to shifting, of course).  The
above equation will give you a generic binary value.  You then look at the sign
byte's bit to determine the sign of the number and you are done!
     Confused?  Let's look at some examples.  The number 13 is 1101 in binary,
or $D in hex.  Now 1101 is going to be normalized into a four byte section,
i.e., we will place the number in such that the lead bit is a 1.  Hence our
mantissa is:
11010000 00000000 00000000 00000000
  $D0      $00      $00      $00
But remember, the binary point is assumed to be to the left of the mantissa.
So the number stored above reads .1101 which is not right.  To get the real
value out we need to shift the binary point four places to the right.  This is
the purpose of the exponent.  It has the value of 4, plus 128, or 132.
Finally, the number is positive so the sign byte is 0.  Hence, our final
product is:
exponent -------------mantissa-------------- --sign--
10000100 11010000 00000000 00000000 00000000 00000000
  $84      $D0      $00      $00      $00      $00
  132      208        0        0        0        0
     Other examples would be:
One is represented as $81 $80 $00 $00 $00 $00
Two is represented as $82 $80 $00 $00 $00 $00
Three is represented as $82 $C0 $00 $00 $00 $00
One half is represented as $80 $80 $00 $00 $00 $00
One fourth is represented as $7F $80 $00 $00 $00 $00
And -67 is represented as $87 $86 $00 $00 $00 $80
     Next time, we'll look at the calling protocol for using the subroutines.

A. Ray Miller
Univ Illinois