Tri-Base Arithmetic

This program turns your TS1000 into a multi-base arithmetic calculator and number converter. It was written for the TS1000 so it could be used while you write another program on the TS2068. Used in this way, “ARITH” proves to be very useful as a programmers tool. You can also use the program by itself as an instructive aid for understanding what happens in binary or hex arithmetic and logic operations.

“ARITH” lets you input either binary (base 2), decimal (base 10), or hexadecimal (base 16) numbers. The results of the calculation will be displayed in all three number systems. However, inside the program, all values are converted to their decimal equivalent. Almost all calculations are done in decimal, and the results re-converted to binary and hex for the display.

To use “ARITH”, enter Listing 1 and RUN it. The screen will clear and the computer will wait for you to input a decimal, hex or binary number. Every entry must be preceded with a “D”, “H”, or a “B” to indicate whether the number is decimal, hex, or binary respectively. Decimal values must be integers and must range between -65536 and +65535. Hex and Binary inputs must be positive numbers, however, they may be converted to negative after they are entered by using the “+/-” function. (ex: To display the negative of H0010, first enter H0010. Then enter “+/-“.)

Values are displayed in a single row across the screen; decimal on the left, hexadecimal next in four hexadecimal digits (hexadigits?), then as 16 binary bits on the right. The display shows negative decimal numbers with a minus sign. Negative hex numbers are displayed in 16’s compliment form. A hexadecimal “F” is placed before the four hex digits (Actually, there are an infinite number of “F’s” off to the left of such a number. All but the first is discarded. Negative binary numbers are displayed in 2’s compliment form. The 16 bits of a negative binary number are preceded by a “C” as an indicator of “Complimented” (In a manner analogous to the “F” explained for hex numbers, this “C” stands for an infinite number of “1’s” to the left of the specified number of bits. “C” is used instead of an extra “1” because it is clearer).

Arithmetic Operators

The customary “+”, “-“, “*”, “/”, and “=” functions are available in the program. These symbols are entered alone. After entry, they appear on the far left of the screen between the two rows which display the values to which they apply. The “+/-” (change sign) operator is also available. This serves to give the 2’s compliment of a number. Here are a few examples of how to solve decimal/hex/binary problems.

To add binary 00100111 to decimal 8192, press:

B00100111 <ENTER>
+ <ENTER>
D8192 <ENTER>
= <ENTER>

Pressing ENTER after the “=” produces the result in decimal, hex, and binary.

To subtract decimal 400 from hex FDD8, press

HFDD8 <ENTER>
- <ENTER>
D400 <ENTER>
= <ENTER>

The correct answer in decimal, hex, and binary will be given.

Logical Operators

Besides the arithmetic operators just shown, you also have the logical operators, AND, OR and XOR. Additionally, “C” gives the 1’s compliment of a number. These operators are entered in the same way as arithmetic operators:

To logically AND binary 10101010 with decimal 465, press:

D465 <ENTER>
AND <ENTER>
B10101010 <ENTER>
= <ENTER>

The result of the operation will be displayed. Observing the binary outputs of logical functions shows very graphically exactly what happens when AND, OR or XOR is applied to two numbers.

Chaining Operations

Either arithmetic or logic operations may be chained (A+B+C-D). However, there is no hierarchy among operators and no facilities are provided for parentheses. There is a single memory available to help with slightly complex calculations. This memory is accessed by entering the commands “STO” which stores the last number displayed and “RCL” which puts the contents of the memory onto the display.

Shift & Rotate

Binary numbers may be shifted left or right by entering “SR” and “SL”. All 16 bits of the number will shift and the corresponding decimal and hex numbers will change accordingly.

Binary numbers can also have their bits rotated. Do this by entering the command “RRn” or “RLn” where “n” specifies the number of bits to rotate. It is necessary to include the number of bits to provide flexibility in rotating eight-bit numbers in the same way that the Z80 microprocessor does. If, for example, n=8, the left most eight bits will be set to zero and the rotation will occur using only the right most bits of the original number. This simulates the RLC x and RRC x (Rotate Left Circular and Rotate Right Circular) instructions of the Z80 except that no carry flag is set or reset. If n=9, the RL x and RR x instructions can be simulated with bit 8 (the ninth bit from the right) being used as the Carry flag.

Screen Control

As numbers and calculations are displayed, the screen slowly fills. Eventually, after the 22nd line is printed, an error 5 (Screen Full) will appear. Entering CONTinue at this point will clear the screen and start the display at the top again. Two other commands, SCL and “B” will scroll the screen upwards. This will preserve previous numbers of the display which is nice if you want to compare before and after effects of the shift-rotate, or logic operators. The screen can be cleared any time by entering CLS or “V”.

Hard Copy

If the 2040 printer is connected and switched on, all information printed to the screen will also be printed on the 2040. This can be stopped by switching off the printer. The entire screen can be copied by entering either COPY or “Z” (the COPY key).

Errors

It is possible to crash this program because there are only a few error traps. However, a crash is not normally a serious problem. The program can just be restarted with “RUN”. The following error traps DO exist:

Incorrect entries–The program looks for one of the acceptable entries, and if it is not found, the input is ignored and the cursor is again displayed at the bottom of the screen.

Display overflow–If a result exceeds the maximum which can be displayed (-65537 to +65536) the word “OVERFLOW” will appear instead of the actual number. The number itself will not be lost, and further calculations can be performed on it.

Bits to rotate–The number “n” in the rotate operations is constrained to lie between 1 and 16 inclusive. If it falls outside this range, the entry will be rejected.

Conversion to 2068

For those who wish to use this arithmetic program on the 2068, you can load the listing into the computer either by using the UPLOAD 2000 program or by typing it in by hand.

Delete lines 50, 70, 80, 260, 270, 9910, and 9920 because the TS2068 has no FAST, SLOW, or SCROLL commands, and uses a different system for auto-run.

Finally, change these lines to read as indicated:

  10 BORDER 0: PAPER 0: INK 6: CLS
20 POKE 23658,8: DIM H$(5): DIM B$(17)
430 LET D=B*D+CODE I$(I)-48-(7 AND CODE I$(I)>57)
1000 LET I$="1's Compl"
1310 IF D<2^(B-1) THEN LET C=0
1330 IF D>2^B THEN LET D=D-2^B
1360 LET D=INT (D/2)+C*2^(B-1)
2810 LET H$(I)=CHR$ (H-16*INT (H/16)+48+(7 AND (H-16*INT (H/16)>9)))
2910 LET B$(I)=CHR$ (B-2*INT (B/2)+48)
9900 CLEAR: SAVE "arith" LINE 1

Listing 1

"ARITH"- TRI-BASE CONVERSIONS
AND ARITHMETIC FOR T/S-1000.

10 DIM H$(5)
20 DIM B$(17)
30 LET F$=""
40 LET L=1
50 SLOW
60 INPUT I$
70 FAST
80 IF I$="B" OR I$="SCL" THEN GOTO 260
90 IF I$(1)="D" THEN GOTO 300
100 IF I$(1)="H" OR I$(1)="B" THEN GOTO 400
110 IF I$="V" OR I$="CLS" THEN CLS
120 IF I$="Z" OR I$="COPY" THEN COPY
130 LET L=0
140 IF I$="+" OR I$="-" OR I$="*" OR I$="/" OR I$="AND" OR I$="OR" OR I$="XOR" THEN GOTO 500
150 LET L=2
160 IF I$="+/-" THEN GOTO 900
170 IF I$="STO" THEN GOTO 1500
180 IF I$="RCL" THEN GOTO 1600
190 LET L=3
200 IF I$="=" THEN GOTO 600
210 IF I$="C" THEN GOTO 1000
220 IF I$="SL" OR I$="SR" THEN GOTO 1100
230 IF LEN I$<3 THEN GOTO 50
240 IF I$(1 TO 2)="RL" OR I$(1 TO 2)="RR" THEN GOTO 1200
250 GOTO 50
260 SCROLL
270 GOTO 50
300 LET D=VAL I$(2 TO )
310 GOTO 2000
400 LET B=2+(14 AND I$(1)="H")
410 LET D=0
420 FOR I=2 TO LEN I$
430 LET D=B*D+CODE I$(I)-28
440 NEXT I
450 GOTO 2000
500 LPRINT I$
510 PRINT I$
520 GOSUB 700
530 LET F$=I$
540 LET L$=B$
550 GOTO 50
600 GOSUB 700
610 LET D=X
620 GOTO 1900
700 IF F$="" THEN LET X=D
710 IF F$="+" THEN LET X=X+D
720 IF F$="-" THEN LET X=X-D
730 IF F$="*" THEN LET X=X*D
740 IF F$="/" THEN LET X=INT (X/D)
750 IF F$<>"AND" AND F$<>"OR" AND F$<>"XOR" THEN RETURN
800 LET D=0
810 FOR I=2 TO 17
820 IF F$="AND" THEN LET D=2*D+(VAL B$(I) AND VAL L$(I))
830 IF F$="OR" THEN LET D=2*D+(VAL B$(I) OR VAL L$(I))
840 IF F$="XOR" THEN LET D=2*D+((VAL B$(I) OR VAL L$(I)) AND NOT (VAL B$(I) AND VAL L$(I)))
850 NEXT I
860 LET X=D
870 RETURN
900 LET D=-D
910 GOTO 1900
1000 LET I$="1-COMPL"
1010 LET D=-D-1
1020 GOTO 1900
1100 LET D=(2*D AND I$(2)="L")+(INT (D/2) AND I$(2)="R")
1110 GOTO 1900
1200 LET B=VAL I$(3 TO )
1210 IF B>16 OR B<1 THEN GOTO 50
1220 LET C=1
1230 FOR I=2 TO 17-B
1240 LET B$(I)="0"
1250 NEXT I
1260 LET D=0
1270 FOR I=2 TO 17
1280 LET D=2*D+VAL B$(I)
1290 NEXT I
1300 IF I$(2)="R" THEN GOTO 1350
1310 IF D<2**(B-1) THEN LET C=0
1320 LET D=2*D+C
1330 IF D>2**B THEN LET D=D-2**B
1340 GOTO 1900
1350 IF D/2=INT (D/2) THEN LET C=0
1360 LET D=INT (D/2)+C*2**(B-1)
1370 GOTO 1900
1500 LPRINT "STO"
1510 PRINT "STO"
1520 LET S=D
1530 GOTO 40
1600 LET D=S
1610 GOTO 1900
1900 LPRINT I$
1910 PRINT I$
2000 IF D<65536 AND D>-65537 THEN GOTO 2060
2010 LPRINT "OVERFLOW"
2020 LPRINT
2030 PRINT "OVERFLOW"
2040 PRINT
2050 GOTO 30
2060 IF D<0 THEN GOTO 2200
2100 LET H=D
2110 LET H$(1)=" "
2120 GOSUB 2800
2130 LET B=D
2140 LET B$(1)=" "
2150 GOSUB 2900
2160 GOTO 3000
2200 LET H=65536+D
2210 LET H$(1)="F"
2220 GOSUB 2800
2230 LET B=65536+D
2240 LET B$(1)="C"
2250 GOSUB 2900
2260 GOTO 3000
2800 FOR I=5 TO 2 STEP -1
2810 LET H$(I)=CHR$ (28+H-16*INT (H/16))
2820 LET H=INT (H/16)
2830 NEXT I
2840 RETURN
2900 FOR I=17 TO 2 STEP -1
2910 LET B$(I)=CHR$ (28+B-2*INT (B/2))
2920 LET B=INT (B/2)
2930 NEXT I
2940 RETURN
3000 IF L=1 THEN LPRINT
3010 IF L=1 THEN PRINT
3020 LET T=6-LEN STR$ D
3030 LPRINT TAB T;D;TAB 8;H$;TAB 15;B$
3040 PRINT TAB T;D;TAB 8;H$;TAB 15;B$
3050 IF L=0 THEN GOTO 50
3060 IF L=2 THEN GOTO 40
3070 GOTO 30
9900 CLEAR
9910 SAVE "ARITH"
9920 RUN

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.