This article appeared in the November/December 1982 issue of Today, CompuServe's magazine for CIS subscribers.
by James Davenport
The most popular programming language on all microcomputers is BASIC, which is what usually comes with the computer when you buy it. However, a computer cannot directly execute a BASIC program; it can only execute a set of predefined primitive instructions (machine code). The computer must translate the BASIC statements into the appropriate machine language instructions. This is usually done by a BASIC interpreter which looks at each BASIC keyword as it is encountered and executes the machine instructions necessary to perform the correct function. This can cause your program to run considerably slower than it would if it were written in machine code. It is often necessary to have a section of a program execute faster then BASIC can execute it. The solution is to have that portion of the program written in machine code to have it execute as fast as possible.
The problem then becomes how to write the machine language code. You could figure out exactly what it is that you need done and write the appropriate binary instructions and poke them into memory. This is very difficult to even think about doing and is almost impossible to do without making a mistake. To help alleviate this problem, the machine code instructions are given abbreviations called mnemonics. A program written in these mnemonics is called an assembly language program. However, the computer cannot directly execute assembly language either. The assembly language program must also first be translated into the correct binary values which the computer can execute. This process is called “assembling” the source code and is performed by an “assembler.”
There are several methods to produce machine code from assembly language source statements. Each method has its advantages and disadvantages. Three methods of assembling source code into machine code are described here.
You can go through by hand, looking up the values of the assembly language mnemonics in a table of machine codes and calculating address offsets. This is very tedious and usually leads to errors. Simple changes, such as adding another instruction in the middle, are very difficult to make. This method requires no investment except for your time and frustration.
Another method of translating assembly language into machine code is to buy an assembler for your microcomputer. This can range from a small investment ($30) to a several hundred dollar investment. The assembler usually comes with an editor of some sort to let you enter and edit assembly language source statements. For this reason it is sometimes called an Editor/Assembler. Assemblers have the advantage that you can assign names (labels) to memory locations or statements. Since the assembler calculates all address offsets when the program is assembled, using labels makes it easy to add another instruction in the middle of existing code.
One of the major disadvantages of using an assembler on your computer is that you are usually limited to the size of the program you can assemble. Since the assembler runs in your machine, you must have the assembler, your assembly language source program, and sometimes the resulting machine code in your computer's memory at the same time. This can severly limit the size of your program. It can also restrict where your program can reside in memory.
If you only want to write several small programs in assembly language, it may not be cost-effective to buy a powerful assembler.
Another method of assembling is to use a cross assembler. A cross assembler assembles machine code for a processor other than what it is running on. For example, a cross assembler might run on a Z-80 but translate an assembly language program written in 6502 mnemonics to 6502 machine code. The Z-80 could never execute the program it assembled. The object code would first have to be transferred to a 6502 before it could be executed. A cross assembler does NOT translate a Z80 assembly language program to machine code for a 6502; it runs on a processor which is different than the processor for which it is assembling machine code.
Using a cross assembler lets you use all of the facilities available on one computer to assemble machine code for another computer. This could include using storage facilities, editors, and printers which are not available on the target computer.
CompuServe has seven cross assemblers available to use. This means you can write and assemble assembly language source code for your microcomputer on CompuServe's mainframe computers (but you cannot run them). Cross assemblers are available for the RCA CDP1802, 650x, Motorola 6800, Motorola 6809, Intel 8080, National SC/MP, and the Zilog Z80 processors. These cross assemblers provide many features that are not found in most assemblers for microcomputers (conditional assembly, full macro capability, and universal file symbol control)
To write an assembly language program on CompuServe, you must first create a file containing the assembly language source code for your program using one of the editors (e.g. FILGE). For example, if you were going to write an assembly language program for the Z80 you might create the file TEST.Z80. You then run the corresponding cross assembler by typing the command:
You will then be prompted with:
Enter the name of your file, ‘TEST’.
The cross assembler will then assemble your program and report any errors it finds. After the cross assembler is finished, two additional files will have been created: ‘TEST.LST’ and ‘TEST.OBJ’. The file ‘TEST.LST’ is the listing of your program. It contains all of your source code along with the addresses and values for each instruction and a cross reference. To obtain a printed copy of the listing enter the command:
This will send a copy of the listing file to one of CompuServe's printers and then tell you how many pages were printed (you may have to first enable the line printer by typing the command ‘R PRINT’). The listing will then be mailed to you.
The file ‘TEST.OBJ’ contains the machine code in Intel hex format. Each line has the format:
where ‘dd...dd’ are the ‘nn’ data bytes to be stored starting at the address ‘aaaa’ and ‘cc’ is the checksum for the line. All numbers are hexadecimal. The sum of all bytes, including the checksum, will be zero. The high order byte of the address preceeds the low order byte in the address. The ‘00’ will always be zero.
You must then transfer the machine code file from CompuServe into your computer's memory. If you are running one of CompuServe's VIDTEX programs, you can load the machine code into your computer's memory using the LODHEX program. The LODHEX program uses an error-free protocol to prevent any noise on the communications line from causing an incorrect byte to be loaded into your computer's memory. To run LODHEX, enter the command:
Your computer will then be interrogated to see if it is running a machine language loader (such as VlDTEX). If it is not, LODHEX will exit and not attempt to load the machine code into your computer. If your computer is running a machine language loader, you will be prompted with:
File name to be loaded:
Enter the name of the file containing your machine code (e.g. ‘TEST.OBJ’). You will then be prompted with:
Address offset (HEX):
Enter the amount to offset the load address of the machine code. Eor example, if your program is supposed to load at 1200 (hex) and you wanted to load it at 1400 (hex), you would enter ‘0200’. If you do not want to offset your program, enter a blank line. The machine code will then be loaded into your computer's memory. LODHEX will tell you the lowest and highest address loaded and the total length of the program. You can then save the machine code to disk or tape for future use.
Some versions of VIDTEX allow you to execute your program also. If you are running a version of VIDTEX which supports this, you will then be prompted with:
Execution address (HEX):
Enter the hexadecimal address at which you want to start execution. If you do not want to start execution just enter a blank line.
The cross assemblers may not always be the best method of assembling assembly programs, but they are more powerful than most microcomputer assemblers and may be much less expensive. Complete documentation can be ordered on all of the cross assemblers and the VIDTEX program through the documentation ordering section of the FEEDBK program.
James Davenport is a software development supervisor at CompuServe Incorporated and a frequent contributor to TODAY magazine.