In the August 1976 issue of

*Dr. Dobb's Journal*Steve Wozniak and Roy Rankin published a listing of floating point math routines for the 6502. The code is portable and provides routines for floating point add, subtract, multiply and divide as well as natural and common log and exponential functions and conversion between fixed and floating point. A version of this code was later included in the firmware for the Apple II.

The article in PDF and text format is available from 6502.org so I thought it would be fun to get it running on the Replica 1. I took the code and made a few small changes to get it to assemble under the CC65 assembler. I also included some later fixes that were published in Dr. Dobb's.

I then wrote an interactive program that allows the different functions to be executed. I did some minimal testing and all the routines seemed to be working quite well.

Here is a sample run of the program (input by the user is in bold):

FLOATING POINT DEMONSTRATION PROGRAM

F - FIXED TO FLOATING POINT

P - FLOATING TO FIXED POINT

L - NATURAL LOG

N - COMMON LOG

E - EXPONENTIAL

A - FLOATING POINT ADD

S - FLOATING POINT SUBTRACT

M - FLOATING POINT MULTIPLY

D - FLOATING POINT DIVIDE

? - THIS HELP SCREEN

X - EXIT

SELECT A FUNCTION:

**F**

FIXED TO FLOATING POINT

ENTER 16-BIT HEX NUMBER:

**0123**

FLOATING POINT IS: 88 48C000

SELECT A FUNCTION:

**P**

FLOATING TO FIXED POINT

ENTER EXPONENT AND MANTISSA:

**88 48C000**

FIXED POINT IS: 0123

SELECT A FUNCTION:

**F**

FIXED TO FLOATING POINT

ENTER 16-BIT HEX NUMBER:

**0456**

FLOATING POINT IS: 8A 456000

SELECT A FUNCTION:

**M**

FLOATING POINT MULTIPLY

ENTER EXPONENT AND MANTISSA:

**88 48C000**

ENTER EXPONENT AND MANTISSA:

**8A 456000**

RESULT IS: 92 4EDC20

SELECT A FUNCTION:

**P**

FLOATING TO FIXED POINT

ENTER EXPONENT AND MANTISSA:

**92 4EDC20**

ERROR OCCURRED AT ADDRESS $1FE6

SELECT A FUNCTION:

**A**

FLOATING POINT ADD

ENTER EXPONENT AND MANTISSA:

**88 48C000**

ENTER EXPONENT AND MANTISSA:

**8A 456000**

RESULT IS: 8A 579000

SELECT A FUNCTION:

**P**

FLOATING TO FIXED POINT

ENTER EXPONENT AND MANTISSA:

**8A 579000**

FIXED POINT IS: 0579

SELECT A FUNCTION:

**X**

This code could be used to write assembly language programs that need to do floating point math. One piece is missing though -- conversion between the floating point representation and one that can be input by a user or printed. This is usually done using Binary Coded Decimal (BCD).

A little research found a couple of articles by Marvin L De Jong in the February and April 1981 issues of COMPUTE! magazine that described BCD to floating point and floating point to BCD conversion routines. While the floating point format he used was slightly different from what was used in Woz's code, I'm hoping that I can adapt it. So that is my next little project.

All of the code can be found here on github.

## 2 comments:

Are you sure this is BCD? It looks like it's just the usual decimal (string) representation. BCD is normally used to mean the encoding of two digits per byte.

I'm just now getting to the floating point / BCD code. He refers to it as BCD but it does look like it is string representation.

Post a Comment