Machine Code Programming (part 1)

See all articles from Sinc-Link v2 n1
Author(s): Stan Piotrowski
Publication: Sinc-Link
Pub Details: Sinc-Link v2 n1 (Volume 2, Issue 1)
Pages: 8-12

In some machine code programs you may have disassembled you will have noticed the uses of the OR, AND, XOR, RES, BIT, etc. The uses of these machine code commands are explained in other machine code books so I will only touch on it slightly. In order to use these commands, you must be aware of the binary digits of each code (0 to 255). The last page of this newsletter will have the three most used counting features… decimal, hex, and binary.

Each binary code is made up of 8 digits and are counted from right to left with the first digit being 0. For example, the code for the letter “H” is 45. If you check the binary number for 45, you have: 0010 1101. BIT 0 is 1, etc. to BIT 7 is 0. If you change BIT 7 to a 1, the letter “H” is inversed. Check the new binary digit and you will see that the new binary number is 1010 1101 which is code 173. Checking in your manual, you will find that code 173 is inverse “H”. The ZX-81 uses this fact when printing the commands in Basic. Parts of the ROM are tables of the commands and when the subroutine in the ROM goes to print the command word, it checks BIT 7. If it is 0, it continues to print each letter until it finds the seventh BIT to be a 1 and stops printing the command word. For example, the Basic command word “PRINT” will have the first 4 letters in normal mode in the command table but the “T” will be inversed. The routine then stops printing the command word, changes the inverse to normal and returns the keyboard to the user. Therefore, if your are entering a machine code program that will contain tables, this is the fastest and most efficient use of machine code.

I am presently writing a machine code disassembler program that makes use of the above routine. For example, the mnemonic instruction for code 205 is “CALL”. The subroutine goes to the word table where ‘call’ is located and prints the word checking BIT 7 each time until it finds that it is a 1 then stops printing the word. The routine IS long and tedious (like any machine code routine) but it can do all this and fill the screen in less than 1 second since machine code is very, very fast.

Whenever I come across a machine code routine, I may find some parts of the routine interesting or useful in other routines. If you have Toni Bakers’s book on “MASTERING MACHINE CODE ON YOUR ZX81“, check the games out line by line and you will discover how to use the keyboard scan routine, or routine which I found useful in 2 different programs. One routine decreases a score to zero and another increases a score indefinately. Take out these routines and check the logic in them and how they are used. The following is how numbers can be increased by one which in machine code is very fast. Another useful feature of this program is how fast machine code cycle is. In about 1 second, it has checked, compared and printed about 1000 numbers. Try that in Basic!!!

First of all, I will give you the program in Basic then the machine code routine.

10 LET A=1
20 PRINT AT 10,14;A
30 LET A=A+1
40 GOTO 20

Now the machine code routine…. First, enter a REM statement with 35 characters (such as 35 X’s. You can enter more if you like but this is a waste of memory).

Now enter the machine code loader program. For those of you don’t have a loader program, the following is a machine code loader program:

1 REM XXXX....etc  (35 X's)
10 LET A=16514
20 INPUT B
30 POKE A,B
40 SCROLL
50 PRINT A;",";PEEK A
60 LET A=A+1
70 GOTO 20

RUN and ENTER the following bytes: (1 at a time)

42,12,64                    LD HL, (16396)   :display file
location
17,124,1 LD DE, 381 :screen centre
25 ADD HL,DE
126 CHECK LD A,(HL) :A contains the
character at
that location
254,0 CP 0 :is it blank?
32,2 JRNZ +2 :No. It contains
a number so jump
forward 2 bytes
62,28 LD A,28 :Yes. A now contains
the zero digit
60 DEC A :LET A=A-1
254,38 CP 38 :is A larger than
the code for
digit "9"?
32,5 JRNZ +5 :No. Jump ahead
5 bytes
54,28 LD (HL),28 :Yes. Print a
zero
43 DEC HL :move display
back 1 space
24,239 JR -17 :jump back 17
bytes to CHECK
119 LD (HL),A :print the digit
A contains
205,187,2 CALL 699 :keyboard scan
62,255 LD A,255
189 CP L :check for no
202,130,64 JP Z, 16514 key pressed
201 RET :if a key is
pressed, return
to Basic & stop.
Decimal / Hex / Binary conversion table, 0-131 (part 1)
Decimal / Hex / Binary conversion table, 0-131 (part 1)
Decimal / Hex / Binary conversion table, 132-255 (part 2)
Decimal / Hex / Binary conversion table, 132-255 (part 2)

Well, that’s it fellow club members. Please accept my apologies for any typing errors that may have crept in. If there are errors, try and work out the proper or expected commands. In any case, a correction will be made in the next Newsletter.

Since I have just become the Newseditor, I have tried to have articles for the brand new programmer to the more experienced one. If you have any questions about a programming problem, chances are that others may be having the same problem, so feel free to submit these questions and if possible, an answer will be found to benefit all members.

By now you will have noticed that the format and Newsletter has changed from the previous ones. This is because of a cost savings to the club. Also, since I am new at this type of job, I am learning and with a bit of luck, the Newsletter will get better. I have no idea what type of articles or information you would like to see in your Newsletter so please pass this information to me and I will try to have them in future Newsletters.

Your News Editor,
Stan Piotrowski

Direct all correspondance to one of the Executive officers or: P.O Box 7274, Stn. A
Toronto, Ont.,
M5W 1X9