HX-20 Technical Note

Using the Trigonometric Functions


The HX-20 has useful routines for SIN, COS, TAN and ATN built into the BASIC ROM. This technical note describes how to call them.

1. Addresses

The routines are located at the following addresses in Bank 0, (the internal bank of memory):

Routine Address
ATN$C040
SIN$CF69
COS$CF6F
TAN$CFBB

2. Floating point format used

The routines are called with the following parameters:

Location Contents
$83$0 = Short real
$85$4 = Short real
$D5Exponent, biased $80
$D6,$D7,$D8Mantissa
$DDMantissa sign flag:
$0 = positive
$FF = negative

Thus:

an exponent of $7F = 2^-1
an exponent of $80 = 2^0
an exponent of $81 = 2^1
and so on...

Note also that an exponent of zero represents zero, regardless of the value of the mantissa.

The mantissa is stored most significant byte first. The binary point is to the left of the most significant bit in $D5. All mantissas are between 0.5 and 1, and hence the most significant bit is always one.

For example:

Location
$D5 $D6 $D7 $D8 $DD Number
Contents: $75 $99 $99 $9A $FF -0.3
$75 $99 $99 $9A $0 +0.3
$80 $80 $0 $0 $0 +0.5

This representation is NOT the same as that used to store variables in BASIC, although it can be easily converted to that format.

3. Results

The routines return a result in the same format in locations $D5-$D8 and $DD. Only bit 7 of location $DD is significant. Other locations from $80 to $FF may be altered.

Note that the routines are NOT re-entrant, and thus must not be called when BASIC is running, as BASIC uses the same areas of memory for all functions.

4. Example Program

The following code for my HX-20 Assembler shows how to use the built-in trigonometric functions.

100 CLEAR 200,0:MEMSET&HB00:DEFINT A-Z:FOR I=1 TO 2:GOSUB 180:NEXT I
110 A!=0:X=VARPTR(A!):POKE AADR,X\256:POKE AADR+1,X MOD 256
120 B!=0:X=VARPTR(B!):POKE BADR,X\256:POKE BADR+1,X MOD 256
130 INPUT "Value ";A!
140 EXEC DOATN
150 PRINT "ATN=";B!
160 GOTO 130
170 REM ------------------------------------
180 ASM I
190 OBJ
200 HXATN EQU $C040
210 ;
220 FP    EQU $D5
230 ;
240 FPSGN EQU $DD
250 ;
260       ORG $A49
270 AADR  RDB 1
280 BADR  RDB 1
290 ;
300 DOATN EQU *
310  CLR +$83       ;CLEAR PRECISION FLAG
320  LDA A #4
330  STA A +$85     ;4 BYTES FOR SINGLE REALS
340  LDX AADR
350  LDA A X+0      ;EXPONENT
360  STA A FP
370  LDA A X+1      ;MSB MANTISSA+SIGN
380  PSH A
390  ORA A #$80     ;SET MSB OF MANTISSA
400  STA A FP+1
410  PUL A
420  AND A #$80     ;SIGN FLAG
430  STA A FPSGN
440  LDA A X+2      ;MANTISSA
450  STA A FP+2
460  LDA A X+3      ;MANTISSA LSB
470  STA A FP+3
480  JSR HXATN
490  LDX BADR
500  LDA A FP       ;MANTISSA+SIGN
510  STA A X+0
520  LDA A FP+1     ;MSB MANTISSA
530  AND A #$7F     ;STRIP TOP BIT
540  LDA B FPSGN    ;SIGN
550  AND B #$80
560  ABA            ;COMBINE SIGN + MANTISSA
570  STA A X+1
580  LDA A FP+2     ;MANTISSA
590  STA A X+2
600  LDA A FP+3     ;LSB MANTISSA
610  STA A X+3
620  RTS
630 ASM OFF
640 RETURN