The Computer Tutor: Reverse Scroll with Machine Language Subroutine

Author(s): Gene G. Buza
Publication: Synchro-Sette
Pub Details: Synchro-Sette v2 n5 (Volume 2, Issue 5)
Date: May 1983
Pages: 5-7
The Computer Tutor heading art with REVERSE SCROLL WITH MACHINE LANGUAGE SUBROUTINE panel, graduate tutor pointing at a computer
The Computer Tutor heading art with REVERSE SCROLL WITH MACHINE LANGUAGE SUBROUTINE panel, graduate tutor pointing at a computer

Good morning Class. Sorry I wasn’t able to make it last session. We have had some people who notified us to tell me that the line-renumbering routines from last class didn’t work properly. I have noticed that in large programs, only about the first 60 lines were renumbered. One person told me that the routine itself became renumbered and blew the program.

Whenever data is POKEd into memory, a condition can exist that may create the desired effect with one machine such as the TS-1000 but will not work properly on another such as the ZX-81. Without intensive checking, I believe there is a subtle difference in ROMs between these two machines and even possibly between the earlier and later ROMs used in the ZX-81s.

To demonstrate this effect, the following is a program that creates a reverse scroll on the screen with a graphic flag:

  10 GOSUB 9000
20 DIM A$(23,32)
30 LET C$="********███████████
"
40 LET D$="******** █
"
50 LET E$="███████████████████
"
60 LET F$="█ █
"
70 FOR N=1 TO 5 STEP 2
80 LET A$(N,8 TO 26)=C$
90 LET A$(N+1,8 TO 26)=D$
100 NEXT N
110 FOR N=7 TO 11 STEP 2
120 LET A$(N,8 TO 26)=E$
130 LET A$(N+1,8 TO 26)=F$
140 NEXT N
150 LET A$(13,8 TO 26)=E$
160 FOR N=1 TO 22
170 PRINT A$(N)
180 NEXT N
190 LET A=1
200 LET B=A+1
210 IF B=23 THEN LET B=1
220 LET C=A-1
230 IF C=0 THEN LET C=22
240 PRINT AT USR X,0;A$(C)
250 LET A=C
260 GOTO 200
8999 STOP
9000 REM REVERSE SCROLL ROUTINE
9010 FAST
9020 LET X=30000
9030 LET Z$="01D6022A0C4009545D0
1B5022A0C4009EDB8C9"
9040 FOR N=1 TO LEN Z$-1 STEP 2
9050 POKE X,16*CODE Z$(N)+CODE Z
$(N+1)-476
9060 LET X=X+1
9070 NEXT N
9080 LET X=30000
9090 SLOW
9100 RETURN

Instead of the flag scrolling from the bottom of the screen up, the reverse occurs. The speed of the scroll is much faster, too.

The problem is that although this program works fine on a TS-1000, the scroll won’t work on any of the ZX-81s we have around here.

Unlike other machine language routines that depend on a REM statement in the first line of the program, this routine is held in a sub-routine starting with line 9000 and is initialized before it is needed by the GOSUB instruction in the first line.

The two methods of machine language entry that most text material proposes are:

1. creating a loop to input each hex code into memory separately.

2. putting the routine after a REM statement in the first line of the program.

The first method may involve saving the routine separate on tape, typing in NEW, POKEing in numbers to change RAMTOP, etc.

The possibilities of making mistakes can cause some time-consuming efforts where programs crash or just won’t work. If the routine follows a REM statement in the first line, it is accessed by calling a user routine (USR) from memory location 16514 which is the beginning memory location of the first character following the REM statement.

I personally don’t like either method although sometimes there is no choice. But once in a while, a different, easier method can be used. The one I propose can be put just about anywhere in the program.

The routine is POKEd into the memory locations of a 16K TS-1000, starting with decimal 30000 in RAM, although any available RAM memory location can be used as long as the main program isn’t residing in it.

From that point on, whenever it is needed, it is called upon with the PRINT AT USR call in line 240.

The string variables in lines 30 to 60 contain the various graphic components of the flag (consult the GRAPHICS entry methods in the owner’s manual if you’re not certain).

These strings are then entered into a 23 line matrix by the routines from line 70 to 150 and also line 20. The flag is first displayed with lines 160 to 180. This is as far as the ZX-81 will perform.

The lines 190 to 260 perform the loop to scroll the flag with line 240 doing the actual scrolling.

You can experiment with this program by eliminating the lines from 30 to 150 and creating your own string variables from A$(1) to A$(23). You can use the LET command to accomplish this or set up a FOR/NEXT loop such as the following:

30 FOR N=1 TO 22
40 INPUT A$(N)
50 SCROLL
60 PRINT A$(N)
70 NEXT N
80 CLS

The routine in assembly language that creates the scroll is as follows:

01D602      LD BC, 726
2A0C40      LD HL,(16396)
09          ADD HL,BC
54          LD D,H
5D          LD E,L
01B502      LD BC,693
2A0C40      LD HL,BC
09          ADD HL,BC
EDB8        LDDR
C9          RET

The characters in the left column represent hexidecimal codes. You will notice that they are in multiples of 2. They are used by the computer in pairs. The characters in the right column represent what these pairs of characters do. Numbers are loaded into memory locations, added or subtracted from numbers in other memory locations, etc. The final instruction, <C> is for the routine to RETurn to BASIC. Assembly language is a method of entering machine code that has similarities to the BASIC language. The USR in line 240 acts like a GOSUB command, but it jumps out of the BASIC program. The RET in the machine language routine acts like the RETURN in the BASIC language but returns the routine from machine language back to BASIC. If C9 does not end the routine, the program usually will not allow the user to perform such actions as BREAKing it.

Most people who attempt to understand machine or assembly code (myself included) become easily confused and frustrated when their time-consuming efforts are rewarded with some minor effect or a program crash.

Does anyone see a similarity between the assembly listing and the FLAG program listing? Yes, the gentleman in the rear has his hand up?

He says that line 240 in the FLAG program has the same code as the left column of the assembly code listing. Wow, right the first time!

That is correct! The characters in the left column are the only thing that the routine is concerned with. The right column is only an explanation of what they do. The gentleman in the rear has his hand up again but I know what his question is. He wants me to explain what they do.

No!

At this point, all I want to bring out is how to take these routines and utilize them in a BASIC program and the 9000 routine does just that. You will notice that line 9030 has the characters in the same order as they appear in the left column of the assembly listing. The routine from lines 9040 to 9070 takes these characters, two at a time, and POKE their decimal equivalents into adjacent memory locations starting with 30000. Line 9020 sets the variable <X> to the first memory location, 30000 for the routine, and then line 9060 increments <X> by one until the routine is totally inserted. Line 9080 resets <X> back to 30000 so that it can be called with <USR X> in line 240. <USR 30000> would work just as well.

Other USR commands that work for various routines are <PRINT USR X> and <RAND USR X>. Change line 240 in the FLAG program to:

240 RAND USR X or 240 GOTO USR X

I see by the old clock that our session is coming to a close. For your homework, I want you to scour your TS or ZX textbooks for assembly or machine language programs whose listings end with C9 and enter the code into the string variable <Z$> in the 9000 routine and then build a program around that routine to create some desired effect.

Send me in the mail any unusual or interesting effects with either a listing or on tape and I will share them with the rest of you.

CLASS DISMISSED!

PS – our thanks to those of you who responded to our request for machine language input – we should have something for our readers in the upcoming issues on a regular basis. We intend to keep it in a format that is easy for beginners to understand. We would appreciate letters regarding this month’s tutorial, whether it was easy to understand or not and especially any unique methods using this technique or any others – Ed.

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