Volume C1 · Basic concepts · TAOCP Section 1.3
Why a machine language
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.3 of the book.
Contents
Why a machine language
Overview
An algorithm in the book is first written in English and in numbered steps, as in Section 1.1. A step such as "set to the larger of and " has no cost until it is turned into instructions of a computer. Section 1.3 opens with a short statement that the book turns its main algorithms into the instructions of one made-up machine. The reasons for this choice are argued mainly in the preface of the volume, and they can be put in three points. The count of the steps of a program is a measure of its time, so it must be exact. A high-level language hides the steps, so it cannot give the count. A real machine changes within a few years, so a book that lasts needs a machine that does not change.
The section has no numbered item and no statement to prove, so this chapter has no Node line. It uses nothing before it, and Sections 1.3.1 and 1.3.2 build on it.
The ideas
What the cost of an algorithm means. The time of a program is the sum, over the instructions that run, of the time of each instruction. To write this sum, a reader must know three things: which instructions the program has, how many times each runs, and the time of one run of each. The second item is the work of the mathematics of Section 1.2. The first and the third are the work of a machine language. A program in a language of a higher level has none of the two: the compiler picks the instructions, and the result differs between compilers and between machine types. For the time of an algorithm to be a fact of the algorithm and not of the compiler, the program has to be at the level of single instructions.
Why the machine is invented. There are two options: take a real machine, or define a new one. A real machine has a short life, and its manual is long and full of special cases that have no use for the study of algorithms. The invented machine MIX is chosen to have the features that all common machines of the time have, and none of their odd features. It has one accumulator and one extension register, six index registers, a jump register, a store of words that are addressed by numbers, a sign and a comparison indicator, and a set of devices for input and output. The reader who knows MIX can read a program for another machine with a short effort, because the ideas are the same.
Why a word of a MIX is unusual. A word has five bytes and a sign. The size of a byte is not fixed: any size from to values is allowed, and a program that is correct must give the right result for all of them. This one rule makes the reader write programs that do not use the accident of the size, as a program for a family of real machines must. It is also the reason why MIX can be a binary machine and a decimal machine in one definition. The rule is stated in Section 1.3.1.
Why an assembly language. Instructions in a machine are numbers. To write numbers by hand is a task that is slow and that gives errors, so a language is defined in which an instruction has a name, a place has a name, and the numbers are filled in by a program called an assembler. Section 1.3.2 defines the language MIXAL. A program in MIXAL is the same as the program in numbers, line by line. The assembler replaces names by numbers, and the only words that it adds are data words for the literal constants and for the symbols that are never defined. So the time of a MIXAL program is exact, and the program can be run on a simulator to test it.
The cost of the choice. A reader who wants to write programs for a real machine will have to learn another language, and the details of a real machine, such as the size of a cache or the time of a branch, are not in MIX. The loss is accepted because the first aim is to count the steps of an algorithm, and this skill carries over to other machines. The time model of MIX is simple and its instructions are few, which is a gain for a first reading.
How to study the two sections. Section 1.3.1 is a reference and a definition. A first reading takes the words, the registers, the format of an instruction and the instruction groups, and leaves the details of each instruction for the time of use. The reader should run a few instructions by hand, with the contents of the registers written down after each. Section 1.3.2 is the first place where programs appear: a small program for the largest of numbers, then a program for a table of prime numbers. These two should be traced on a small input by hand, since the later chapters expect the reader to count the steps of a program of this kind without help. The chapters of C1 for 1.3.1 and 1.3.2 give a trace of each program, and each count in them was checked by a run of the program.
A small check of the claim about cost. Take the program of Section 1.3.2 for the largest of numbers. The listing of TAOCP takes units, where is the number of times the maximum changes during the scan. For and the input read from the end, the scan sees , then , then . The maximum changes once, to , so and the time is units. For the input the scan sees , then , then , so and the time is units. The listing of the C1 chapter for Section 1.3.2 takes units, which gives and units for the same two inputs. The time of one algorithm is not a single number: it depends on the input, and the study of this dependence is the analysis of an algorithm.
Notes and history
A book on algorithms that fixes one machine makes the time of a program a fact that a reader can recompute. MIX is a design for this purpose, not a copy of one real machine.
The idea that a book should state the machine, so that the time of a program can be read from it, is the one that the later chapters rely on. For a reader who has studied a language with a virtual machine, the closest parallel is the instruction set of that machine: the count of the instructions that run is a model of the time, and its use is the same.
Section 1.3 opens the part of Chapter 1 that is about programs. It explains why the book writes its programs for an invented computer and its assembly language, and it introduces the two sections that define them: Section 1.3.1 for the machine MIX and Section 1.3.2 for its assembly language MIXAL. This chapter gives the reasons, the limits of the choice, and a way to study the two sections.
The source of this page. Something is wrong? Report it.