Loading Hexadecimal Listings

Author(s): David Ornstein
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n6 (Volume 3, Issue 6)
Date: June 1982
Pages: 18-19

Have you ever skipped a machine code (MC) program because use the BASIC loader program given with it looked hard? Here is a solution. To use a hex MC listing, just type the hex codes into a REM statement. This program converts the hex and stores it into a space explicitly for it.

As an example, look at this MC program to add 10 and 20 in hex:

HEX     ASSEMBLY     COMMENTS
3E 10   LD A,10     ;first number
06 20   LD B,20     ;second number
80      ADD A,B     ;A=A+B
06 00   LD B,00     ;B=00
4F      LD C,A      ;BC=00xx
C9      RET         ;return

With a listing like this, you’ll often find a loader program to help you enter the code. It usually asks you for a start address (where to put the code), then lets you type in hex (or decimal). The lines you enter appear on the screen and scroll. As you type in codes, the computer POKEs the binary equivalents into successive memory locations, usually in a REM statement.

This method has several drawbacks. First, whenever you want to use or test the MC, you must use the loader and type it all in again. Second is the messy REM statement. You often have to type in something like:

   1 REM (437 spaces)

just to use other loaders.

Third, you must be careful not to LIST the program, or that REM line might crash the system.

To use our example program with the XFER loader (listing follows), type in these two lines, with the XFER program itself:

  10 REM ( 3E 10 06 20 80 06 00 4F C9
20 REM $

RUN. After it loads your MC, it will GOTO 1000. The code at 1000 executes your machine code via a USR statement. The way the code runs gives you these features:

1. You can place your MC REM lines anywhere in the program, as long as they each open with a ( character, and the set of them is eventually followed by one REM $ statement.

2. Any number of spaces may precede or follow the hex codes. But do not put any spaces before the opening ( or the terminating $ after REM.

So these lines are legal:

  10 REM (3E    1006 20   80
20 REM ( 06004F C9
30 REM $

You may also interleave any number of BASIC lines (including standard REMs) with your hex code REMs. The same rule applies to the REM $ statement. The only restrictions are that the lines must appear in a linear order and you must not split the hex codes themselves (ie, inserting spaces between the 2 digits of a single code).

If you make a mistake, just edit the REM statement as you would any BASIC line.

XFER fills a string variable, Z$, with binary bytes of MC, converted from hex. Note the LET Z$ at line 110 must be the first assignment (LET) statement executed in the program. Be sure to use the token for REM in line 330. Type 330, then REM (E key), then backspace with the left arrow key and type IF X$…, then move with the right arrow key to finish the line.

 100 REM XFER
110 LET Z$=""
120 LET A=16513-5
140 LET SA=PEEK 16400+PEEK 16401*256+3
200 REM MAIN LOOP
210 GOSUB 300
220 IF X$="(" THEN GOSUB 400
230 IF X$="$" THEN GOTO 1000
240 GOTO 200
300 REM FIND A REM
310 LET A=A+5
320 LET X=PEEK A
325 LET X$=CHR$ X
330 IF X$=" REM " THEN GOTO 370
340 LET A=A+1
350 IF PEEK A=118 THEN GOTO 300
360 GOTO 340
370 LET A=A+1
380 LET X=PEEK A
385 LET X$=CHR$ X
390 RETURN
400 REM PROCESS A LINE
410 GOSUB 500
420 IF EOL=1 THEN RETURN
430 LET H=(X-28)*16+PEEK (A+1)-28
440 LET A=A+1
450 LET Z$=Z$+CHR$ H
460 GOTO 400
500 REM FIND A NON-SPACE CHAR
505 LET EOL=0
510 LET A=A+1
520 LET X=PEEK A
530 IF X=0 THEN GOTO 510
540 IF X=118 THEN LET EOL=1
550 LET X$=CHR$ X
560 RETURN
1000 REM DONE WITH XFER
1010 LET VALUE=USR SA
SYNTACTIC SUM: 43593, 8K

To place machine code in the first line (REM) of the program, make these mods:

delete 450, 140
130 LET D=16514
140 LET SA=D
450 POKE D,H
451 LET D=D+1

When you want to call your machine code, use a USR call to SA. This is illustrated in line 1010.

If you enter the example MC program here, change line 1010 to PRINT USR SA. The answer should be 48, which is 10+20 in hex.

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