Beginner’s Loading ML Programs

Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n8 (Volume 2, Issue 8)
Date: August 1981
Pages: 11-12

This month we’ll learn how to put machine language (ML) programs into your computer. You may not understand ML programs you use, but following these directions, you can load any ML program. We have 2 ways to do this, with decimal or hexadecimal (hex) numbers.

In COMPUTER NUMBER SYSTEMS, Mar.81, we learned to convert decimal numbers, our usual 1-10 counting system, into hex (or base 16) numbers, and vice versa. Far from being just a math exercise, this skill will now come in handy.

In June’s SYNTAX, we saw that BASIC command POKE A,B puts code B into address A. POKE uses decimal numbers. You can POKE decimal ML code, just as we POKEd character code. Try this simple ML program.

Here’s the decimal listing for a 4K ML program to add 45 and 15. (It’s easier, of course, to write a BASIC program to do this, but we need a simple example.)

62 45 46 15 141 111 38 0 201

LOAD the PLACING USR CALLS IN 4K, 8K program (Jun.81 p.8) for 4K; use 1 for K. Our ML program has 9 commands, so use 9 for N, the number of bytes to reserve. Now you have RAM space that BASIC can’t touch (otherwise, the computer could crash).

USR CALLS gives our first available RAM address as 16382-N+(1024*K). In this case, N=9 and K=1, so our first address is 17399. Simply POKE the decimal numbers one at a time into sequential RAM addresses, starting at 17399:

  10 POKE 17399,62
20 POKE 17400,45
30 POKE 17401,46
40 POKE 17402,15
50 POKE 17403,141
60 POKE 17404,111
70 POKE 17405,38
80 POKE 17406,0
90 POKE 17407,201
100 PRINT USR(17399) RUN

Don’t worry right now what the codes stand for. Lines 20 and 40 enter the numbers to add; change these to add other numbers (whose sum doesn’t exceed 255). This way, you can enter ML programs, whether or not you know how they work.

It’s pretty tedious typing all those POKE statements; let’s program the computer to do some of the work for us using a FOR-NEXT loop (see FOR-NEXT Loops, Nov.80):

  10 FOR I=0 TO 8
20 INPUT X
30 POKE 17399+I,X
40 NEXT I
50 PRINT USR(17399) RUN

You will get 9 input prompts. Type each decimal number, hitting NL after each. When you’ve entered the last one, the answer to 45+15 will appear on the screen.

We can add BASIC frills to the program by adding and changing the following lines:

   5 PRINT "ENTER DECIMAL NUMBERS ONE AT A  TIME.  HIT NL AFTER EACH."
50 PRINT "45+15=";USR(17399)

We can also enter the ML program in hex. Convert each decimal number into hex, using Bill Herron‘s decimal-to-hex conversion programs (Mar.81) or a hex calculator (or paper and pencil).

Here’s our hex listing:

3E 2D 2E 0F 8D 6F 26 00 C9

Each hex number corresponds in order to a decimal number.

To enter hex code directly, use a BASIC program like Matthew Johnson‘s Hex Monitor program (May 81). Again, reserve the 9 bytes our program takes up. Type one 2-digit hex number in response to each prompt from that program (each number must have 2 digits; that’s why 0 is written as 00). Just hit (NL) after the “STORAGE LOCATIONS” prompt. This program automatically provides the USR call (line 50 in our BASIC program above).

The 4K Hex Monitor won’t work with an 8K ROM. You can POKE the decimal listing in 8K, but use these decimal numbers instead:

62 45 46 15 141 79 6 0 201

(Include line 50’s USR call at the end.) Two commands change because of the differences between the 4K and 8K ROM’s USR command.

The POKE method may be easier for beginners. If you have an assembly with no decimal listing, just change the hex numbers to decimal and POKE them into addresses you reserved using PLACING USR CALLS. In SYNTAX assembly listings, hex code is in the second column of numbers. Remember that hex numbers are always expressed in 2 digits, even when they only need single digits (type 9 as 09, for example). So convert the hex numbers 2 digits at a time. If you see a line like 3E40, read it as 2 hex numbers, 3E and 40.