Converting Applesoft Basic to Atari Basic

Neater Numerical Tables

Paul N. Havey


In Neaten Up Those Messy Numerical Tables by Donald J. Taylor (Creative Computing, December, 1981), the author designed a short program, written in Applesoft, which enables the user to print numbers in a neat exponential form. While the program may be an answer to the dreams of many an Apple owner, it is of little use to still-dreaming Atari owners.

Conversion from Applesoft to Commodore or TRS-80 Basic is more direct than conversion to Atari Basic. This is because Atari Basic does not support string arrays like the others. However, with the unlimited string length capability of Atari Basic, program conversions are possible for Atari owners and, in some cases, simplified.

Included here is a translation from Applesoft Basic to Atari Basic of Taylor's exponential program. Also included is a listing of the original program for comparison. Both programs convert a number to a string which is then disassembled. The number is then reassembled by concatenation into a thousand string mantissa and exponential for the scientific notation format. Zeros and signs are inserted where needed. See Table 1 for a sample of printed output.

The variable names and time requirements for the Atari Basic version are only slightly different from the original Applesoft Basic version. Table 2 contains a list of variable names for the Atari Basic version and Table 3 contains the time and memory requirements for both versions.

The handling of string variables is one of the fundamental differences between Applesoft and Atari Basics. In Microsoft Basic (and in Applesoft) extraction or splitting a string into pieces is done by the functions MID$, RIGHT$, and LEFT$. In Atari Basic, strings are split by using a subscript or set of subscripts. For example, A$(6,12) means that the substring starts at the sixth character and ends with the twelfth. If one number is within the subscript then the subscript begins with that character and ends with the last character in the string.

Table 4 contains a list of Atari to Microsoft translations. The LEN(A$) function is the same in both types of Basic.

Atari Basic uses the LEN(A$) functions to concatenate two substrings. In Microsoft Basic, concatenation is done with a plus sign. Table 4 shows an example of each.

Microsoft Basic uses subscripts as an indication of a string array. Atari Basic, while not supporting string arrays, can simulate string arrays by using subscripts. In Atari Basic, simulated arrays that have all the same length are the most useful.


Table 1. Sample Data Columns.

Column A Column B

-4E-04 -4.00E-04
-1.10679718E-03 -1.11E-03
-0.02 -2.00E-02
-0.158113883 0
0 1.58E+00
1.58113883 1.00E+01
10 4.74E+01
47.43416949 2.00E+02
790.569415 7.91E+02
40000 4.00E+04
1739252.71 1.74E+06
2.68793601E+09 2.69E+09
9E+09 9.00E+09
A = As the Apple and Atari print.
B = As converted by this program
(rounded off to three significant digits,).


Table 2. Atari version of the variable list.

Name Description
   
S$ Sign of original number (+ or -)
E$ Exponent part of number (E)
BUF$ Mantisa part of number
BUFI$ Integer mantisa part of number
BUF Original unformatted, unsigned number
L Length of mantisa part of number
X$ Final version of number for printing
X Original unformatted, signed number
   
The original number "X" is disassembled into the strings "$$", "E$", and "BUF$". After processing, the number is reassembled as the string "X$".


Table 3. Atari Time and Memory Requirements.

Duration with rounding in milliseconds:
Duration without rounding in milliseconds:
Memory used in bytes:
170
125
1400


Table 4. String Operation Comparisons.

Item # MICROSOFT ATARI

1
MID$(A$,X,Y) A$(X,Y)
2
LEFT$(A$,X) A$(1,X)
3
RIGHT$(A$,X) A$(LEN(A$)-X)
4
A$=A$+B$ A$(LEN(A$)+1)=B$
5
C$=A$+B$ C$=A$:C$(LEN(C$)+1)=B$
6
A$(1)="AAA" A$(1,3)="AAA"
7
A$(2)="BBB" A$(4,6)="BBB"
8
A$(3)="CCC" A$(7,9)="CCC"
 
Table 4 contains the most common string operations for both Basics. Items 1, 2, and 3 perform substring extraction. Items 4 and 5 concatenate two strings. Items 6 to 8 give examples of string array notation.

Program 1.

10 REM SCIENTIFIC NOTATION FORMAT
20 REM CONSTANT SIGNIFICANT DIGITS
30 REM TRUNCATOR/ROUNDER
40 REM REVISED FOR ATARI BASIC
45 REM BY PAUL HAVEY    01 DEC 82
50 REM INPUT: TWO VARIABLES
60 REM 1. X=NUMBER TO FORMAT
70 REM 2. D=SIGNIFICANT DIGITS
75 REM D-1 FOR ROUNDING
80 REM OUTPUT: ONE VARIABLE
90 REM 1. X$=FORMATTED NUMBER
100 DIM X$(20),BUF$(20),E$(3),BUFI$(10),S$(1)
110 S$=" ":IF SGN(X)=-1 THEN S$=" "
120 BUF=ABS(X):BUF$=STR$(BUF):L=LEN(BUF$)
130 IF L>4 THEN IF BUF$(L-3,L-3)="E" THEN E$=BUF$(L-2):BUF$(L-3)="0000":GOTO 230
140 IF BUF=0 THEN X$=" 0":GOTO 300
150 IF BUF<1 THEN 210
160 BUFI$=STR$(INT(BUF))
170 E$="+0":E$(3,3)=STR$(LEN(BUFI$)-1)
180 IF BUF<10 THEN 230
190 BUF$(2,2)=".":BUF$(3,LEN(BUFI$)+1)=BUFI$(2)
200 GOTO 230
210 IF BUF$(3,3)="0" THEN E$="-02":BUF=BUF*100:BUF$=STR$(BUF):GOTO 230
220 E$="-01":BUF=BUF*10:BUF$=STR$(BUF)
230 BUF$(LEN(BUF$)+1)="00000000"
240 BUF$=BUF$(1,D+1):BUF$(2,2)="."
290 X$=S$:X$(2)=BUF$:X$(LEN(X$)+1)="E":X$(LEN(X$)+1)=E$
300 RETURN

Program 2.

250 REM ROUNDING ROUTINE
260 BUF$(2)=BUF$(3):BUF=(VAL(BUF$)+5):BUF$=STR$(BUF):IF LEN(BUF$)=D THEN 280
270 L=ABS(VAL(E$)+1):E$(2)=STR$(L):IF L<10 THEN E$(3)=E$(2):E$(2,2)="0"
280 X$=BUF$(2):BUF$(2,2)=".":BUF$(3)=X$(1,D-2)

Program 3.

100 REM SCIENTIFIC NOTATION FORMATTER
102 REM CONSTANT SIGNIFICANT FIGURES
104 REM TRUNCATOR/ROUNDER
106 REM DONALD J. TAYLOR
108 REM JUNE 1, 1982
110 REM INPUTS REQUIRED=X AND D
112 REM X=INPUT #
114 REM D=# DIGITS FOR TRUNCATION
116 REM D-1=# DIGITS FOR ROUNDING
118 REM X$=OUTPUT
120 REM
122 S$=" ":IF SGN(X)=-1 THEN S$="-"
124 X=ABS(X):X$=STR$(X):P=LEN(X$)
126 IF P>4 THEN IF MID$(X$,P-3,1)="E" THEN P$=MID$(X$,P-2):X$=LEFT$(X$,P-4):X$=LEFT(X$,1)+"."+MID$(X$,3):GOTO 140
128 IF X=0 THEN X$=" 0":P$=" ":GOTO 164
130 IF X<1 THEN GOTO 136
132 XI$=STR$(INT(X))
134 X$=LEFT$(XI$,1)+"."+MID$(XI$,2)+MID$(X$,LEN(XI$)+2):P$="+0"+STR$(LEN(XI$)-1):GOTO 140
136 IF MID$(X$,2,1)="0" THEN P$="-02":X$=MID$(X$,3,1)+"."+MID$(X$,4):GOTO 140
138 P$="-01":X$=MID$(X$,2,1)+"."+MID$(X$,3)
140 X$=X$+"00000000"
142 X$=LEFT$(X$,D+1)
162 X$=S$+X$+"E"+P$
164 RETURN

Program 4.

144 REM ROUNDING ROUTINE : LINES 144-160
146 X$=LEFT$(X$,1)+MID$(X$,3)
148 X=VAL(X$)+5:X$=STR$(X)
150 IF LEN(X$)=D THEN GOTO 160
152 P=VAL(RIGHT$(P$,3))+1
154 IF SGN(P)=1 THEN GOTO 158
156 P$="-"+RIGHT$(("0"+STR$(ABS(P))),2):GOTO 160
158 P$="+"+RIGHT$(("0"+STR$(ABS(P))),2)
160 X$=LEFT$(X$,1)+"."+MID$(X$,2,D-2)

Paul T. Havey, P.O. Box 5148, Santa Monica, CA 90405.

Table of Contents
Previous Section: The Challenge is Met (Super Text Mode)
Next Section: Interfacing Your Atari