Apple 2_Woz_Floating_Point_Routines_for_the_6502.pdf
(
38 KB
)
Pobierz
http://www.6502.org/source/floats/wozfp1.txt
31 October 2004
TABLE OF CONTENTS
Floating Point Routines for the 6502 by Roy Rankin and Steve Wozniak
Originally published in the August 1976 issue of Dr. Dobb's Journal, these
floating point routines allow 6502 users to perform most of the more popular and
desired floating point and transcendental functions, namely: Natural Log, Common
Log, Addition, Subtraction, Multiplication, Division, and conversions between
floating and fixed point numbers.
Errata for Rankin's 6502 Floating Point Routines by Roy Rankin
In the November/December issue of Dr. Dobb's Journal Roy Rankin published three
error corrections to the Floating Point Routines presented above.
Floating Point Implementation in the Apple II by Steve Wozniak
An almost identical set of the above routines appeared in the original manual
for the Apple II (the Red Book, January 1978). Documentation for these routines
appeared in another book, the Wozpak II, in November 1979.
===========================================================================
Floating Point Routines for the 6502 by Roy Rankin and Steve Wozniak
Originally published in the August 1976 issue of Dr. Dobb's Journal, these
floating point routines allow 6502 users to perform most of the more popular and
desired floating point and transcendental functions, namely: Natural Log, Common
Log, Addition, Subtraction, Multiplication, Division, and conversions between
floating and fixed point numbers.
Dr. Dobb's Journal, August 1976, pages 17-19.
Floating Point Routines for the 6502
by Roy Rankin, Department of Mechanical Engineering,
Stanford University, Stanford, CA 94305
(415) 497-1822
and
Steve Wozniak, Apple Computer Company
770 Welch Road, Suite 154
Palo Alto, CA 94304
(415) 326-4248
Editor's Note: Although these routines are for the 6502, it
would appear that one could generate equivalent routines for
most of the "traditional" microprocessors, relatively easily,
by following the flow of the algorithms given in the excellent
comments included in the program listing. This is particularly
true of the transcendental functions, which were directly modeled
after well-known and proven algorithms, and for which, the
comments are relatively machine independent.
These floating point routines allow 6502 users to perform
most of the more popular and desired floating point and
transcendental functions, namely:
Natural Log - LOG
Common Log - LOG10
Exponential - EXP
Floating Add - FADD
Floating Subtract - FSUB
Floating Multiply - FMUL
Floating Divide - FDIV
Convert Floating to Fixed - FIX
Convert Fixed to Floating - FLOAT
They presume a four-byte floating point operand consisting of
a one-byte exponent ranging from -128 to +127 and a
24-bit two's complement mantissa between 1.0 and 2.0.
The floating point routines were done by Steve Wozniak,
one of the principals in Apple Computer Company. The
transcendental functions were patterned after those offered by
Hewlett-Packard for their HP2100 minicomputer (with some
modifications), and were done by Roy Rankin, a Ph.D. student
at Stanford University.
There are three error traps; two for overflow, and one for
prohibited logarithm argument. ERROR (1D06) is the error
exit used in the event of a non-positive log argument. OVFLW
(1E3B) is the error exit for overflow occuring during calculation
of e to some power. OVFL (1FE4) is the error exit for
overflow in all of the floating point routines. There is no
trap for underflow; in such cases, the result is set to 0.0.
All routines are called and exited in a uniform manner:
The arguments(s) are placed in the specified floating point
storage locations (for specifics, see the documentation preceeding
each routine in the listing), then a JSR is used to
enter the desired routine. Upon normal completion, the
called routine is exited via a subroutine return instruction (RTS).
Note: The preceeding documentation was written by the Editor, based
on phone conversations with Roy and studying the listing. There is a
high probability that it is correct. However, since it was not written
nor reviewed by the authors of these routines, the preceeding
documentation may contain errors in concept or in detail.
-- JCW, Jr.
In the Exponent:
00 Represents -128
...
7F Represents -1
80 Represents 0
81 Represents +1
...
FF Represents +127
Exponent
SEEEEEEE
n
Two's Complement Mantissa
SM.MMMMMM MMMMMMMM MMMMMMMM
n+1
n+2
n+3
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
JULY 5, 1976
BASIC FLOATING POINT ROUTINES
FOR 6502 MICROPROCESSOR
BY R. RANKIN AND S. WOZNIAK
CONSISTING OF:
NATURAL LOG
COMMON LOG
EXPONENTIAL (E**X)
FLOAT
FIX
FADD
FSUB
FMUL
FDIV
FLOATING POINT REPRESENTATION (4-BYTES)
EXPONENT BYTE 1
MANTISSA BYTES 2-4
MANTISSA:
TWO'S COMPLIMENT REPRESENTATION WITH SIGN IN
MSB OF HIGH-ORDER BYTE. MANTISSA IS NORMALIZED WITH AN
ASSUMED DECIMAL POINT BETWEEN BITS 5 AND 6 OF THE HIGH-ORDER
BYTE. THUS THE MANTISSA IS IN THE RANGE 1. TO 2. EXCEPT
WHEN THE NUMBER IS LESS THAN 2**(-128).
EXPONENT:
THE EXPONENT REPRESENTS POWERS OF TWO. THE
REPRESENTATION IS 2'S COMPLIMENT EXCEPT THAT THE SIGN
BIT (BIT 7) IS COMPLIMENTED. THIS ALLOWS DIRECT COMPARISON
OF EXPONENTS FOR SIZE SINCE THEY ARE STORED IN INCREASING
NUMERICAL SEQUENCE RANGING FROM $00 (-128) TO $FF (+127)
($ MEANS NUMBER IS HEXADECIMAL).
REPRESENTATION OF DECIMAL NUMBERS:
THE PRESENT FLOATING
POINT REPRESENTATION ALLOWS DECIMAL NUMBERS IN THE APPROXIMATE
RANGE OF 10**(-38) THROUGH 10**(38) WITH 6 TO 7 SIGNIFICANT
DIGITS.
ORG
NOP
NOP
BSS
NOP
BSS
BSS
BSS
BSS
BSS
BSS
3
3
3
4
4
4
4
1
SET BASE PAGE ADRESSES
EXPONENT
MANTISSA
EXPONENT
MANTISSA
SCRATCH
2
2
1
1
0003
0003
0004
0005
0008
0009
000C
0010
0014
0018
001C
1D00
EA
EA
00 00 00
EA
00 00 00
00
SIGN
X2
M2
X1
M1
E
Z
T
SEXP
INT
*
*
*
*
*
LOG
ORG $1D00
STARTING LOCATION FOR LOG
NATURAL LOG OF MANT/EXP1 WITH RESULT IN MANT/EXP1
LDA M1
BEQ ERROR
1D00
1D02
A5 09
F0 02
1D04
1D06
1D07
1D0A
1D0C
1D0E
1D10
1D12
1D14
1D16
1D18
1D1B
1D1D
1D1F
1D21
1D23
1D25
1D28
1D2A
1D2B
1D2D
1D30
1D32
1D34
1D36
1D38
1D3A
1D3D
1D3F
1D40
1D42
1D45
1D47
1D49
1D4B
1D4C
1D4E
1D51
1D53
1D55
1D57
1D59
1D5A
1D5C
1D5F
1D62
1D64
1D67
1D69
1D6A
1D6C
1D6F
1D71
1D74
1D76
1D77
1D79
1D7C
1D7E
1D81
1D83
1D84
1D86
10 01
00
20
A5
A0
84
49
85
A9
85
20
A2
B5
95
B5
95
BD
95
CA
10
20
A2
B5
95
B5
95
BD
95
CA
10
20
A2
B5
95
CA
10
20
A2
B5
95
95
CA
10
20
20
A2
BD
95
CA
10
20
A2
BD
95
CA
10
20
A2
BD
95
CA
10
20
1C 1F
04
80
04
80
0A
00
09
2C 1F
03
04
10
08
18
D1 1D
08
F0
4A 1F
03
08
14
10
08
D1 1D
04
F0
50 1F
03
14
04
F9
9D 1F
03
08
14
04
F7
77 1F
1C 1F
03
E1 1D
08
F8
4A 1F
03
DD 1D
04
F8
9D 1F
03
D9 1D
04
F8
50 1F
ERROR
*
CONT
BPL CONT
BRK
JSR
LDA
LDY
STY
EOR
STA
LDA
STA
JSR
LDX
LDA
STA
LDA
STA
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
LDA
STA
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
STA
DEX
BPL
JSR
JSR
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
SWAP
X2
=$80
X2
=$80
M1+1
=0
M1
FLOAT
=3
X2,X
Z,X
X1,X
SEXP,X
R22,X
X1,X
SEXP1
FSUB
=3
X1,X
T,X
Z,X
X1,X
R22,X
X2,X
SAVET
FADD
=3
T,X
X2,X
TM2
FDIV
=3
X1,X
T,X
X2,X
MIT
FMUL
SWAP
=3
C,X
X1,X
MIC
FSUB
=3
MB,X
X2,X
M2MB
FDIV
=3
A1,X
X2,X
M2A1
FADD
IF ARG>0 OK
ERROR ARG<=0
MOVE ARG TO EXP/MANT2
HOLD EXPONENT
SET EXPONENT 2 TO 0 ($80)
COMPLIMENT SIGN BIT OF ORIGINAL EXPONENT
SET EXPONENT INTO MANTISSA 1 FOR FLOAT
CLEAR MSB OF MANTISSA 1
CONVERT TO FLOATING POINT
4 BYTE TRANSFERS
COPY MANTISSA TO Z
SAVE EXPONENT IN SEXP
LOAD EXP/MANT1 WITH SQRT(2)
SEXP1
SAVET
Z-SQRT(2)
4 BYTE TRANSFER
SAVE EXP/MANT1 AS T
LOAD EXP/MANT1 WITH Z
LOAD EXP/MANT2 WITH SQRT(2)
TM2
Z+SQRT(2)
4 BYTE TRANSFER
LOAD T INTO EXP/MANT2
T=(Z-SQRT(2))/(Z+SQRT(2))
4 BYTE TRANSFER
COPY EXP/MANT1 TO T AND
LOAD EXP/MANT2 WITH T
T*T
MOVE T*T TO EXP/MANT2
4 BYTE TRANSFER
LOAD EXP/MANT1 WITH C
T*T-C
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH MB
MB/(T*T-C)
LOAD EXP/MANT2 WITH A1
MB/(T*T-C)+A1
MIT
MIC
M2MB
M2A1
1D89
1D8B
1D8D
1D8F
1D90
1D92
1D95
1D97
1D9A
1D9C
1D9D
1D9F
1DA2
1DA4
1DA6
1DA8
1DA9
1DAB
1DAE
1DB0
1DB3
1DB5
1DB6
1DB8
1DBB
A2
B5
95
CA
10
20
A2
BD
95
CA
10
20
A2
B5
95
CA
10
20
A2
BD
95
CA
10
20
60
03
14
04
F9
77 1F
03
E5 1D
04
F8
50 1F
03
18
04
F9
50 1F
03
D5 1D
04
F8
77 1F
M2T
M2MHL
LDEXP
MLE2
1DBC
1DBF
1DC1
1DC4
1DC6
1DC7
1DC9
1DCC
1DCD
1DD1
1DD5
1DD9
1DDD
1DE1
1DE5
1E00
20
A2
BD
95
CA
10
20
60
7E
2D
80
02
7F
B9
80
80
81
86
80
08
7F
00
00 1D
03
CD 1D
04
F8
77 1F
6F
ED
5A
7A
58
0C
52
40
AB
49
6A
66
40
00
*
*
COMMON LOG OF MANT/EXP1 RESULT IN MANT/EXP1
*
LOG10 JSR LOG
COMPUTE NATURAL LOG
LDX =3
L10
LDA LN10,X
STA X2,X
LOAD EXP/MANT2 WITH 1/LN(10)
DEX
BPL L10
JSR FMUL
LOG10(X)=LN(X)/LN(10)
RTS
*
LN10
DCM 0.4342945
R22
LE2
A1
MB
C
MHLF
*
*
*
*
EXP
ORG $1E00
STARTING LOCATION FOR EXP
EXP OF MANT/EXP1 RESULT IN MANT/EXP1
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
=3
L2E,X
X2,X
EXP+2
FMUL
=3
X1,X
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH LOG BASE 2 OF E
LOG2(3)*X
4 BYTE TRANSFER
DCM 1.4142136
DCM 0.69314718
DCM 1.2920074
DCM -2.6398577
DCM 1.6567626
DCM 0.5
SQRT(2)
LOG BASE E OF 2
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
LDX
LDA
STA
DEX
BPL
JSR
RTS
=3
T,X
X2,X
M2T
FMUL
=3
MHLF,X
X2,X
M2MHL
FADD
=3
SEXP,X
X2,X
LDEXP
FADD
=3
LE2,X
X2,X
MLE2
FMUL
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH T
(MB/(T*T-C)+A1)*T
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH MHLF (.5)
+.5
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH ORIGINAL EXPONENT
+EXPN
4 BYTE TRANSFER
LOAD EXP/MANT2 WITH LN(2)
*LN(2)
RETURN RESULT IN MANT/EXP1
1E00
1E02
1E05
1E07
1E08
1E0A
1E0D
1E0F
A2
BD
95
CA
10
20
A2
B5
03
D8 1E
04
F8
77 1F
03
08
FSA
Plik z chomika:
Amiga7878
Inne pliki z tego folderu:
Apple Assembly Language_Maurer_1984.pdf
(33419 KB)
6502 Assembly Language Subroutines.pdf
(29632 KB)
6502 Assembly Lanuage Rountines part 1.pdf
(15099 KB)
6502 Assembly Language Programming.pdf
(7507 KB)
6502 Assembler in BASIC.pdf
(2207 KB)
Inne foldery tego chomika:
65816_gs
basic
c
cpm
forth
Zgłoś jeśli
naruszono regulamin