<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Thu, 17 Jun 1999 12:23:43 +0100
From   : Stuart William McConnachie <stuart@...>
Subject: Re: Floating Point

In message <37676176.27A8F0C0@...>, Chris Richardson
<chris@...> writes
>I was wondering if you could understood how to use the floating point
>routines discussed on page O.8-3 of the Master Reference Manual (Part
>2). I have tried point &7FE and &7FF  to single and four byte
>representations of numbers in zero page
Do not modify any of the locations from &7F0 to &7FF, these are just for
reference!

&7FE is the zero page location of a pointer to a variable stored in BBC
BASIC internal floating point format.  The format is discussed on page
L.2-8.  Not that this value is a 5 byte value (1 byte exponent, 4 byte
mantissa).  For example, if you define a basic variable "fred" which is
located at &E34 and suppose we want to make the [argp] point to that.
Then you would read location &7FE which gives you the zero page location
of [argp], which is ALWAYS &4A using BASIC 4.  You would then set zero
page &4A to &34 (LSB or variable address) and location &4B to &0E (MSB
of variable address).

&7FF this is the zero page location of the facc (Floating point
accumulator).  In contrast to the [argp], which is a 2 byte pointer to a
variable elsewhere in memory, the facc is actually stored in the zero
page locations given by &7FF.  This value is NOT stored in the same
format as the internal 5 byte floating point notation, instead it is
held to a slightly greater accuracy to prevent rounding errors and speed
up calculations.  In order to get a value into the facc, first point the
[argp] at an internal 5 byte floating point value, then call the LDA
routine via [&7FA].  Perform the arithmetic you require.  Not this may
involve any number of SQR / * + or unary - operations on the facc and
using other BASIC variables pointed to by [argv].  When you have
finished save the answer from the facc back to [argp] by using the STA
routine via [&7FC].  This will automatically convert back to internal 5
byte format for you.  Note that the facc is ALWAYS located at &2E when
using BASIC 4.

NB: You can not use the floating point routines this way from BASIC (and
fairly pointless it would be anyway as you already have direct access to
all the arithmetic routines, and more besides, through BASIC
statements!)  This is because the facc and [argp] are continually in use
by BASIC every time your BASIC program manipulates a number, even if you
aren't doing any floating point arithmetic!  You must call the floating
point routines from a BASIC _assembler_ program.  Make sure you have
BASIC selected as your current language though, because the routines use
the language workspace in zero page (fairly obvious)!

>Could you
>please explain how these routines are used and give an short example,
>because the SQR routine would be great to have use of in many programs?
Certainly.  This rather mindless example uses an assembler routine,
which is passed a BASIC floating point variable via CALL, to calculate
and return the square root of that variable.  The variable is actually
overwritten with the square root result:

        REM Create space for our assembler routine
        DIM Code% &100

        REM Two pass assembly
        FOR W%=0 TO 2 STEP 2
        P%=Code%
        [OPT W%

        \Get the zero page address of argv
        LDX &7FE

        \Initialize [argv] to point to the BASIC floating point number
        \passed via CALL (See Master Ref part 2 L.2-7)
        LDA &601 : STA &00,X
        LDA &602 : STA &01,X

        \Load the facc with the contents of [argv]
        JSR float_lda_vec

        \Perform SQR of facc
        JSR float_sqr_vec

        \Save the contents of the facc back to the variable at [argv]
        JSR float_sta_vec

        \Return to BASIC
        RTS

        \ Call BASIC routine to initialize facc
        .float_lda_vec
        JMP (&07FA)

        \ Call BASIC routine to find square root of facc
        .float_sqr_vec
        JMP (&07F0)

        \ Call BASIC routine to save contents of facc
        .float_sta_vec
        JMP (&07FC)

        ]
        NEXT

        REM Initialize a floating point number to test with
        N=PI*PI

        REM Now find the square root of N (answer should be PI!)
        CALL Code%, N

        REM And display the result.  PI - Magic!
        PRINT N

Of course we would have been much better of just using the basic SQR()
function directly!  The above code is directly equivalent to:
        N=PI*PI
        N=SQR(N)
        PRINT N

However, had we some complex maths to do, a considerable amount of time
can be saved by freeing the BASIC interpreter from having to decode our
BASIC statements and expressions.

Regards,
-- 
Stuart McConnachie (stuart@...              )
43 The Hollows, Long Eaton, Nottingham, NG10 2ES, UK
Mobile: 0966 224307
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>