HEXEDIT is a short simple BASIC program that will allow you to write, enter and debug hexadecimal machine code routines with a minimum of frustration and wasted time. HEXEDIT makes the proofreading and correction of machine code programs especially easy since the one to four bytes of each Z80 instruction are stored in a separate REM statement; this produces an easily understood display like those found in larger disassemblers. Corrections can then simply be made with the user-friendly cursor-driven facilities of the Sinclair 8K ROM (LIST, EDIT, DELETE, cursor controls and so on). Even users who know little about machine language programming can use HEXEDIT to great advantage when typing in other peoples’ programs. It requires only 2K of RAM for most applications.
HEXEDIT: The BASIC Version
Type in the program shown in Listing 1, then proofread and correct it carefully. Try out HEXEDIT by typing this short machine code test routine in REM statements into the main program:
10 REM D7
20 REM 18
30 REM FD
(You could also enter this at 10 REM D718FD.)
Save a copy in case it crashes, and then run it. The screen should go white for a few seconds and then give you the number of bytes of code that have been processed (in this case three). List the program and you should now see your translated code in line 1:
1 REM NOT / CLEAR. . . . . (etcetera)
Your three bytes of machine code have been translated and POKEd down into the 1 REM line; another copy has been placed above RAMTOP which will have been automatically re-set to 18176. If you make a mistake entering code in any of the REM lines after line 1, you can use the 8K ROM’s cursor and editing functions to bring it down and make any corrections needed. Now execute these three bytes of code in line 1 by:
RAND USR 16514
in the immediate mode and the screen will fill with graphics “L”s. If this doesn’t work you’ve made an error somewhere; find and correct it. Once everything is working properly, delete lines 10 through 30 and edit out the three-byte subroutine stored in the top line so it looks just like Listing 1. Save this on a fresh tape as HEXEDIT.
1 REM ................................
9700 FAST
9710 POKE 16389,71
9720 CLEAR
9730 LET L=16514
9740 LET H=18176
9750 LET P=L
9760 LET P=P+1
9770 IF PEEK P=118 AND PEEK (P+5)=234 THEN GOTO 9800
9780 GOTO 9760
9800 LET P=P+5
9810 IF PEEK P<>234 THEN GOTO 9900
9820 LET P=P+1
9830 POKE L,16*PEEK P+PEEK (P+1)-476
9840 POKE H,PEEK L
9850 LET L=L+1
9860 LET H=H+1
9870 LET P=P+2
9880 IF PEEK P=118 THEN GOTO 9800
9890 GOTO 9830
9900 PRINT "BYTES= ";L-16514
Listing 1. HEXEDIT
Inverse
Once you have tested and debugged HEXEDIT, you may use it to enter longer machine language subroutines. INVERSE is a useful 26-byte program which can be used in any BASIC program to quickly change every character on the screen to its inverse and vice versa. Load HEXEDIT from your previously saved tape and add the additional REM lines (10 to 160) shown in Listing 2. Do not type in the assembly-language mnemonics in the right-hand column, however; they are included for interest only. Make some typographical errors on purpose and then correct them (and any others) to see how easy this is. When you are fairly sure the code is error-free, save everything to tape just in case you crash. Make sure there are at least 26 characters after 1 REM so there will be enough space to store the program (a few extra does not hurt.)
10 REM 2A0C40 LD HL,(D-FILE)
20 REM 23 INC HL
30 REM 0605 LD B,05
40 REM C5 PUSH BC
50 REM 0691 LD B,91
60 REM 7E LD A,(HL)
70 REM C600 ADD A,00
80 REM FE76 CP 76
90 REM 2803 JR Z,03
100 REM C680 ADD A,80
110 REM 77 LD (HL),A
120 REM 23 INC HL
130 REM 10F3 DJNZ,F3
140 REM C1 POP BC
150 REM 10ED DJNZ,ED
160 REM C9 RET
Listing 2. INVERSE
Now run and you will get a report of BYTES=26. Write this down for later use and then press ENTER. You will see that your translated code for INVERSE has been placed in the top REM line. (Never try to edit this line after code has been transferred into it.)
Since you have all this stored on another tape, it is safe to try to execute the code for INVERSE: Just use:
RAND USR 16514
and 0/0 will appear if everything is okay. (INVERSE does nothing at this point since the display file is empty.)
You could isolate the code in the 1 REM line by deleting every single line below it, but this would be a rather unpleasant chore, especially with longer programs. Since HEXEDIT has automatically moved RAMTOP down to 18176 and has placed another copy of INVERSE above this, you can simply use NEW and ENTER to erase everything else. All the BASIC lines will be removed, but the code above RAMTOP will be preserved. If you don’t believe this try:
PRINT PEEK 18176
and 42d, the first byte of INVERSE, will appear on the screen. Now type in DOWNLOAD, exactly as shown in Listing 3. Proofread it and make sure there are at least 26 characters in line 1 after the REM. Also, make sure you are in the FAST mode. Now save DOWNLOAD to tape. (I find it is convenient to keep it on side two of my C-5 HEXEDIT tape.)
1 REM ................................
2 PRINT "HOW MANY BYTES? ";
3 INPUT N
4 PRINT N
5 LET H=PEEK 16388+256*PEEK 16389
6 FOR I=0 TO N-1
7 POKE 16514+I,PEEK (H+I)
8 NEXT I
Listing 3. DOWNLOAD.
Run the program, input the number of bytes when prompted (26), and INVERSE will be transferred down into the 1 REM line of DOWNLOAD. Delete lines 2 through 8 and save your finished copy of INVERSE in 1 REM to tape.
Test the INVERSE routine by adding the BASIC lines shown in Listing 4. Run SCREENFILL and when the screen is full tap any key except BREAK and watch the screen “invert”. If you have made a mistake simply reload the tape you saved of HEXEDIT with INVERSE included, correct the errors, and repeat downloading.
1 REM [illegible]
10 SLOW
20 LET A$=CHR$ INT (RND*64)
30 FOR I=1 TO 8
40 LET A$=A$+A$
50 NEXT I
60 PRINT A$;A$;A$( TO 192)
70 IF INKEY$="" THEN GOTO 70
80 LET X=USR 16514
90 GOTO 70
Listing 4. SCREENFILL.
Always be sure there is enough space in the first line dummy REM statement to hold any code you are going to put there. Otherwise some very nasty things can happen. (If you don’t believe this, try it!) It is a good policy to always save before running or a USR call when using HEXEDIT. It only takes a few seconds to save and it will avoid much (re)typing time.
Never try to bring down the 1 REM line itself for editing once it has been filled with code. If your program contains 7E, the five bytes that follow will simply vanish from your program and you will crash. (7E is the code for LD A, (HL); it occurs in most programs!)
With HEXEDIT and DOWNLOAD in your software library you can write and debug your own hexadecimal machine language programs or use it to enter other people’s listings. Debugging and editing are much easier since the component bytes of each Z80 instruction are listed out to exactly the right length. Since both programs are so short, you can save or re-load in a matter or seconds as “insurance” against crashes. Once you have become familiar with these two routines, I think you will use them frequently and find them to be great time savers.
HEXEDIT: 16K RAM Version
You can increase the capacity of HEXEDIT if you plan to write and edit routines longer than 256 bytes and you have more than 2K of RAM. If you have 16K, for example, you can simply make the following changes in HEXEDIT:
9710 POKE 16389, 100
9740 LET H=25600
This will reset RAMTOP to 25600 instead of 18176. DOWNLOAD doesn’t need any changes since line 5 PEEKs RAMTOP for the starting address of the higher copy. Remember that whenever you use DOWNLOAD you will need to be sure that there are enough characters in the first line to hold all the bytes of your code. The price you will pay for this extended capacity is that loading and saving will take a bit longer; the 2K version is preferable for most applications of HEXEDIT.
HEXEDIT2: The Machine Code Version
You can use the BASIC 2K version of HEXEDIT to help produce a machine code version of itself! I have written this in 83 bytes, but I am sure other programmers will be able to improve upon this. HEXEDIT2, the machine code version of HEXEDIT, runs quite a bit faster and takes up a lot less RAM space. As presented, it operates nicely in 2K or more.
Load your previously tested and debugged tape of HEXEDIT. Now type in the REM statements containing the hexadecimal code for HEXEDIT2 (Listing 5) beginning with 10 REM FD360547. Do not type in the assembly language mnemonics in the right hand column. Proofread and edit the listing very carefully and change line 1 so that it contains at least 83 characters after REM (more is okay). Now save everything on tape. Run and you should get a “bytes” report of 83; write this down. Now list and your code will be seen in the 1 REM line. Use NEW to erase all the BASIC lines, then type in DOWNLOAD2 as shown in Listing 6. We want to end up with out first line REM statement having a high line number, 9901 in this case, so our code for HEXEDIT2 can reside at the very end of the program, just as it did in the BASIC version. Make sure that there are exactly 83 characters after the REM in line 1. Now run. The 83 translated bytes of code will appear in line 9901. Do not edit this line under any circumstances. Delete lines 9910 to 9930 and add lines 1 and 9900 to produce Listing 7. Make sure you get line 9900 exactly right, as this chooses the starting address (D-FILE)-84) that the code in line 9901 is to be run from . Save these three lines on tape as HEXEDIT2. It should look just like Listing 7 when you are done.
10 REM FD360547 LD(IY+05),47
20 REM CD9A14 CALL 149A
30 REM 110047 LD DE,4700
40 REM 018240 LD BC,4082
50 REM C5 PUSH BC
60 REM 60 LD H,B
70 REM 69 LD L,C
80 REM 23 INC HL
90 REM 7E LD A,(HL)
100 REM FE76 CP 76
110 REM 20FA JR NZ,FA
120 REM E5 PUSH HL
130 REM 23 INC HL
131 REM 23 INC HL
132 REM 23 INC HL
133 REM 23 INC HL
134 REM 23 INC HL
140 REM 7E LD A,(HL)
150 REM FEEA CP EA
160 REM E1 POP HL
170 REM 20EE JR NZ,EE
180 REM 23 INC HL
181 REM 23 INC HL
182 REM 23 INC HL
183 REM 23 INC HL
184 REM 23 INC HL
190 REM 7E LD A,(HL)
200 REM FEEA CP EA
210 REM 201E JR NZ,1E
220 REM 23 INC HL
230 REM 7E LD A,(HL)
240 REM D61C SUB 1C
250 REM 07 RLCA
251 REM 07 RLCA
252 REM 07 RLCA
253 REM 07 RLCA
260 REM C5 PUSH BC
270 REM F5 PUSH AF
280 REM 23 INC HL
290 REM 7E LD A,(HL)
300 REM D61C SUB 1C
310 REM 4F LD C,A
320 REM F1 POP AF
330 REM A9 XOR C
340 REM C1 POP BC
350 REM 02 LD (BC),A
360 REM 12 LD (DE),A
370 REM 03 INC BC
380 REM 13 INC DE
390 REM 23 INC HL
400 REM 7E LD A,(HL)
410 REM FE76 CP 76
420 REM 280A JR Z,0A
430 REM 18E3 JR E3
440 REM 60 LD H,B
450 REM 69 LD L,C
460 REM C1 POP BC
470 REM A7 AND A
480 REM ED42 SBC HL,BC
490 REM 44 LD B,H
500 REM 4D LD C,L
510 REM C9 RET
Listing 5. Code for HEXEDIT2.
9901 REM ...................................................................................
9910 FOR I=0 TO 82
9920 POKE 16514+I,PEEK (18176+I)
9930 NEXT I
Listing 6. DOWNLOAD2.
1 REM ................................
9900 PRINT USR (PEEK 16396+256*PEEK 16397-84)
9901 REM CLEAR [illegible]
Listing 7. HEXEDIT2.
You can test this program by entering the short routine described above.
10 REM D718FD
Running this should put NOT / CLEAR into the top REM line; RAND USR 16514 should fill the screen with “L”s, as before. I would suggest putting a copy of DOWNLOAD (Listing 3) on the second side of your HEXEDIT2 tape for convenient downloading. Remember that you will need to adjust the number of characters in the 1 REM line each time you use DOWNLOAD after NEW-ing away the BASIC lines. You should write this on your tape (or put it in a REM statement). As an exercise, try using HEXEDIT2 to produce a 1 REM line containing the code for INVERSE.
User’s Summary of HEXEDIT and DOWNLOAD
1. Load HEXEDIT.
2. Enter hexadecimal machine code in REM statement between lines
10 and 9690.
3. Proofread these REM statements carefully and edit out any errors
you have made in the code.
4. Add enough characters to line 1 so there is plenty of space for
your code.
5. Save in case of a crash.
6. Run.
7. Record the number of bytes of code reported.
8. Test your code with RAND USR 16514 (or whatever the appropriate
starting address is).
9. If it works, then erase all BASIC lines with NEW, ENTER.
10. Load DOWNLOAD from your tape.
11. Make sure the computer is in the FAST mode.
12. Adjust the number of dummy characters in line 1 so that it equals
the number of bytes reported above.
13. Run DOWNLOAD.
14. Input the number of bytes when prompted.
15. Delete lines 2 to 8.
16. Save and label this 1 REM line on tape for later use in your own
programs.
17. Execute the code with RAND USR 16514 or LET XXX=USR 16514 (or
the appropriate starting address).
Modifying HEXEDIT2
Just as with HEXEDIT you can extend the capacity of HEXEDIT2 if you need more than 256 bytes and if you have 16K of RAM. Plug in your 16K RAM pack (be sure to disconnect the power supply first!) and load HEXEDIT2. Now carefully enter the following four lines in the immediate mode.
POKE 16389, 100
CLEAR
POKE PEEK 16396 + 256 * PEEK 16397 – 81, 100
POKE PEEK 16396 + 256 * PEEK 16397 – 75, 100
This version will automatically change RAMTOP to 25600. Save this to tape. DOWNLOAD (Listing 3) will work fine, even with the change in RAMTOP.
You can make the machine code in line 9901 of HEXEDIT2 unlistable by using
POKE PEEK 16396 + 256 * PEEK 16397 – 89,-1
in the immediate mode. Now only lines 1 and 9900 will appear on the screen and you will have additional RAM space available for REM lines containing hexadecimal code. Even though you cannot see line 9901, it is still there and will be executed by line 9900. (This technique will work in either the 2K or 16K version.)
Either version of HEXEDIT with DOWNLOAD will save you many, many hours of debugging time and will increase the pleasure that efficient machine language programming brings to the Timex Sinclair user. I hope you will find these routines as helpful as I have and welcome hearing about any refinements you might make.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.