The articles in this series are excerpts from the book, “Programming Arcade Games,” soon to be published by Reston Publishing of Reston, Virginia
In the second part of his series on programming arcade games, Bob Fraser discusses the programmer’s most powerful tool
USING your current knowledge of BASIC as a foundation, you are now ready to learn machine language programming. Using machine language is exciting and challenging, and your programs will have a speed unmatched in BASIC. Your arcade games will be faster, more exciting, more fun to play. First you must become familiar with these concepts:
1) Organization of computer memory 2) Storage of numbers 3) Computer terminology — for example, registers and addressing
This article explains these concepts. You will need this information to take full advantage of subsequent articles in this series.
Advantages of Machine Language Programming
A good arcade game requires fast, fluid movement. This is best accomplished using machine language because instructions are executed faster and more directly than in BASIC. A high execution speed allows the computer to do more in less time, which becomes extremely important in arcade games involving a lot of movement.
BASIC is slow executing because it is actually an intermediate language between humans and computers. We can easily translate our thoughts into BASIC; then the computer translates our thoughts into machine language using a monitor program consisting of many machine language routines stored permanently in the computer. The monitor program interprets and executes each BASIC statement.

For a simple instruction such as PRINT AT 5,4;”EXAMPLE” the computer must (1) “Read” the PRINT command and go to the machine language routine that handles printing; (2) “Read” the AT statement and call a subroutine that sets print positions; (3) “Read” the 5,4, print coordinates, verify their validity and then set the print position (if the coordinates are invalid, execution is terminated and an error message printed); (4) Move each letter of the word EXAMPLE to the screen, one at a time.

As you can see, the computer does a lot of work executing a BASIC statement. Machine language bypasses the translation steps and moves the word EXAMPLE directly to the screen, thus executing fewer machine language statements and therefore reducing the overall execution time. Executing more statements in less time, machine language is the serious programmer’s most important tool.
Three Number Systems
To use machine language instructions effectively, you should understand the decimal (base 10), binary (base 2), and hexadecimal (base 16) number systems. Decimal numbers, which you use every day, are formed through a set of ten digits. The binary and hexadecimal systems form numbers similarly but with a different number of digits. Figure 2 shows the digits used by each system.
Digits used by the three number systems.
Binary 0,1
Decimal 0,1,2,3,4,5,6,7,8,9
Hexadecimal 0,1,2,3,4,5,6,7,8,9,
A,B,C,D,E,F
Figure 2. The three number systems.
Very often in machine language programming, you will convert a number from one base to another. This is made easier using the table in Figure 3.
Counting to 15 in the three systems,
with conversion examples.
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Converting to decimal
1. Converting base 2 to decimal:
1111 = 1 x 2³ + 1 x 2² + 1 x 2¹
+ 1 x 2⁰
= 8 + 4 + 2 + 1
= 15
2. Converting base 16 to decimal:
FF = 15 x 16¹ + 15 x 16⁰
= 240 + 15
= 255
Converting binary to hexadecimal
Computers use the base 2 system (binary) for storing numbers in
memory. Any number from 0 to 255 is stored as a group of eight
binary digits. These binary digits can be converted to
hexadecimal quite easily using the chart.
11111111 = [1111] [1111]
= FF
Each group of four binary digits represent one hexadecimal digit.
1. 10100111 = [1010] [0111]
= A7
2. 01001111 = [0100] [1111]
= 4F
3. 10001010 = [1000] [1010]
= 8A
Figure 3. Conversions.
Bits and Bytes
Bits and bytes are terms specifying units of memory. A bit is the smallest unit of memory accessible to the programmer, and contains a binary digit, either 0 or 1. 1+ is called set when it equals 1, reset when it equals 0.
Bit Bit [0] = Reset [1] = Set
A byte is a group of eight bits. The number stored in a byte can range from 0 to 255.
Byte Hex Decimal
0000 0000 = 00 = 0
0000 0001 = 01 = 1
0000 0010 = 02 = 2
. . .
. . .
. . .
1111 1110 = FE = 254
1111 1111 = FF = 255
Organization of Computer Memory
The memory of the computer is divided into two main areas, ROM (read only memory), and RAM (read access memory). ROM is located first in memory. The start of ROM is at address location 0 and is 8192 bytes in length.
Memory +----------+ | RAM | +----------+ 8192 | ROM | +----------+ 0
ROM is a permanent area of memory, the location of all the monitor routines.
RAM is an area of memory free to the programmer, where the BASIC program, BASIC variables and display area are located.
ROM is permanent in memory because it contains all the monitor routines that allow the computer to function. This information is retained in the computer even when the computer is unplugged.
RAM is not permanent in memory, and its information is lost every time the power is turned off. There are four sections of RAM that are very important to machine language programming. They are shown here:
+----------+ | BASIC | | Variable | | Area | +----------+ ? | Display | | File | +----------+ ? | BASIC | | Program | | Area | +----------+ 16809 | System | | Variable | | Area | +----------+ 16384

The system variable area stores variables used by the monitor routines. The location of these variables in memory is always the same. Some of them supply useful information to the programmer such as the address location of the display file or the BASIC variable area.
The BASIC program area is the area of memory where the BASIC program is stored. Its starting address is always 16509, but its final address varies depending on the size of the BASIC program.
The character codes for each printable location on the screen are stored in the display file. The address location of the display file varies depending on the size of your BASIC program.
Space is reserved in the BASIC variable area for each variable used by your BASIC program. The address location of this area also depends on the size of your BASIC program.
Representation of Numbers
A byte of memory is made up of eight bits. Together, those eight bits can form a number from 0 to 255 decimal. For numerical purposes, such as adding and subtracting, the computer uses the leftmost bit to indicate the sign of the number.
0 = positive
1 = negative
BIT 7 6 5 4 3 2 1 0
------------------------
: : : : : : : :
: : : : : : : :
------------------------
With only seven bits representing a number, the largest positive number is 127 (64 + 32 + 16 + 8 + 4 + 2 + 1).
+127 = 7F = 0111 1111
Negative numbers are represented differently using notation called two’s complement. To form the two’s complement of a number, reverse all bits in the number — 0s become 1s, 1s become 0s — and add 1 to the number.
For example, -100 is changed to 2’s complement like this:
Binary: 0110 0100 Reverse bits: 1001 1011 Add one: 1001 1100
The range of numbers in a byte is shown here:
Decimal Hexadecimal Two's
Complement
+127 = 7F = 0111 1111
+126 = 7E = 0111 1110
+125 = 7D = 0111 1101
: : :
: : :
+2 = 02 = 0000 0010
+1 = 01 = 0000 0001
0 = 00 = 0000 0000
-1 = FF = 1111 1111
-2 = FE = 1111 1110
: : :
: : :
-125 = 83 = 1000 0011
-126 = 84 = 1000 0010
-127 = 85 = 1000 0001
-128 = 86 = 1000 0000
Range of numbers in a byte or
register:
+127 <--> -128
The example below shows how the computer adds two numbers.
0000 1010 (+10)
+ 0001 1000 (+24)
------------------------
= 0010 0010 (+34)
Registers
A register is a single byte location inside the computer. The T/S1000,ZX81 contains 24 registers, eight of which are frequently used in machine language instructions. These eight registers are lettered A to F.
A register holds a number from 0 to 255. Machine language instructions manipulate the data in these registers in various ways. Here are three examples of machine language instructions using the registers:
Instruction Explanation
LD A,4 — Load the A register with
4 (let A = 4)
LD B,A — Load the B register with
the contents of the A
register
INC A — Increment the value in
the A register by one
Some instructions use register pairs. A register pair can be used to store large numbers such as addresses. A number stored in a register pair can range from 0 to 65536.

Register Pairs
High Low
AF = | A | F |
BC = | B | C |
DE = | D | E |
HL = | H | L |
A register contains 16 bits of information. The rightmost eight bits comprise the low order register. The leftmost eight bits comprise the high order register.
Register Pair Example
Suppose the BC register pair contained the following information:
BC = [01000000 | 10000010]
The contents of BC can be converted to decimal and hexadecimal.
Decimal * Hexadecimal
BC * BC
/ * /
[64] [130] * [+40] [+82]
BC = 64 x 256 +
130 * BC = +4082
= 16514 *
The decimal and hexadecimal numbers are equivalent.
+4082 = 4 x 16³ + 0 x 16² + 8 x 16¹
+ 2 x 16⁰
= 16514
Getting Ready for Machine Language
In a previous example you saw the machine language instruction: LD A,4. This is actually a mnemonic for the instruction. Machine language is entered into the computer as a series of numbers. Two numbers are entered for this instruction.
Code Mnemonic
62 LD A,4
4
Writing a machine language program purely as numbers would be almost impossible to understand. For this reason, mnemonics are always written beside the code.
You have now covered the background material necessary for machine language programming. In the next chapter you will learn the entire machine language instruction set. You will soon be writing your arcade games in fast moving machine language.