Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [137]
So far we've seen how CP/M (like most operating systems) provides commands and utilities that let you perform rudimentary housekeeping regarding files. We've also seen how CP/M loads program files into memory and executes them. An operating system also has a third major function.
A program running under CP/M often needs to write some output to the video display. Or the program might need to read something that you've typed on the keyboard. Or the program might need to read a file from the disk or to write a file to the disk. But in most cases, the CP/M program does not write its output directly into video display memory. Likewise, the CP/M program does not access the hardware of the keyboard to see what you've typed. And the CP/M program definitely does not access the disk drive hard-ware to read and write disk sectors.
Instead, a program running under CP/M makes use of a collection of subroutines built into CP/M for performing these common chores. These subroutines have been specifically designed so that programs can get easy access to all the hardware of the computer—including the video display, keyboard, and disk—without worrying programmers about how these peripherals are actually connected. Most important, a program running under CP/M doesn't need to know about disk sectors and tracks. That's CP/M's job. It can instead store whole files on the disk and later read them.
Providing a program with easy access to the hardware of the computer is the third major function of an operating system. The access that the operating system provides is called the application programming interface, or API.
A program running under CP/M uses the API by setting register C to a particular value (called the function value) and executing the instruction
CALL 5
For example, a program obtains the ASCII code of a key typed on the keyboard by executing
MVI C,01h
CALL 5
On return, accumulator A contains the ASCII code of the key that was pressed. Similarly,
MVI C,02h
CALL 5
writes the ASCII character in accumulator A to the video display at the cursor position and then increments the cursor.
If a program needs to create a file, it sets register pair DE to an area of memory that basically contains the name of the file. Then it executes the code:
MVI C,16h
CALL 5
In this case, the CALL 5 instruction causes CP/M to create an empty file on the disk. The program can then use other functions to write to the file and eventually close the file, which means it has finished using the file for now. The same program or another program can later open the file and read its contents.
What does CALL 5 actually do? The memory location at 0005h is set up by CP/M to contain a JMP (Jump) instruction, which jumps to a location in the Basic Disk Operating System (BDOS) of CP/M. This area contains a bunch of subroutines that execute each of the CP/M functions. The BDOS—as its name implies—is primarily responsible for maintaining the file system on the disk. Frequently, the BDOS has to make use of subroutines in the Basic Input/Output System (BIOS) of CP/M, which is the area that actually accesses the hardware of the keyboard, the video display, and the disk drives. In fact, the BIOS is the only section of CP/M that needs to know about the hardware of the computer. The CCP does everything it needs to do using BDOS functions, and so do the utilities that come with CP/M.
The API is a device-independent interface to the hardware of the computer. What this means is that a program written for CP/M doesn't need to know the actual mechanics of how the keyboard works on a particular machine, or how the video display works, or how to read and write disk sectors. It simply uses the CP/M functions to perform tasks that involve the keyboard, display, and disk. The bonus is that a CP/M program can run on many different