Applying the ZX Assembler

See all articles from ZX-Appeal Mar 90
Author(s): Vince Lee
Publication: ZX-Appeal
Pub Details: ZX-Appeal Mar 90
Date: March 1990
Pages: 8, 13-14

There is a function used in BASIC called “INKEY$”, which lets a program read which keys have been pressed on the keyboard. This allows menu driven programs and arcade style games to be possible. This month we are going to assemble our own routine to use in our machine language programs. Parts of the code came from Toni Baker‘s, “Mastering Machine Code” and from Marcio V.‘s article in the May 85 newsletter.

The keyboard is really a series of interconnecting switches. The computer can locate which switch has been pressed by scanning the matrix. If none of the keys are pressed, the scan returns a value of FFFFH. (“H” is used to indicate a hex number.) If a key is pressed, a value representing the location is returned.

The computer can be directed to scan the matrix by CALLing on the ROM routine located at 02BBH. The value will be sent to register pair HL. The scan can also be obtained by peeking into the System Variables’s “LAST_K” when the computer is operating in the SLOW mode.

As part of its housekeeping chore, the ROM’s display routine also performs a keyboard scan. This is how the system knows which keys have been pressed. It then stores this data in the System Variable’s LAST_K which we can access simply by “peeking” into its location 4025H, 4026H. Using the register pair BC to perform this task will be an advantage as you will later see.

What are the advantageous and disadvantageous of these two methods? Execution time is much faster using LAST_K than using the ROM routine. However LAST_K can only be used when the computer is operating in the SLOW mode. The system does not generate a display and thereby does not generate a keyboard scan when it operates in the FAST mode.

Now that we have the location code we need to convert it to the key that it represents. This is accomplished by LoaDing register pair BC with the code and then CALLing the ROM routine located at 07BDH. Register pair HL will then point to the character in memory which was pressed.

Let’s look at the routine called INKEY. We peek into LAST_K with register BC. Two loops are used to test for a legitimate key press. When a key is pressed, the scan value is less than FFFFH. INCrement it and it will still be “less” than 0000H. The JR NZ instruction continues the first loop until the fingers leave the keyboard. If none of the keys are pressed, the scan values is FFFFH. INCrement it and it becomes 0000H. The JR Z instruction continues the second loop until a key is pressed.

Now we have a genuine key press. The scan value is DECremented to return it back to its original value. Since register pair BC already contains the code, we can continue and CALL on the DECODE routine. We can now LoaD register A with the character that was pressed. A different register can be used but this one works the best.

Let’s take a look at the SCRLL routine. It is similar to the SCROLL routine used in BASIC. The TV display is nothing more than a mirror image of the memory’s display file. Manipulating this area causes the same effects on the screen. LDIR is a block move instruction with its own “FOR NEXT” loop. The BC number of consecutive bytes are moved from location HL to DE. This shifts all the bytes up by “one line”.

The STACK is a “shelf” in memory used for temporary storage. When we left BASIC with the USR statement, a return address was automatically placed there. Thus when we are ready to switch back, we use the instruction RET which retrieves this 16 bit number off of the “shelf” and uses it as a guide.

Other instructions also make use of the STACK. CALLing a subroutine will place a return address, while its counterpart RETurn, remove it for its use. PUSHing a register pair places a copy of its contents while POPing removes the copy from the STACK and place it to a designated register pair.

Only items at the top of the STACK can be acessed. This means that no matter how many items are stored, the last item placed are also the first item taken. And whatever’s placed on the STACK must eventually be removed to allow the designated RETurn instruction to acess the address for BASIC.

There are tricks that you can play with the STACK. In “Mastering M/C”, it was used as a pointer for printing characters to the screen. The instruction “LD BC,HL” does not exist. But it can be accomplished with two instructions, PUSH HL and then POP BC. You can even change a RET instruction into a “GOTO” instruction by manually changing the value of the return address.

The EXIT routine clears the screen and then returns the system back to BASIC. We accessed this option with the JR instruction which does not place an address on the STACK. And since the STACK is cleared of additonal data, the RETurn instruction finds the address to BASIC.

Let’s take a look at the whole program which prints unexpanded characters from the keyboard to the screen. As usual we begin by defining some variables to make the listing easier to follow. The numbers with the “+” in front tells the Assembler that they are decimals and that they are to be converted into hex. Next we calculate the address for the various locations on the screen and then set up register HL as a pointer for line 21 and register B as a counter for the number of characters allowed per line. We also provide an area for saving the calculations as the registers will be altered in the program.

We CALL INKEY to find which keys have been pressed. Certain keys have been given command functions and will be tested. The ComPare instructions are used along with conditional branch instructions to form “IF THEN” statements like those used in BASIC. ComPare tests by subtracting. The value in the specified source is always “compared” with the value in register A. The status are then placed in the FLAG register.

In this program we utilize both the ZERO flag and the CARRY flag. If the value in register A is equal to the source value, the ZERO flag will be “zero”. If the value in register A is not equal to the source value, the ZERO flag will be “not zero”. If the value in register A is less than the source value, the CARRY flag will show a “carry”. If the value in register A is equal to or greater than the source value, the CARRY flag will show “no carry”. Conditinal branch instructions such as JR cc, JP cc, CALL cc or RET cc can then be used to act accordinly.

If you pressed <SHIFT> <A> for STOP, the program will go to the EXIT routine. If you press <ENTER>, the program will go to the NLINE routine. And finally, if you press any key whose character code is greater than “Z”, the CP RAND and JR NC instructions forms a trap to prevent it from being printed and causing the system to crash. The “LD (HL),A” print routine is not set up to handle expanded characters.

We now use our pointer and counter to print our character and then make a decision, “Is the line full”? If it’s not full, the program will save the pointer and counter and then wait for the next key press. If it is full, the screen will scroll up one line, the pointer and counter will be reset and then the program will wait for the next key press.

Up till now we’ve only covered programming parts of the computer, the screen and now the keyboard. What’s next you ask? How about some real programming with real applications? How about programming I/O devices and developing programs from flow charts? And examine memory and I/O interfacing from a binary point of veiw. In short, how about pushing the ZX/TS to its limits?

      ;-------------------------;
; ;
; KEYBOARD TEST ;
;-------------------------;
; ;
; THIS PROGRAM PRINTS ;
;UNEXPANDED CHARACTERS ;
;FROM THE KEYBOARD TO ;
;THE SCREEN. ;
; ;
;PRESS ;
; <SHIFT><A> TO EXIT. ;
;-------------------------;

DCODE=07BD;ROM
CLS= 0A2A; ROUTINES.
SLOW= 0F2B;
DFILE=400C;SYSTEM
LASK= 4025; VARIABLES.
RND= 40 ;CHARACTER
ENTER=76 ; HEX
STOP= E3 ; CODE.
ROW= +32 ;ROW HAS 32 COLUMNS.
LIN1= +33 ;1 ROW *33COLS.
LIN20=+660;20ROWS*33COLS.
SIZE= +726;22ROWS*33COLS.

INITL LD HL,(DFILE);SAVE START
INC HL ; ADDRESS OF
LD (SCRN),HL ; "SCREEN".
LD DE,LIN1 ;SAVE ADDR
ADD HL,DE ; OF THE
LD (ATL1),HL ; 1ST LINE.
LD DE,LIN20 ;SAVE ADDR
ADD HL,DE ; OF THE
LD (ATL21),HL; 21ST LINE.
CALL SLOW ;SET SLOW M.

STPOS LD HL,(ATL21);SET PRINT
LD B,ROW ;POSITION.

SVPOS LD (LIN21),HL;SAVE
LD HL,RCOUN ; PRINT
LD (HL),B ; POSITION.

PRINT CALL INKEY ;GET KEY.

CP STOP ;DO YOU WANT
JR Z EXIT ; TO QUIT?

CP ENTER ;IS IT A
JR Z NLINE ; NEWLINE?

CP RND ;IS IT A
JR NC PRINT ; LEGL CHAR?

LD HL,RCOUN ;GET
LD B,(HL) ; PRINT
LD HL,(LIN21); POSITION.

LD (HL),A ;PRINT
INC HL ; CHARACTER.

;-------------------------;
; INKEY ;
;-------------------------;
; THIS ROUTINE IS USED ;
;TO FIND WHICH KEYS ARE ;
;PRESSED ON THE KEYBOARD ;
; ;
;USE ONLY IN SLOW MODE. ;
;REGS AF, BC, DE AND HL ;
; ARE AFFECTED. ;
;REG A CONTAINS THE KEY ;
; PRESSED IN HEX. ;
;-------------------------;

INKEY LD BC,(LASK);WAIT FOR
INC C ; FINGERS TO
JR NZ INKEY ; LEAVE KB.
WAIT LD BC,(LASK);WAIT
INC C ; FOR FINGERS
JR Z WAIT ; TO PRESS A
DEC C ; KEY.
CALL DCODE ;PLACE KEY
LD A,(HL) ; IN REG A.
RET

;-------------------------;
; SCRLL ;
;-------------------------;
; THIS ROUTINE WILL ;
;SCROLL UP THE FIRST 22 ;
;LINES ON THE SCREEN ;
; ;
;LINE 23 MUST BE BLANK. ;
;REGS BC, DE, AND HL ;
; ARE AFFECTED. ;
;-------------------------;

SCRLL LD HL,(ATL1);SCROLL
LD DE,(SCRN); SCREEN
LD BC,SIZE ; UP ONE
LDIR ; LINE.
RET

;-------------------------;
; EXIT ;
;-------------------------;
; THIS ROUTINE WILL END ;
;THE PROGRAM AND RETURN ;
;BACK TO BASIC. ;
; ;
;SCREEN IS CLEARED. ;
;ALL REGS. ARE AFFECTED. ;
; ;
;CAUTION- STACK MUST BE ;
; CLEAN. ;
; ;
;EVOKE WITH JR/JP EXIT. ;
;-------------------------;

EXIT CALL CLS ;CLR SCREEN.
RET ;EXIT TO
; BASIC.

SCRN 0000
ATL1 0000
ATL21 0000
LIN21 0000
RCOUN 00

;--------- END ---------;

[As printed, the listing jumps from “INC HL ; CHARACTER.” at the foot of page 13 to the INKEY routine on page 14: the rest of the PRINT routine (the “is the line full” test) and the NLINE routine it calls are not printed in this issue.]

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