Now that you have your ‘feet a bit wet’ in machine code, let’s get on with just a few more commands. If we want to print a character to the screen, there are a few ways to do it. The simplest way is a one byte machine code command which prints any of the printable characters. If you look in the manual, you will see a few RST’s which are actually machine code subroutines built within the Z-80 microchip. The one mostly used is RST 16d (or RST 10h) which is ‘PRINT A CHARACTER ROUTINE’. (By the way, whenever you see the small letter ‘d’ or ‘h’, this simply means the number is ‘decimal’ or ‘hexadecimal’ or ‘hex’.) There is only one stipulation with using RST 16 and that is the character you wish printed must be contained in the ‘A’ register and it must be printable. The machine code byte for RST 16 is 215d or D7h. Therefore, you must first load the ‘A’ register with a printable character in code form. Checking your manual again, you will find that the printable characters run from code ‘0’ to code ’63’ and their inverse is from code 128 to code 191.
As you recall in our earlier Newsletter, you must load the machine code somewhere (above RAMTOP, the 8K area, or from a REM statement.) and the most convenient place is from a REM statement which is always at 16514. So enter a REM statement with a few characters (at least 4); then using your loader program (or by direct command since it is a tiny program), POKE the following numbers in:
POKE 16514,62 POKE 16515,38 POKE 16516,215 POKE 16517,201.
This is what it looks like in mnemonics:
LINE CODE MNEMONIC COMMENTS ----------------------------------- 1 62,38 LD A,38 load 'A' with the code for the letter 'A' 2 215 RST 16 Call the subroutine to print a character 3 201 RET return to Basic
Now RAND USR 16514 and the letter ‘A’ is printed on the screen. This may not seem like a big deal now but it’s the first step in machine code programming. Let’s expand the program a little to show you the real value and speed of machine code.
The above program is similar to the following Basic:
10 LET A$="A"
20 PRINT A$
30 STOP
Expanding the machine code routine…..
LINE CODE MNEMONIC COMMENTS ----------------------------------------------- 1 62,38 LD A,38 Load the 'A' register with the letter 'A' 2 215 RST 16 Call the subroutine to print a character 3 24,251 JR 251 Jump back 5 bytes (to start)
Now, RAND USR 16514, and immediately, the whole screen fills with the letter ‘A’. Try this with a few other character codes or their inverse. The above machine code is somewhat similar to adding line 30 to the above Basic line which is:
30 GOTO 10
There is still a faster printing routine but we’ll get into that some other time. In the meantime, I will explain the new command: ‘JR’. There really is no equivalent in Basic. In the above Basic line, we have what would be considered in machine code as ‘absolut addressing’. In other words, the program must GOTO a specific line and no others. The machine code equivalent would be a ‘JP’ (or Jump) command which would be Jump to a specific address. But the machine code command ‘JR’ means ‘Jump Relative’ to the address it is on to another address. In this case it was 5 bytes. Notice, no specific address is specified; only the number of bytes to jump. What this means is that by jumping relative, you save 1 machine code byte (since JP takes a total of 3 bytes), and most important is that the whole machine code program is re-locatable anywhere in memory. In the JR routine, no specific address is specified which means that it only jumps forward or backward, the required number of bytes. Threfore, you could load the above routine at 8192 then RAND USR 8192 and get the same results.
Again, you must learn a new rule or exception to the above ‘JR’. You are restricted as to how far you can jump relative; you can only jump ahead 128 bytes or back 127 bytes. If the jump is greater, then you must use a ‘JP’ (jump to absolute address) instead. Therefore, when the computer comes across the machine code command ’24’, the byte following tells the computer how far to jump and whether it is a forward or a backward jump. If the computer reads a number from 1 to 128, then the routine will jump ahead that many bytes; but if it sees a number from 129 to 255, then that means to jump backwards. But how do we determine the number of bytes backwards? Forward is simple enough since we just read it directly; for the backwards jump, subtract that number from 256 and the result is the number of bytes to jump back. Therefore, in the previous machine code routine, JR 251 means 256-251=5 which is jump back 5 machine code bytes.
There is something else to remember when figuring the number of bytes (this is usually the most troublesome area of machine code). Whenever determing the number of bytes forward or backwards, ALWAYS REMEMBER TO COUNT THE BYTE ITSELF. For example, if the computer reads JR 3, and the bytes in the program were: 10,16,24,3,1,18,22,64; it would jump to the address holding the byte value 64. Let’s go through this step by step because anytime you have trouble in machine code, always check the JR routines.
The computer reads the first 2 bytes then sees the byte 24. It next reads how many bytes to jump relative and the following byte is 3. Therefore, the computer has processed bytes 10,16,24 and no it is processing byte value #3. Jumping ahead 3 bytes, the first byte to jump is 1, the next byte is 18 and the 3rd byte to jump is 22. The computer then begins its machine code command execution at byte value 64 which is the 4th byte (3 bytes have been jumped). If you check back on our previous machine code tiny program, you should see how we arrived at jumping back 5 bytes. Don’t forget, the computer first reads the command for JR then the number of bytes which are two bytes processed before the actual command is executed.
Again, if we are outside of this range, then then you must use a JP instruction (or if you’re very careful, another JR instruction). The JR instruction uses only 2 bytes whereas the JP instruction uses 3 bytes. For example, in the previous machine code program, you could have used JP 16514 (jump to the address of 16514) which would be in machine code: 195,130,64. But you can readily see that if you decided to move this to the 8K area, then you would have to change the JP address which instead of 16514 would be 8192 which means more math calculations.
You now have 2 more new commands learned: JP and JR. But again there are duplications (of a sort). If you check your manual, the code 40,n which is JR z,n and code 202; JP z,nn. These mean if the arithmetic performed equals zero then execute a JR or a JP. There are other commands similar such as JR C or JP C (carry set-to be discussed), etc. With the 2 new commands, you have learned several new commands.
Arithmetic Performed = 0
The arithmetic used is a simple subtraction performed on the ‘A’ register and can be performed on the ‘A’ register only. If you check your manual, notice for the codes 184 through to 191…CP B, CP C, etc. (& code 254…CP n). But compare ‘B’ to what? You can only compare any of the registers or numbers to whatever value is contained in the ‘A’ register. So instead of writing: “CP B TO A”, since we can only compare to the ‘A’ register, we write: CP B and ‘A’ is understood. For example, if the ‘B’ register contained the value 10 and the ‘A’ register contained the value 10, when the computer comes across the command ‘CP B’, it will subtract the ‘B’ register from the ‘A’ register. NOTE: it doesn’t actually subtract the 2 registers. They will still contain the same values. It subtracts them ‘mentally’ if you like to determine the results.
After performing the subtraction, if the result is zero, then (in computer terminology) the zero flag is raised. If the computer came across the next instruction which might be JR Z, 10…it would jump ahead 10 bytes since the zero flag is set or raised.
We can compare any of the registers with ‘A’ or a number from ‘0 to 255’ with the ‘A’ register. This is a very important and widely used command on the computer. For example, if in a space invaders game you were allowed to be hit 3 times before GAME OVER, the computer could decrease the value by one each time you were hit and after the third time, jump to the routine (JP Z) to tell you the game is over.
The above raises a new and another important question; that is, the meaning of the FLAGS. The ones mostly used are:
C – CARRY NC – NO CARRY Z – ZERO NZ – NOT ZERO
There are more but you will very seldom use them and by the time you want to use them, there are books to explain them and you are well on your way in machine code almost to the expert level.
Back to the flag issue. If you recall, a byte is made up of 8 bits: e.g. 10101010. If the bit is a ‘1’ then that bit is said to be set. If the bit is a ‘0’, then the bit is said to be reset. Somewhere out in the far reaches of the computer, 1 byte (i.e. 8 bits), are set aside for the FLAGS. Each bit in this byte is reserved for 1 function: 1 bit is for the zero flag, another for the carry, etc. Since we cannot directly access the bits, it’s not worth mentioning which bit does what function. Since we only want to know whether a certain bit is set (flag raised), or reset (not zero for example), then we put in that machine code command and the computer checks the proper bit for us. Therefore, if the computer comes across the command: ‘JR Z’, it checks the Zero flag; if it is set (raised), then it will execute the JR command; if it is reset (flag not raised or bit is zero), then it does nothing with the command after checking the zero flag.
Even though some books show which bit represents which flag, that is of no concern to us since the computer does all the checking. But remember, 2 flags may be affected by the ‘CP’ command. For example, if the ‘B’ register contained 20 and the ‘A’ register contained 10 and by doing a comparison we subtract 20 from 10. Obviously this does not equal zero so the zero flag is lowered (bit=0 or zero flag reset). But since the computer deals with whole positive numbers only, we have what is called an overflow and the ‘CARRY’ bit is set (or raised). We must keep this in mind if we will be concerning ourselves later in a machine code program of checking both the ‘carry’ and the ‘zero’ flags.
The carry flag is simple to understand. We knoe that if we subtracted 10 from 10, we get zero or if we subtract 5 from 10, we get 5. Notice that they are all in the range from ‘0-255’ and are positive numbers. Therefore, if we subtract 20 from 10, in reality we get minus 10 (-10) or negative 10. We kno that the computer deals in positive whole numbers only so that the only way of passing this information on to us is by the computer setting the ‘CARRY’ bit (or raising the ‘carry’ flag). The same thing would happen if the ‘A’ register contained the value 255 and it came across the instruction: ‘ADD A,10’. Again, we would have an ‘overflow’ (numbers gone past the ‘0-255’ range). There actually is no overflow as such. It simply reverts back to zero and starts counting again. But we need some way of knowing that the number is greater than the value capable of being held by the register. This is done by setting the ‘carry flag’. Therefore ‘A’ would now contain the value ‘9’. Remember, ‘0’ is a number too.
This can be useful in general addition. Don’t forget, all calculators are like computers. The arithmetic to be performed are simply a set of machine code instructions. For example, if we were to add 2 numbers that go beyond the 255 range, then the first register will contain the new number and we can ‘INC’ the second register by ‘1’ if ‘carry is set’.
If you have understood this article, you will have learned 3 new commands (which again are duplicated in a slightly different form). These commands and a complete understanding of the flags are very important in machine code programming since they are used quite extensively. On page 59 of ‘Toni Bakers‘ book (MASTERING MACHINE CODE ON YOUR ZX-81), she shows a routine on how to print a message to the screen. Go through the routine and disregarding her unsual commands such as ‘CALL SPRINT’, check the machine code instructions on what she is doing. This particluar routine has been used by me on several different machine code programs…even for a game since you can print the whole graphic game in machine code using her routine before the actual playing of the game. (This of course will be explained further in a future article.) To understand any of her routines or your own, do the same thing I do; read the machine code instruction and using a scratch pad, execute each instruction as the computer would and write down the results. For example, if you LD A, 10, then write down A=10. The next instruction might be INC A. Cross out 10 and write 11. This is the only we I have found to check why a particluar machine code routine wouldn’t work and finding my errors since in machine code, we have no SYNTAX checking.
That’s it for now so until next time, have fun programming in machine code. Remember, it may take 10 times longer thanBasic to program in machine code but the program will work hundreds of times faster. Very impressive.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.