Commodore PET User Manual page 234

Table of Contents

Advertisement

(Notice that the contents of A$ and B$ are not the same as those input at line 20.
The same variable names are used ta allow program compatibility between ail
four math programs.l Only two parameters are passed at a time because the addi-
tion subroutine adds only two numbers at a time. After P1 $ and P2$ are added.
the addition subroutine will be called again ta add the contents of P3$ and P4$.
Within the subroutine, the variable C will be assigned the sum of A$
+
B$
and converted into C$.
Once the values for P1$ and P2$ are passed ta A$ and B$ the addition
subroutine is called:
1129 GOSUB 2999
A$ and B$ are right-justified and equated in length for addition by adding blanks
from BLANK$ ta the shorter string (if there is one) in lines 2010-2040:
2910 BLANK$="
21329 X=LEN(A$):Y=LEN(B$)
213313 IF X<Y THEN A$=LEFT$(BLANK$,Y-X)+A$
21349 IF X>Y THEN B$=LEFT$(BLANK$,X-Y)+B$
Statements 2050 ta 2140 add the corresponding digits of A$ and B$ and
convert the sum Cinto the numeric string C$. (A full explanation of this process is
offered in the "Addition via Numeric Strings" section. page 191.)
21350 D=0:N=1:C$=""
21360 FOR I=:LEN(A$) TO 1 STEP-1
2070 A=VAL(MID$(A$,I, 1»
2080 A=A+D:D=0
2090 B=VAL(MID$(B$,I,1»
2100 C=A+B
2110 IF C>=10 THEN D=1
2120 IF D=l AND 1=1 THEN N=2
2130 C$=RIOHT$(STR$(C),N)+C$
2140 NEXT 1
The sumo C$, is passed through a FOR ... NEXT loop ta truncate any leading
blanks or zeros at lines 3000 ta 3060. This truncation routine is from "Subtraction
via Numeric Strings" (page 205).
301313 REM***TRUNCATE LEAD ZEROS***
31301 L=e
30113 FOR 1=1 TO LEN(C$)
3020 IF VAL(MID$(C$, 1,1»=0 THEN L=L+1
3030 IF VAL(LEFT$(C$,I»<>e THEN I=LEN(C$)
3040 NEXT 1
3050 C$=RIOHT$(C$,LEN(C$)-L)
3060 RETURN
C$. the sum of P1 $ and P2$, is returned ta the main program and converted
ta M1$:
1130 M1$=C$
The contents of C$ must be transferred ta M 1$ because C$ must be cleared
before the addition subroutine is called again at line 1150 ta add P3$ and P4$.
221

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Cbm

Table of Contents