Have you ever wished that you could clear just a portion of the screen? Recently, while working on a game program, I was faced with this problem. The playfield was printed on the upper portion of the screen, and I wanted to clear only the lower portion to print different prompting messages.
The PRINT AT command seemed to be the solution; however, I soon found out that, if the new message did not completely overwrite the old, some very unusual messages could appear. A nested FOR-NEXT loop could be used to generate spaces to overwrite the area before the new message is printed, but, if the area is very large, this could take several seconds. The only other solution seemed to be a machine code subroutine.
The machine code subroutine shown in Figure 1 searches the display file from a predetermined starting point, testing each location to determine if it contains the 118 end of line delimiter. If it does not, the byte is set to 0.
Figure 1. PARtial CLEaring MC subroutine.
Address Machine Label Instruction Comments
Code (d)
16514 30,0 LD E,0 ;set E to 0
16516 62,117 LD A,117 ;set A to 118
16518 60 INC A
16519 33,14,64 LD HL,16398 ;set HL to address
16522 78 LD C,(HL) of next PRINT
16523 35 INC HL location in
16524 70 LD B,(HL) display file
16525 197 PUSH BC
16526 225 POP HL
16527 1,74,1 LD BC,330 ;set counter
16530 205,152,64 CALL,16536 ;GOSUB "START"
16533 201 RETURN ;RETURN to Basic
16534 224 BC TEST RET,PO ;if BC=0 RETURN from SUB
16535 35 INC HL
16536 237,161 START CPI ;test (HL) for 118
16538 43 DEC HL
16539 40,249 JP Z,"BC TEST" ;if (HL)=118 jump to BC TEST
16541 115 LD (HL),E
16542 24,246 JR,"BC TEST"
NOTE: As shown in the article, this routine will clear the screen from PRINT position 12,0 to the end of the screen.
To understand the routine better, let’s look at the display file and one of the system variables related to it. First, and this is very important, the manual states that, if the total amount of memory, according to the variable RAMTOP, is larger than 3 1/4K, the display file is padded out with 24*32 spaces and generally remains at its full size. (Caution: Should the display file become reduced in size, it is possible to overwrite the VARIABLES area of memory. See the manual for details.) At addresses 16398 and 16399 is the variable DF-CC, the address of the next PRINT position in the display file. The manual states that this variable can be POKEd to change the location of the next PRINT, but an easier method is to use the PRINT AT x,x command. This saves calculating the address since the monitor will do it for you.
Figure 2. MC loader program.
1 REM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10 FOR I=16514 TO 16543
20 SCROLL
30 PRINT I;TAB 8;
40 INPUT N
50 PRINT N
60 POKE I,N
70 NEXT I
Type in this program, RUN, and ENTER. Then type in the numbers from the machine code column in Figure 1.
Now let’s look at the machine code. The first part of the program sets up the variables. The E register holds the 0 used to fill the display. In the A register is the 118 for the end of line test. The HL pair is the address of the display location to be tested, and the BC pair contains the number of locations to be tested.
At this point in the program we execute a CALL to the test routine labeled START. The CPI instruction compares the contents of the A register to the contents of the address pointed to by the HL pair. If both equal 118, the zero flag is set. The HL pair is then incremented, and the BC pair is decremented. If the BC pair now equals zero, the parity flag is set for “odd parity.” Since the CPI instruction incremented the HL pair, we now decrement it so that it still indicates the current location in the display file.
Next, if the zero flag was set, indicating that the location contained the end of line delimiter, we jump to BC TEST. Otherwise, the location is filled with 0, and then we jump to BC TEST. At BC TEST, if the parity flag indicates the “odd parity,” we return to the instruction following the CALL which is the RETURN to Basic. If the BC pair is now zero, the HL pair is incremented and the next location is tested.
The program in Figure 1 is held in the first Basic program line as a REM. To use the program, execute the two Basic lines:
110 PRINT AT 12,0
110 LET X=USR 16514
The PRINT AT location should be the place from which you wish to start clearing. I called this routine from three different locations in my program and used the command
LET X=USR 16514
each time with no problems.
In addition to using this program for clearing part of the screen, several other possibilities exist. Since the addresses in the program will not change if it is kept in the first Basic line, the value in the E register could be POKEd to make it any character and the value in the BC pair POKEd for any number of locations, allowing you to quickly fill any portion of the screen with any combination characters.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.