Disassembled BASIC – A Review

See all articles from SYNC v1 n2

Disassembled Basic, Linsac, 68 Barker Road, Middlebrough TS5 5ES, England. 48 pages, paperback.

Linsac has published a disassembled listing of the Sinclair Basic. What, you may ask, is a disassembly? Well, when machine language is placed in memory, either in RAM or ROM, it is stored as numbers. These numbers represent machine-language instructions, called “mneumonics” since they are abbreviated versions of the command. For example, an increment instruction is represent as INC. When a programmer enters a program using these or op codes, an assembler is used to turn the commands into numbers. A disassembler reverses the process, turning the numbers into lines of code so humans can understand it.

A few lines of disassembled code look like this:

07BE 2B       L07BE   DEC     HL             ;**INTERPRETER**
07BF 222640 LD (L4026),HL ;SAVE PROG ADDRESS
07C2 210000 LD HL,L0000 ;ZERO HL
07C5 00 NOP ; AND 'NEW' CODE
07C6 221540 LD (L4015),HL ;ZERO POINTER
07C9 211940 LD HL,L4019 ;POINT TO SYNTAX FLAGS
07CC CB6E BIT 5,(HL) ;TEST IMMEDIATE FLAG
07CE 2807 JR Z,L07D7 ;JUMP IF IMMEDIATE

The first column contains the memory locations of the code. In this example, the code starts at hex address 7BE. The next column shows the hex value or values for the command. Commands require from one to three bytes, depending on how much information is required. For instance, decrementing a register requires only one byte. Loading a single register with a number would require two bytes, one for the command and one for the number to be loaded. Loading a register with a value from a memory location requires three bytes, one for the load command and two for the value of the location. The third column is for labels. In this case, the labels used are just the locations of the commands prefaced by an L (labels must start with a letter, though they can contain numbers). There are two uses of labels. First, when assembling the code, you can refer to the label instead of the location. Thus, if you have a segment of code that multiplies two numbers, you could label it MULT. When you wanted to jump to that code, you could say JP (for jump) MULT. When the program is assembled, the correct value for MULT will be inserted.

The other use for labels is to make the code easier to read. When you are scanning a disassembly and see MULT, you know you have found the multiplication routine. Unfortunately, the Linsac disassembly doesn’t use this kind of label, making the listing harder to decipher. The label L07BE tells us that the line is at 7BE, but doesn’t give us any useful information.

The fourth column contains the mneumonics, followed by any values used by the commands. The final column contains comments, making up in part for the lack of meaningful labels. The comments help, but they are vague in places.

Stepping through the code, the first command decrements a pair of Z-80 registers called H and L. Next, the value in these registers is stored in memory. The HL registers are loaded again, this time with values from another area of memory. The NOP command stands for “no operation.” Possibly, this byte was left open for future changes in the code, or was created by an earlier change in the code. The comment isn’t clear. The next two commands again place the register values in memory and reload the registers. The BIT command tests a value stored in location 5 to see whether the command is from a program or from the immediate mode. If the command is immediate, the zero flag is set. The next line jumps to 7D7 if the zero condition is met. Otherwise, the program falls through to the next line (not listed here).

With a disassembled copy of Sinclair Basic in hand, an experienced programmer can work wonders. He can access any of the Basic functions through calls to the monitor, using the USR function. Even more powerful is the potential to use the Basic subroutines from machine language programs. There is no need to write your own PRINT subroutine when you can go through one that already exists in the monitor. In essence, the disassembly puts the full power of Basic at the programmer’s disposal, even when he is writing programs in machine language.

The listing comes fully commented, with asterisks marking the function of each subroutine, such as *LIST*, and *PRINT CHARACTER*. As mentioned earlier, there are places where the comments could be clearer. The printout is a Xeroxed copy of a dot-matrix listing, but it is legible. The listing, including a table of referenced lines, is 48 pages long.

Besides being a good tool for programmers, this book could also be used by someone who is learning Z-80 code. By seeing how the commands are used, and what the results are, you can gain an understanding of this machine language.