Applying the ZX Assembler

See all articles from ZX-Appeal Dec 89
Author(s): Vince Lee
Publication: ZX-Appeal
Pub Details: ZX-Appeal Dec 89
Date: December 1989
Pages: 6-8
TIMEX SINCLAIR 1000 picture drawn on the screen by the program
TIMEX SINCLAIR 1000 picture drawn on the screen by the program

Last month we looked at printing to the screen using the help of the ROM’s RST 10 routine. This month we’ll try it without using the ROM. The advantage is an increase in speed but on the down side is the lost of the error checking routines.

In a 16K ZX/TS, an area in memory is set aside to hold the picture information for the ULA chip to produce the display on the screen. This area is known as the “dislay file”. Place a character’s hex code into a specific location in this file and the character will appear in its corresponding place on the screen. However there are three important things to remember; This file dose not have a fixed location, information is stored in a particular format and expanded characters placed in the file will corrupt the display.

There is another area of memory reserved by the computer which contains information used by the system. Hense it is called the “System Variables”. At location 16396, 16397 named “D_FILE”, the starting location of the display file is stored. (You should also note that location 16400, 16401 named “VARS” contains the starting location of the stored variables. Since variables are placed right after the display file in memory, this address can also be thought of as the first memory location after the dislay file.)

The display file uses hex code 76 (or if you prefer, the instruction HALT) as markers. This is automaically set up by the computer. The very first byte in the file contains a marker. It is then followed by twenty-four sets of 33 memory locations. The twenty-four sets represents the rows on the screen. The first 32 locations in each set represents the columns in the row while the 33rd location contains a line end marker.

Let’s take a look at the machine code program which draws a picture of a TS1000. It was originally written in BASIC by Wayne McCarthy and it has appeared in one of the early newsletters. We begin by setting up the variables to make the listing easier to follow. The HL register will be used as the screen pointer. We start by locating the address of the “screen” in memory. Notice how we skip the first address with the INCrement instruction because it contains a marker. We then CALL on our subroutines to do their chores and then we return back to BASIC.

Just like last time we are using a system known as parameter passing. In this case, the subroutines are used to to set up the environment to CALL on one of the three printing routines, PRROW, PPOKE or MPOKE. The PUSH HL instruction saves a copy of the “screen address” to the STACK. This allows us to manipulate the pointer to print to different lines on the screen. And when we are finish, the POP HL instructions returns the HL pointer back to its original position.

PRROW is used to print lines of a single character to the screen. It is used to draw the case and the line across it. The start of the row is located. The B register is set up to loops thirty-two times to fill up the line while the C register is set up to loop for the number of lines to be filled. At the end of each line, the address is INCremented to avoid overwriting the marker. This routine can also be used to clear the screen by loading the “space” character in register A.

PPOKE is used to print text to the screen. Two pointers are used. One which keeps track of the “screen” while the other keeps track of the letters printed. It is possible to print more than one line at a time by over printing the line end marker with another marker. However if you “erase” a marker, the display will be corrupted. Mutiples of twirty-thee, locates the start of each line while an offset less than thirty-one, allows for a “TAB” function. MPPKE is just a modified version of the PPOKE routine. This new routine allowed the two graphic characters to sanwiched the letters on the keys.

One of the most neglected aspects of programming is proper documentation. It does mean more work but it is important that the codes be understood for future references. The header which are the instructions at the beginning of the routine explains what the routine is and how to use it. The explainations in the comments column explains the purpose of the code. Notice in the PRROW routine, the first four lines of code was summed up with the comment “fill row with character stored in A”. We didn’t explain how the B register is set up to loop thirty-two times. Nor the fact that the HL register pair is being INCremented. This can be seen just by looking at the code. But what can’t easily be seen is why are we doing this?

Just how slow is BASIC? Run this program and a picture of the TS1000 will instantaneouly appear while the BASIC part will crawl along at a snail’s pace as it tries to number the lines on the screen. At the end of the program press the C key if you want a copy of the screen printed out onto the TS2040 printer.

      ;-----------------------;
; TS1000 ;
;-----------------------;
; ;
; THIS PROGRAM GIVES AN ;
;EXAMPLE OF THE VARIOUS ;
;METHODS OF PRINTING TO ;
;THE SCREEN WITHOUT ;
;USING THE ROM ROUTINES.;
;-----------------------;

ENDM= FF ;MSSGE END MARKER.
INVSP=+128 ;CHARACTER CODE.
CHKER=+136 ;CHARACTER CODE.
CKINV=+138 ;CHARACTER CODE.
COLSP=+5 ;CHARACTER CODE.
SPCOL=+133 ;CHARACTER CODE.
LOOP4=+4 ;LOOP TO PRT KEYS.
DFILE=+16396 ;LOCATE D. FILE.
COLMN=+32 ;32 COL PER ROW.
LIN1= +1 ;PRINT 1 LINE.
LIN24=+24 ;PRINT 24 LINES.
NXLIN=+34 ;SKIP TO NEXT LIN.
ATR1= +33 ;AT ROW 1, 33*1.
ATR12=+396 ;AT ROW 12, 33*12.
ATR15=+495 ;AT ROW 13, 33*15.
COPY2=+02155 ;COPY2 ROUTNE ADR.

;-----------------------;

START LD HL,(DFILE);FIND START
INC HL ; OF SCREEN.
CALL CASE ;PRNT CASE.
CALL STEP ;PRNT STEP.
CALL NAME ;PRNT NAME.
CALL KEYS ;PRNT KEYS.
RET

CASE PUSH HL ;SAVE SCN LOC.
LD C,LIN24 ;PRINT
LD A,INVSP ; 24 LINES OF
CALL PRROW ; INVSPACE.
POP HL ;RSTORE S LOC.
RET

STEP PUSH HL ;SAVE SCN LOC.
LD DE,ATR12;BEGIN PRINTNG
ADD HL,DE ; AT LINE 12,
LD C,LIN1 ;ONE
LD A,CHKER ; LINE OF
CALL PRROW ; CHECKERS.
LD C,LIN1 ;THEN
LD A,CKINV ; ONE LINE OF
CALL PRROW ; CK/INV.
POP HL ;RSTORE S LOC.
RET

;-----------------------;
; PRROW ;
;-----------------------;
; THIS ROUTINE WILL ;
;PRINT ROWS OF UNEXPAND.;
;CHAR. TO THE SCREEN. ;
; ;
;HL POINTS TO BEGINNING ;
; OF THE ROW IN DISPL.;
; CONTENTS IS ALTERED.;
;C CONTAINS THE NUMBER ;
; OF ROWS TO BE PRTED.;
; CONTENTS IS ALTERED.;
;A CONTAINS THE HEX ;
; CODE OF THE CHARCTER;
; TO BE PRINTED. ;
; ;
;B CONTENTS IS ALTERED.;
;CAUTION- NO ERROR TRAP ;
; IN ROUTINE. ;
;-----------------------;

PRROW LD B,COLMN ;FILL
CLOOP LD (HL),A ; ROW WITH
INC HL ; CHARACTER
DJNZ CLOOP ; STORED IN A.
INC HL ;SKIP "ENTER".
DEC C ;CONT TILL ALL
JR NZ PRROW; ROWS CPLETE.
RET

;-----------------------;

NAME PUSH HL ;SAVE S LOC.
LD DE,ATR1 ;MOVE PRINT
ADD HL,DE ; POS TO ROW1.
LD DE,NMCHR;PRINT
CALL PPOKE ; NMCHR.
POP HL ;RSTORE S LOC.
RET

NMCHR 80 B9 AE B2 AA BD
"SINCLAIR 1000" FF

KEYS PUSH HL ;SAVE S LOC.
LD B,LOOP4 ;ADJ 4 LOOPS.
LD DE,ATR15;MOVE S POINTR
ADD HL,DE ; TO ROW 15.
LD DE,K1CHR;MOVE CHR PTR.
KLOOP CALL PPOKE ;PRINT
INC DE ; TAB CHRS.
CALL MPPKE ;PRINT
INC DE ; CHARACTERS.
CALL PPOKE ;PRINT ROW
INC DE ; FILL CHRS.
PUSH DE ;SAVE CHR PTR.
LD DE,NXLIN;SKIP A
ADD HL,DE ; SCREEN LINE.
POP DE ;RSTORE CH PR.
DJNZ KLOOP ;CONT. TILL
; K4CHR FNISH.
POP HL ;RSTORE S LOC.
RET

K1CHR FF
"1234567890" FF
80 80 FF

K2CHR 80 FF
"QWERTYUIOP" FF
80 FF

K3CHR 80 80 FF
"ASDFGHJKL=" FF
FF

K4CHR FF
"/ZXCVBNM.£" FF
80 FF

;-----------------------;
; PPOKE ;
;-----------------------;
; THIS ROUTINE WILL ;
;PRINT UNEXPAND CHARS ;
;TO THE SCREEN. ;
; ;
;HL POINT SCREEN LOC. ;
; CNTNTS IS ALTERED ;
;DE POINT FIRST CHAR. ;
; TO BE PRINTED. ;
; CNTNTS IS ALTERED.;
;"FF" END OF CHR MARKER.;
; ;
;A REG IS ALTERED. ;
;CAUTION, NO ERROR TRAP.;
;-----------------------;


PPOKE LD A,(DE) ;GET CHAR.
CP ENDM ;QUIT IF IT
RET Z ; IS THE ENDM.
LD (HL),A ;PRINT
INC HL ; CHARACTER.
INC DE ;UPDT CHR. PTR.
JR PPOKE ;GO GET NX CHR.

;-----------------------;
; MPPKE ;
;-----------------------;
; THIS IS A MODIFIED VER;
;OF PPOKE. THIS VERSION ;
;WILL PRINT IN THE ;
;FORMAT, COL/SPACE CHAR ;
; DESIRED CHAR, ;
; SPACE/COL CHAR.;
; ;
;HL POINT SCREEN LOC. ;
; CNTNTS IS ALTERED.;
;DE POINT FIRST CHAR. ;
; CNTNTS IS ALTERED.;
;"FF" END CHR MARKER. ;
; ;
;A REG IS ALTERED. ;
;CAUTION, NO ERROR TRAP.;
;-----------------------;

MPPKE LD A,(DE) ;GET CHR.
CP ENDM ;QUIT IF IT
RET Z ; IS ENDM.
LD (HL),COLSP;PRINT
INC HL ; COLSP.
LD (HL),A ;PRINT
INC HL ; CHARACTER.
LD (HL),SPCOL;PRINT
INC HL ; SPCOL.
INC DE ;UPDT C PTR.
JR MPPKE ;GO GET NEXT
; CHR.

;-----------------------;
; CP24L ;
;-----------------------;
; THIS ROUTINE WILL COPY;
;ALL 24 LINES OF THE ;
;SCREEN TO THE TS2040 ;
;PRINTER. ;
;-----------------------;

CP24L LD D,LIN24 ;HOW MANY LIN?
CALL COPY2 ;COPY THEM.
RET

;--------- END ---------;
   5 REM -----------------------
THIS PROGRAM WILL
DEMONSTRATE THE SPEED IN WHICH
MACHINE CODE PRINTS TO THE
SCREEN AS COMPARED TO BASIC.
6 SLOW
10 REM
------- TS1000 --------
MACHINE CODE
20 RAND USR 16516
100 REM
--- LINE NUMBERING ---
BASIC
110 POKE 16418,0
120 FOR A=0 TO 23
130 PRINT AT A,0;A
140 NEXT A
200 REM
-------- PAUSE -------
PRESS <C> FOR COPY OF PICTURE
TO TS2040 PRINTER.

PRESS ANY OTHER KEY TO QUIT.

NOTE - INSERT LINE,
30 GOTO 200
INTO THE PROGRAM TO GET
PICTURE OF JUST TS1000.
210 IF INKEY$="" THEN GOTO 210
220 IF INKEY$="C" THEN RAND USR
16723
4084   2A 0C 40 23 CD 95 40 CD
408C 9F 40 CD BF 40 CD E0 40
4094 C9 E5 0E 18 3E 80 CD B4
409C 40 E1 C9 E5 11 8C 01 19
40A4 0E 01 3E 88 CD B4 40 0E
40AC 01 3E 8A CD B4 40 E1 C9
40B4 06 20 77 23 10 FC 23 0D
40BC 20 F6 C9 E5 11 21 00 19
40C4 11 CC 40 CD 3B 41 E1 C9
40CC 80 B9 AE B2 AA BD 38 2E
40D4 33 28 31 26 2E 37 00 1D
40DC 1C 1C 1C FF E5 06 04 11
40E4 EF 01 19 11 00 41 CD 3B
40EC 41 13 CD 44 41 13 CD 3B
40F4 41 13 D5 11 22 00 19 D1
40FC 10 EC E1 C9 FF 1D 1E 1F
4104 20 21 22 23 24 25 1C FF
410C 80 80 FF 80 FF 36 3C 2A
4114 37 39 3E 3A 2E 34 35 FF
411C 80 FF 80 80 FF 26 38 29
4124 2B 2C 2D 2F 30 31 14 FF
412C FF FF 18 3F 3D 28 3B 27
4134 33 32 1B 0C FF 80 FF 1A
413C FE FF C8 77 23 13 18 F7
4144 1A FE FF C8 36 05 23 77
414C 23 36 85 23 13 18 F1 16
4154 18 CD 6B 08 C9

HEX LIST OF TS1000 PICTURE

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