Machine Code Programming (part 3)

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

Before we can actually get involved with machine code, you must learn some of the commands that go with it. In the ZX-81 Basic language, we have roughly 45 commands; in the Z80 microprocessor, there are over 700!!! But, not only will we normally use less than a quarter of them, about three-quarters of the commands are duplications. So learning one command means you have automatically learned anywhere from 3 to 20 or 30 commands. So don’t fret. It’s really not that awful hard but you must think logically all the time and there are no Syntax checking capabilities.

In Basic, we have a powerful feature and that is we can assign anything to variables, numbers or characters: e.g. LET A = 10. That’s simple enough but let’s see how much memory we use; first the line number takes 4 bytes, LET takes another byte, 2 more bytes for ‘A’ and ‘=’. Because the ZX-81 is floating point arithmetic basic, the ‘1’ in 10 takes 5 bytes and the ‘0’ takes 1 byte. That makes a total of 13 bytes. Add to that the computer must go through the interpreter to check for Syntax and convert to machine code, then converts back again to Basic through the interpreter, it’s no wonder that Basic is a lot slower (it seemed fast when you first got your computer) and is more wasteful of memory. The nice thing in Basic is that we can use any of the alpha characters or combinations, to assign the variables to. Alas, in machine code we are very restricted; we can use only 7 variables! They are A,B,C,D,E,H,L. (We also have the ‘F’ for the flags (to be explained) but we cannot use it for our variables.) With this restriction it seems next to impossible if we need to assign more than 7 variables. But, what we can do is Store that contents of that variable in some other part of memory and get it whenever we need it. That gives us a theoretical more than 16000 variables! We would never need to do this but you can see we have the capabilities.

Let’s deal with the ‘A’ register (in machine code we call them registers instead of variables). The ‘A’ register is commonly called the ‘ACCUMULATOR’ since we can perform a lot of arithmetical manipulations with it whereas on the other register we don’t have that capability; such as adding another number to one already existing in the ‘A’ register. We can’t do that with the other registers. Although it seems we will be doing a lot of extra movements in comparison to Basic, machine code is very, very fast. For example, it can do roughly 10000 manipulations in less than a second! I think that will be fast enough for our needs.

We can, therefore, assign a number to the ‘A’ register the same as we did in the Basic variable ‘A’.

LINE   HEX   DECIMAL   MNEMONIC

1 3E 62 LD A,n

In the above, the line number is used only to draw your attention to that line and is not ever used in machine code. (See the previous Newsletter about addresses are to machine code as line numbers are to Basic). Check in your manual and you will see the code for 62d or 3Eh is LD A,n. When you enter the decimal number 62, it means what it shows in the mnemonics (or opcodes) that we are assigning a number the variable register ‘A’ and the following number will be the assigned number. For example: 62, 10; means LD A, 10 or LOAD THE A REGISTER WITH NUMBER 10. (In basic, LET A=10). No machine code programmer can memorize all the machine code so you must keep checking the manual; although after some usage, will will memorize through habit, some of them.

The ‘A’ register now contains the numerical value of 10. If there were any other values assigned to ‘A’, they have disappeared. The same is true for the remaining registers. If you check the manual, you will see that the code for: LD B,n is 6. You can check the others yourself. If you read the last Newsletter, you will know that the highest number that can be assigned to one register is 255! Therefore, the Z80 chip have other codes that combine 2 registers together. They are in this form only: BC, DE, HL, (& AF). There are no other combinations.

These registers combined, behave in a different manner. To explain it easier we will use the HL register. With the registers combined, we can assign numbers up to 65535. Each register (H & L) can still hold only numbers up to 255 but the computer recognizes it differently. Let’s say we want to assign the number 10000 to the HL register. We must break it down so that neither register is holding a number larger than 255. (remember 0-255 are 256 numbers). Find the INT (10000/256) which is 39. Next multiply 39 * 256 which equals 9984. Subtract 10000-9984 which equals 16. We can then assign the ‘high’ byte (39) and the ‘low’ byte (16). Therefore, we would write the code in mnemonics as: LD HL, 10000. Checking the the machine code for this we find: 33 (decimal) is LD HL,nn. There is one more thing to complicated matters with combined registers. The computer must have the low byte first and the high byte second. Therefore, in actual fact, it is stored as ‘L’ first & ‘H’ second. We would therefore enter the bytes as: 33,16,39. The HL register now holds the value 10000. The same, again, holds true for all the other registers. You can see for the example, ‘H’ is high and ‘L’ is the low byte. Therefore, the BC register has the ‘B’ as the high byte and the ‘C’ as the low byte. DE is the same.

Enter a REM statement of 3 bytes and using your ‘loader program’ from the last Newsletter enter the address: 16514. Now enter 33 16 39. If you PEEK 16514, PEEK 16515, PEEK 16516, you will see they now contain your 3 bytes. (We will see in another Newsletter how to use the rgisters). Continuing on, check your manual and you will see we can add the contents of the registers together (e.g. code 129 is ADD A,C). You can see that we can also subtract registers (e.g. code 152 is SBC A,B) SBC is ‘subtract with carry’ (to be explained at another time). You read it as: SUBTRACT WITH CARRY, THE REGISTER ‘B’ FROM THE REGISTER ‘A’. You will notice the codes from 144 to 151 has no other register after it. This is because that you can only subtract one register from the ‘A’ register and no others so ‘A’ is understood.

If you also check the other codes such as 66 decimal, it means LD B,D or load B with the value of the contents of the D register. One important thing to remember is that if you assigned a number such as 10000 to the HL register and later LD H with another value, you will have changed the contenst of the HL register.

I tried to make the above as clear as possible. You may find it clearer when we actually get to use them.

If you look back on what we’ve learned and check the manual for the various codes, you will see that by learning the above few machine codes, you will now know over 100 machine code commands!!!

There are a few more commands that are not that difficult to learn. If you look at the decimal code ’12’, you will see ‘INC’ which means to increase that register by a value of 1. If the value held by that register is 10 and the machine code routine comes across the instruction to ‘INC’, it will then increase that value to 11. Don’t forget that the registers can hold only numbers up to 255 so if the register is already 255, then an INC command will make the value revert to a ‘0’. Note also the other command of ‘DEC’. This is the same like INC except it means to decrease the value of that register by 1.

Well that’s it for now. In the next newsletter I will show you how to use the above machine code commands in a small program.

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