` COMPUTE! ISSUE 74 / JULY 1986 / PAGE 10`

### The Editors and Readers of COMPUTE!

If you have any questions, comments, or suggestions you would like to see addressed in this column, write to "Readers' Feedback," COMPUTE!, P.O. Box 5406, Greensboro, NC 27403. Due to the volume of mail we receive, we regret that we cannot provide personal answers to technical questions.

## Machine Language Division

I own a Commodore 64 and am teaching myself machine language. There is only one problem preventing me from completing my first useful program-I can't write a program to divide by ten. The objective is to take a number in the range 0-255 from memory and break it into each of its decimal parts. For example, 255 would break down into the digits 2, 5, and 5.

Kevin Owens

The 6502/6510 instruction set does not include a division instruction of any sort. Although it's possible to construct an ML routine for division, it is much easier and faster to use a method called successive subtraction. Here is the basic idea: Each power of ten from highest to lowest (in this case from 100 to 10 to 1) is subtracted from the number until you determine that the number has become negative. Each digit of the number is derived by counting the number of subtractions.

Below is the source code for a short program that displays any three-digit number at the upper-left corner of the screen. You'll need a machine language assembler to create the object code. (This program is written in PAL assembler format for the Commodore 64. Slight modifications are needed to assemble the code with a different assembler or to make the program work on a different 6502 computer.) Once the program is assembled, enter SYS 828: The program displays all the numbers from 0 to 255 in succession. The delay loop in line 130 gives you time to read each number. To see how fast numbers can be converted and displayed, remove this line and reassemble the program.

```10 SYS 700: OPT OO:*=828
20 TEMP = 2
30 LDA #0:STA 53281:LDA #147:JSR \$FFD2
40 LDA #1:STA 53281
50 LDA #0:STA TEMP
60 START INC TEMP:LDA TEMP
70 LDX #0
80 SUBAGAIN LDY #255
90 SUBMORE INY:SEC:SBC DIGITS, X
100 BCS SUBMORE