Machine Code Programming (part 2)

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

Beginning with this Newsletter, we will learn Machine Code programming from scratch and continue in the following Newsletters ending with a small “Shoot-Em-Up” type Space Invaders game with full explanations so that you will be able to make your own Machine Code programs such as games.

First of all we ask the question, Why Machine Code? If by now you don’t know, Machine Code can increase the speed, the execution, literally up to about 1000 faster. If you have ever programmed an “X” or any other character to move across the screen in Basic, you can see how agonizingly slow it is. In Machine Code, you have to actually slow the movement down by as much as 10,000 times … notice ten thousand because machine code is so fast.

This, and the following articles, assume you have some knowledge of Basic since we will be doing some comparisons to help you in understanding what is going on.

The lowest form of a computer language is the Binary system. We really don’t have access to this form but the next level which is using the decimal or hexadecimal system but we stillhave to understand the Binary system in order to understand smoe machine code processes. In the decimal system is it composed of 10’s and increment of tens. For example, if we begin counting, we start with 0, 1, 2, etc. and when we reach 9, the ones column revert to zero and the tens column contains a one. (10). The same again happens when we reach the number 99. The ones and tens columns revert to zeroes and the hundreds column now contains a one. The binary system is much different. It has only two states or can only hold 2 numbers in each column instead of ten. That is because the microprocessor (the Z80 in the ZX-81), the Timex 1000 or any of the other microprocessors in all computers) has only 2 states – on or off or high/low voltages. As you can see the simplest form of the computer down to the bare essentials is on/off. This means that every household actually contains a very primitive form of a computer (although it may seem ridiculous) is your switch to turn on the kitchen lights, for example (on/off). But a computer has millions of these tiny switches (electronic signals in actual fact) in a chip ½ x 2 inches. Therefore, to count we begin with 0 (as in the decimal system), then 1 and that’s all the ones column can count to so as in the decimal system, the next column which was a zero now becomes a one and the ones column reverts to zero. We have thus counted to 2 which looks like this in binary: 0000 0010. To count to three, the binary number becomes: 0000 0011 which now has a 1 in the ones column. To continue to 4, the ones columns have counted as high as they can in binary so the 3rd row is increased by 1 and the first 2 rows revert back to zero (again as the same condition exists for the decimal system). The number 4 in binary then looks like this: 0000 0100 and so on. If you notice, we have 8 rows or columns. Each column is called a “BIT” and 4 bits make a “NIBBLE”. 8 bits or 2 nibbles makes one byte (which comes from Binary Digit). The Z80 is such that 8 bits can travel in parallel to and from the microprocessor so our system is using an “8-bit microprocessor”.

Some home computers have “16-bit” microprocessors. What does this mean? Speed!!! The microprocessor or CPU can receive and send data 16 bits at a time. To explain this, imagine an 8 lane highway and we have gravel trucks containing gravel (computer data). If we send them down the highway in single file, there would be a considerable time interval between the first truck arriving at its destination and the last truck. Imagine, now, if we had a 16 lane highway; there would be a considerable increase in time differences or speed in which the data arrived. Now you can see why we have ribbon cables coming from computers. Besides the signal to turn a device such as a disk drive, on and off, it also is capable of sending and receiving data 8 bits at a time (or more in a 16-bit system) – remember the 8 lane highway). If you “turn on” all the bits (make them all ones) you will see they add up to 255 (check the last Newsletter for the Binary-Hex-Decimal printout). Therefore, each byte can only hold any decimal value from 0 to 255 (or 256 numbers – remember zero is a number too).

Always remember when looking at a binary number, we read it the same as a decimal number – from right to left, e.g. 239 has 9 ones, 3 tens and 2 hundreds.

0000 1010 has no ones, 1 two, no fours and one 8 so this number is 2+8 or 10 decimal.

There, that wasn’t too complicated. Understanding this will actually make the rest easier to understand.

We use the decimal system or hexadecimal system to represent these binary numbers since they can become unwieldy. Can you imagine trying to read:

0100 1010
1101 0001
etc.

We must be able to use, manipulate and change these values in a systematic or logical order and able to locate them or place them anywhere we want (within reason) so we have what are called “addresses”. Each address (beginning with zero) is capable of holding 1 byte (or any one decimal number from 0 to 255). 1024 of these bytes together make up 1K of memory (in actual fact, 1024 addresses means 1K). Therefore, 16K has actually 16383 locations or addresses. If you multiply 16 x 1024 your answer will be 16384 but don’t forget, zero is the first address so from 0 to 16383 are 16,384 addresses.

Each and every computer must be able to communicate with the outside world such as checking if a key is being pressed or the letter “A” is printed on the TV screen. This operating system is called the “ROM” or Read Only Memory. We can use it but cannot change the contents so the letter “A” will always look the same, etc. In the ZX-81 the ROM uses for itself, addresses 0 to 8191. There are many types of ROM’s such as COBOL or FORTRAN but the ZX-81 has the language BASIC built into it. What Basic is, or any other computer language, is an elaborate machine code process. That is the reason that Basic is so slow as compared to machine code. First it takes the data, checks it for Syntax (Rules of the ROM) then checks to see what each of the commands means in machine code, translates it to machine code, executes the command, translates it back to Basic and displays the results (if required). Again, let’s use another analogy. Suppose you bought a Stereo Component set in kit form BUT all the instructions are in Chinese. You would translate each instruction, act on it, then continue on to the next instruction. But what if it referred you back to a previous instruction? You would have to translate it again and act on it. This is like Basic. Time consuming but it works.

A much better way would be to sit down and translate the whole thing then build it. This is the same as Compiler programs that convert Basic program to Machine Code. A much, much faster way. But what if you had built 100,000 sets? You could build the next one without even looking at the instructions. This is machine code. No referring to any routines or instructions but just execute each command in turn or each step, as it came along.

You can see that the ZX-81 is actually a 24K computer (if we follow the advertisements as with other computers). We have an 8K ROM and 16K RAM pack which adds up to 24K. To find out why the 8-Bit computers can only access up to 64K we return again to the binary numbers. This point is now important since we will be using this concept in a lot of our machine code routines.

As pointed out earlier, 1 byte can only hold a decimal number of 255 so the microchip inventors made it such that under certain conditions (to be discussed at length later), it can use a combination of 2 bytes following each other. For example, under a certain condition the computer is told that a number larger than 255 is required so it takes the first byte it receives then waits for the next byte. The next byte it receives is multiplied by 256 then added to the first byte (remember 0-255 are 256 numbers). For example, if the first byte the computer receives is 10 and the second byte it receives is 5, the resulting number is:

10 + (5 * 256) = 1290

Now taking this step to the end of possibilities: the first number is 255 and the second number is 255;

255 + (255 * 256) = 65535

Therefore from 0 to 65535 are 65536 addresses. Since 1K is 1024, we divide: 65536/1024 is 64 so an 8-bit CPU can access up to 64K of memory. This sounds like a lot of memory for you to use but the user does not really have access to all the memory. In one particular computer, the ROM takes up 12K, the Disk Operating System (DOS) takes up a further 10K, hi-res graphics, printers and other devices take up more memory leaving the user with 35K of memory for Basic or machine code.

In the ZX-81, we are even less fortunate with having a 64K RAM pack if you are thinking that you have more memory for your Basic program. The ZX-81 is set up that we can still only use 16K for Basic but part of the 64K RAM pack is used for the systems variables and your variables so that you do have more memory to use, the remainder of the 64K can only be accessed by using machine code. Likewise if you have the Hunter board that occupies the memory locations between 8192 and 16383. When you buy the ZX-81 with a 16K RAM pack the ROM occupies memory locations 0 to 8191 and the RAM pack accesses memory locations 16384 to 32768 which leaves the area blank used by the Hunter board (which is an extra 8K) but can only be used by machine code.

Further checking your manual, you will find that the ZX-81 uses some of your RAM for itself which it calls the “System Variables” (from address 16384 to 16513). The computer has to know, for example, how many lines are presently being used on the screen so that it knows when the TV screen is filled to the end with data or how much memory you have available for Basic. Some of these variables can be changed to suit your needs without causing the ZX-81 to “crash” but others cannot be manipulated. For example, if you check the system variables, you will find that addresses 16388 and 16389 hold the location of RAMTOP. Now that you understand how these 2 addresses can hold a number beyond 255, you can PEEK these locations. You will find (for 16K) that 16388 is 0 (PRINT PEEK 16388) and 16389 is 128. Therefore, 0 + (128 * 256) = 32768. If you go back and check our previous discussion, you will find that the 16K RAM pack uses addresses 16384 to 32768 … there it is, the end of RAM or RAMTOP. Now, by POKEing various numbers into these 2 addresses we can fool the computer into thinking it has less memory (lowering RAMTOP), we can make use of the area above RAMTOP for machine code routines and NEW or CLEAR does not alter the code above RAMTOP because it checks 16388 and 16389 to see how much memory is available. It then clears only this area. After the above lengthy discussion, you should be able to check and see how much memory the programmer wants to use by POKEing various numbers to “Lower RAMPTOP”.

Now we get to the address that is the first address the programmer can use, whether for machine code or Basic and that is 16514. If you recall your Basic you know that by placing a REM statement anywhere in your program is a REMark statement that the computer does not act on or do anything with it. It just tells the Basic programmer what is going on. For example, 10 REM THIS IS MY PROGRAM WRITTEN BY, etc. Carrying this concept one step further, that line does occupy memory addresses, one for each character. If you add the following program lines you will find it as shown;

10 REM  THIS IS MY PROGRAM
20 FOR I=16514 TO 16531
30 PRINT I;","; PEEK I, CHR$ PEEK I
40 NEXT I
50 STOP

After runnning this program, you will find that the REM statement uses 18 address, or memory locations or bytes. So if we continue another step, we can put any decimal number in those addresses after the REM and it won’t make any difference to the Basic program. So we can, therefore, use these addresses for our machine code routines. Without a REM statement, we cannot arbitrarily POKE numbers into the addresses without causing a “crash” so by placing a REM statement at the start of the Basic area, namely 16514, we can place any number of characters after it to save the addresses to be used for our machine code routines. We can enter any character we want such as: 123456 or a number of X’s so long as there are as many as or more than the number of bytes in the machine code routine.

Using a REM gives you the capabilities to SAVE the routine without any fuss whereas above RAMTOP it is much more difficult. But what if you needed a hundred or a couple of thousand bytes reserved; that’s a lot of extra typing. Fortunately, we follow that computer axiom, “LET THE COMPUTER DO THE WORK”. The following is a Machine Code routine that prints the line number, the word “REM” and the number of X’s chosen by the user. Notice that most machine code routines are the first line in any Basic program. That is so that we don’t have to calculate the address to be used by the machine code routine since we know that the first address is always 16514.

First we must lower RAMTOP: POKE 16388,186 POKE 16389,127 NEW, NEWLINE. In order to enter any machine code, we must turn back to Basic to “load” the machine code so enter the following lines (called a “loader program”):

10  SCROLL
20 PRINT "ENTER STARTING ADDRESS"
30 INPUT X
40 SCROLL
50 SCROLL
60 PRINT "ENTER YOUR MACHINE CODE"
70 SCROLL
80 SCROLL
90 INPUT Y
100 POKE X,Y
110 PRINT X;"'"; PEEK X
120 LET X=X+1
130 GOTO 80

REM Generator Machine Code:

RUN the program and enter the address: 32699

Enter the following bytes (not the commas):

237, 75, 66, 64, 205, 231, 2, 197, 3, 3, 197, 1, 6, 0, 42, 41, 64, 9, 34, 41,
64, 33, 125, 64, 205, 158, 9, 35, 112, 35, 112, 35, 193, 113, 35, 112, 35, 54,
234, 35, 54, 118, 193, 197, 205, 158, 9, 193, 197, 35, 54, 61, 11, 120, 177, 32,
248, 193, 42, 41, 64, 9, 34, 41, 64, 195, 7, 2

NEW your “loader” program since it has done its job then enter the following Basic lines:

10  PRINT at 10,0; "HOW MANY BYTES FOR THE REM STATEMENT?"
20 INPUT X
30 POKE 32711, X- INT (X/256) * 256
40 POKE 32712, INT (X/256)
50 RAND USR 32699
60 CLS
70 LIST 0

Well, that’s it for this Newsletter about machine code. Hopefully you have understood the basic concepts since it will give you a better understanding of the articles to follow.

As I’ve said before, please let me know what kind of articles you wish to see in your Newsletter. I am trying to give you a little of the various aspects of the ZX-81 to help you in programming a computer, especially the ZX-81.