Chapter 27. Coding: Assembly Language
Chapter 27 is about writing computer programs, an activity variously known as software engineering, programming, or coding.
Two types of coding are described in this chapter: Assembly language programming based on the the Intel 8080 processor on this page, and on another page, high-level programming using JavaScript.
8080 Assembly Language Programming
This is an emulation of an Intel 8080 microprocessor written in JavaScript. It has four main components:
- An Editor to enter, edit, and save assembly language programs.
- An annotated block of Memory used to show the assembled machine code.
- The CPU emulator that allows you to run the program or step through instructions.
- A Console to display program output and read keyboard input
These components are described further down this page.
Editor - Untitled
Memory
CPU
PC = | 0000h | ||
SP = | 0000h | ||
A | 00h | 00h | Flags |
B | 00h | 00h | C |
D | 00h | 00h | E |
H | 00h | 00h | L |
Flags |
Console
The following sections describe the various components of this emulator.
Editor
The Editor is a stripped down version of a multiline editor application such as the Windows Notepad or the macOS TextEdit. It is here that you can create assembly language programs that are compatible with the Intel 8080 Assembly Language. This language is case insensitive: You can type your programs in uppercase or lowercase or a mix.
The File menu has options to clear the contents of the Editor to begin a new file, open an existing file, and save a file. These menu options invoke familiar dialog boxes.
This simulated file system uses a feature available through JavaScript known as localStorage. The files that you store are private to this particular webpage and to the particular browser you're using. In other words, if you save files while running this page on one browser, you won't be able to see them when using another browser. You can transfer these files between browsers, or save these files elsewhere by copying them from the Editor to the clipboard.
In addition to opening and saving entire files, you can save a snippet of a file by selecting a block of text and choosing Save selection from the menu. You can insert a snippet into your existing file and choosing Insert file from the menu. This features are intended to save and retrieve handy subroutines.
WARNING: Although this editor uses typical message box warnings to help you avoid writing over an existing file, or erasing a file before it's been saved, it does NOT warn you about losing your edits if you refresh or close the webpage.
Several files are pre-stored for you. These include all the assembly language programs in the book, as well as a couple others. These files are described in the File Open dialog box. They are read-only files. You can modify them in the Editor but you cannot save them unless you choose the Save As option to specify a different filename.
The allowable syntax for your programs is generally that described by the Intel 8080 Assembly Language Programming Manual published in 1975 and available on the Internet Archive with some stipulations and exceptions:
- The IN, OUT, EI, and DI instructions are not supported because this emulation has no input or output ports, and no interrupts to enable or disable.
- Instructions that reference register pairs can use either B, D, and H as the Manual indicates, or BC, DE, and HL for purposes of clarity.
- The use of $ to represent the current Program Counter (page 10) is not supported. The use of numbers to represent registers (page 10) is not supported.
- The ORG, END, EQU, DB, DW, and DS directives are supported but not SET. Conditional assembly and macros are not supported.
Notice that hexadecimal numbers that begin with a letter must be preceded with a zero. Otherwise, the assembler could mistake them for labels.
Amoung the available programs is one called Test Suite, which is copyright 1980 and intended to test emulated 8080 processors. This program assembles on runs on the emulator here and was particularly useful for testing it.
If the program contains an ORG statement that causes the assembled program to
begin execution at the address 100h, then five CP/M calls are supported
through the CALL 5
interface:
-
End Execution: Set register C to 0 and
CALL 5
. -
Console Input: Set register C to 1 and
CALL 5
. On return, both the A and L registers contain a character typed in the Console window. - Console Output: Set register C to 2 and register E to an ASCII character. The character will appear in the Console window.
- Output String: Set register C to 9 and the DE register pair to the address of a string of characters terminated by the dollar sign($). The characters up to (but not including) the dollar sign are display in the Console window.
- Buffered Console Input: Set register C to 10 (hexadecimal 0Ah) and the register pair DE to an area of memory, the first byte of which is the length of the buffer. On return, the buffer will contain characters typed at the keyboard up to that length, and the first byte will be the actual length.
The Test CP/M Console Calls program included among the files demonstrates all these function calls.
A CP/M program can terminate in several ways: By a CALL 5
with register C set to zero;
by JMP or CALL to address 0000h; or by a RST 0. When a CP/M program begins execution, the
address 0000h has been pushed on the stack, so it can also terminate with a RET instruction.
Memory
After you've opened or typed in a progam you can press the Assemble button. Any errors in your code are displayed in red under that button.
If the file is successfull assembled, the addresses and machine code are displayed in the Memory box. For convenience, the corresponding instructions are displayed at the right.
This display is helpful when executing the code: Lines of code in the Memory display are highlighted as they are being executed.
CPU
This table displays the state of the CPU, which consists of the Program Counter (PC), the Stack Pointer (SP), the registers A, B, C, D, E, H, L, and the flags.
Four buttons are available:
- Step: Executes the next statement in the program.
- Run: Executes the program continuously at the rate of 100 instructions a second * until the program halts itself.
- Break: Only enabled when the program is running continuously. Stops execution.
- Reset: Resets the Program Counter and Stack Pointer to their initial states. If you want to restore the contents of memory as well, press the Assemble button.
* The original Intel 8080 ran at 2 Mhz. The various instructions (including fetching the instruction from memory) required a minimum of 4 clock cycles and as many as 11. The instuctions executed at a rate of approximately 200,000 to 500,000 a second.
Console
The Console displays output from your programs and allows you to type input to your programs. It should receive input focus when a CP/M call has been made to receive keyboard input, but if not, simply click it and then type. It can be cleared with the Clear button.