Volume C1 · Basic concepts · TAOCP Section 1.4.3.1
A MIX simulator
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.4.3.1 of the book. The nodes and the exercises of the section.
Contents
A MIX simulator
Overview
A simulator of a computer is a program that holds the state of that computer in its own data and changes the state by the rules of the computer. The book's simulator of MIX is written in MIX, and its input is a MIX program in memory. It keeps the registers in a row of cells, it keeps the sign of each register apart from the magnitude, so that it does not depend on a machine that has no negative zero, and it counts the time of the simulated machine in a clock cell. A central control, entered after each instruction, unpacks the instruction and jumps through a table of words to the routine of its operation. The table also gives the time of the operation. Three subroutines serve the routines: one reads a memory cell after it checks the address, one checks a field specification, and one gets the field of a memory cell that the operation needs.
This chapter reads the section in the way that the previous chapter reads Section 1.4.3. It states the plan of the book's program, and it adds a smaller simulator of the same plan that this chapter writes in full. The smaller program has no sign split and no clock, and it keeps one index register, but it has the central control, the table of routines and the groups of jumps. It runs on a real input, and the chapter shows its trace, proves its parts and counts its time for each simulated instruction.
The ideas
The simulated state is data. The registers, the comparison indicator and the memory of the simulated machine are cells of the simulator. The simulated memory is a block of cells, and the simulated address is an offset in this block. The simulated program counter is a register of the simulator.
The central control does the work that all instructions share. It fetches the word at the simulated counter, splits it into the address, the index, the field and the operation code, adds the content of the index register to the address, and advances the counter. Then it jumps through a table. Every routine ends with a jump back to the control.
The table of routines has one cell for each operation code. A code that the simulator does not know leads to an error stop. The table makes the time of the control the same for every operation.
Operations that share a code share a routine. For the codes of the groups of jumps and of the register loads, the field of the instruction chooses the operation. These groups use a second table, indexed by the field.
The simulator may be more exact than the machine. A simulator can count the time that the real machine would take, and it can stop with a message when the simulated program uses a cell that does not exist. The reduced simulator of this chapter does neither. It uses the same word format as the host and it leaves overflow and the checks of addresses out.
The simulation is slow. Each simulated instruction costs many host instructions, and the ratio of the two times is large.
Definitions, results and proofs
Theorem (the effective address, TAOCP Section 1.4.3.1). Let the simulated instruction have the address field (with its sign), the index field and the register with the content , and let . The control of the simulator below computes in rI1 the number , for and .
Node: 1.4.3.1-thm-address · Lean: not yet stated
Explanation. The simulated index register is a cell that holds a word. A table of cells IREGS, with the cell for holding zero, lets the control take the content with one load, whatever the value of . The cell for is the word I1REG, which follows IREGS.
Example. Let the simulated instruction be ADD and let . The control loads in rI1 and in rI2. The index is not zero, so it loads in rI3 from the cell IREGS plus , and INC1 0,3 gives rI1 . The simulator then reads the simulated cell , which is the host cell .
Proof. LD1 INST(0:2) puts the signed address in rI1, and LD2 INST(3:3) puts in rI2. If , J2Z skips the next two lines, and rI1 . If , LD3 IREGS,2 loads the cell IREGS plus , which is the cell of , in rI3. The word of this cell has an address part of two bytes, since every store to it is made with ST3 from a register of two bytes, and the load LD3 takes these two bytes and the sign. Then INC1 0,3 adds rI3 to rI1. So rI1 .
Theorem (the groups of jumps, TAOCP Section 1.4.3.1). In the reduced simulator, the instruction with the code and the field sets the simulated counter to when the simulated register A is negative, zero, positive, not negative, not zero and not positive, in this order of , and leaves the counter at the next instruction otherwise. The code does the same for the register I1. The code with or always jumps, with it never jumps and with it always jumps, since the simulator has no overflow. With it jumps when the comparison indicator is less, equal, greater, not less, not equal and not greater, in this order.
Node: 1.4.3.1-thm-jumps · Lean: not yet stated
Explanation. The six tests of the codes and and the last six of the code have the same form: a test of a signed number against zero. The simulator reuses one table and one set of six stubs for all of them. The comparison indicator is held as a number, , or , so that the test is a test of this number. A stub has two words: the conditional jump to TAKE, and a jump to CYCLE for the case in which the condition fails. The second word is needed, because a conditional jump that fails goes on to the next word, which would be the next stub.
Example. Take the code with (the test J1NN) and after DEC1. The routine loads I1REG in rA, and jumps to the cell of the table with the number . That cell jumps to the stub JNN, whose first word is JANN TAKE. Since rA is negative, JANN does not jump, and the second word of the stub goes back to CYCLE. The simulated counter is not changed. For the test is true, ENT6 0,1 puts the address in rI6, and the loop of the simulated program goes on.
Proof. The code loads the content of the simulated register I1 in rA and jumps through JRTAB with rI2 . The table has the six jumps to the six stubs in the order of the tests of the host: JAN, JAZ, JAP, JANN, JANZ and JANP. So the stub that is reached for is the test with the number in the list of the statement, and it jumps to TAKE if and only if the test is true. TAKE sets rI6 . If the test is false, the second word of the stub goes to CYCLE, and rI6 was increased by one in the control. The code is the same with AREG in the place of I1REG. For the code the routine jumps through J39TAB. The first four cells of this table are the unconditional jump, the unconditional jump, a jump to CYCLE and the unconditional jump, which is the case for . For the cells go to CMPJ. There rI2 is decreased by , the number of the comparison indicator, held in the cell CIND, is loaded in rA, and the jump through JRTAB gives the six tests in the order of the statement: negative is less, zero is equal, positive is greater, and so on.
Algorithms and programs
Program (a simulator of MIX in MIX, TAOCP Program M). The book's simulator is a program of the following design. The simulated registers are in consecutive cells in the order of the operation codes of the load and store instructions, and the cell for the register J and the cell for the zero register stand at the places that the codes of STJ and STZ suggest. A second row of cells holds the signs. The simulated memory has the locations to of the program, and a clock cell counts the simulated time. The central control puts the address with indexing in rI5, the operation code in rI4, the field in rI3 and the next location in rI6. A table of words has, for each code, the jump to its routine and the time of the instruction. Three subroutines, for memory access, for the check of a field and for the fetch of the field of a memory cell, serve most routines. This chapter writes a reduced simulator of the same plan, whose parts are described next.
Node: Program 1.4.3.1M · Lean: not yet stated
The reduced simulator. It simulates these thirteen operation codes of the host language: ADD, SUB, LDA, LD1, STA, ST1, CMPA, the jumps of the code (JMP, JSJ, JOV, JNOV, JL, JE, JG, JGE, JNE and JLE), the register jumps of the codes and for A and I1, the loads and the increments of A and I1 in the codes and (INCA, DECA, ENTA, ENNA, INC1, DEC1, ENT1 and ENN1), and HLT. It has the registers rA and rI1 of the simulated machine in the cells AREG and I1REG, the comparison indicator in CIND, and the simulated memory at the host addresses and above. The registers of the host are rI6 for the simulated counter, and rI1, rI2 and rI3 for the address, the field and the code of the instruction in hand. The control is at CYCLE.
CYCLE LDA SM,6 the instruction at the simulated counter
STA INST
LD1 INST(0:2) the address field
LD2 INST(3:3) the index field
J2Z 1F
LD3 IREGS,2 the content of the index register
INC1 0,3 M = a + r
1H LD2 INST(4:4) the field
LD3 INST(5:5) the code
INC6 1
JMP OPTAB,3 jump through the table
The table OPTAB has words, each a jump. The words for the codes and go to the routines ADD, SUB, HALT, LDA, LD1, STA, ST1, JMPG, JA, J1, EA, E1 and CMPA. All the other words go to BAD, which is a HLT. The routine for ADD, as an example of a short one, is these four lines.
ADD LDA AREG
ADD SM,1 the simulated memory cell M
STA AREG
JMP CYCLE
The comparison is the longest of the simple routines. It does the real comparison of AREG with the simulated cell, and it converts the host's indicator to a stored number, which is read later by the jumps.
CMPA LDA AREG
CMPA SM,1
JL CL
JE CE
ENT3 1 greater
JMP CS
CL ENT3 -1 less
JMP CS
CE ENT3 0 equal
CS ST3 CIND
JMP CYCLE
The simulated program. The test program has fourteen words at the simulated locations to , the five data words at to , and the limit at . It adds the five data words in the cell , and it then compares the sum with the limit: if the sum is greater, it jumps to the HLT, and otherwise it would store in the cell . The program is
| Loc | Instruction | Meaning |
|---|---|---|
| 0 | ENTA 0 | rA |
| 1 | STA 60 | |
| 2 | ENT1 4 | rI1 |
| 3 | LDA 60 | rA |
| 4 | ADD 50,1 | rA rA |
| 5 | STA 60 | rA |
| 6 | DEC1 1 | rI1 rI1 |
| 7 | J1NN 3 | go to if rI1 |
| 8 | LDA 60 | rA |
| 9 | CMPA 62 | compare rA with |
| 10 | JG 13 | go to if greater |
| 11 | ENTA 99 | rA |
| 12 | STA 63 | |
| 13 | HLT | stop |
A run. The simulated program runs the locations to once, the locations to five times, and the locations , , and once, which is simulated instructions. The program run directly in the host takes u. The run in the simulator takes u. At the end the cell of the simulated memory holds and the cell holds , and the whole simulated memory is equal to that of the direct run, cell by cell. The table has the first rounds of the simulation. The column Time is the host time that the pass of CYCLE took, and the last column is the host clock at the end of the pass.
| Pass | Simulated instruction | Time | rA after | rI1 after | Clock |
|---|---|---|---|---|---|
| 1 | ENTA 0 | 23 | 0 | 0 | 24 |
| 2 | STA 60 | 21 | 0 | 0 | 45 |
| 3 | ENT1 4 | 23 | 0 | 4 | 68 |
| 4 | LDA 60 | 21 | 0 | 4 | 89 |
| 5 | ADD 50,1 | 26 | 5 | 4 | 115 |
| 6 | STA 60 | 21 | 5 | 4 | 136 |
| 7 | DEC1 1 | 25 | 5 | 3 | 161 |
| 8 | J1NN 3 | 23 | 5 | 3 | 184 |
| 9 | LDA 60 | 21 | 5 | 3 | 205 |
| 10 | ADD 50,1 | 26 | 6 | 3 | 231 |
| 28 | LDA 60 | 21 | 14 | 668 | |
| 29 | CMPA 62 | 27 | 14 | 695 | |
| 30 | JG 13 | 26 | 14 | 721 | |
| 31 | HLT | 17 | 14 | 738 |
Correctness. The claim is that after passes of CYCLE, the cells AREG, I1REG, CIND and the block at hold the state of the simulated machine after instructions, and rI6 holds the simulated counter. For the cells hold the starting state, which is the program and the data, and ENT6 at START sets the counter to zero. In a pass, the control reads the instruction at the counter, forms by the theorem of the effective address, and jumps to the routine of the code. The routines are the proof by cases. ADD and SUB take the words of AREG and of the cell and put the sum or the difference back in AREG, and since the host uses the same sign and magnitude as the simulated machine, the result is the simulated result when there is no overflow. LDA and STA copy the whole word. LD1 loads two bytes and the sign of the cell and stores them in I1REG, which is the content that a load of an index register gives. ST1 stores the word of I1REG in the cell. The routines of the code and add or load the number in AREG or in the word of I1REG, with the fields of the instruction as the choice. CMPA stores , or in CIND, according as AREG is less than, equal to or greater than the cell. The jumps follow the theorem of the groups of jumps. HLT stops. Each routine ends with a jump to CYCLE, and a jump sets rI6 to in place of the next location. So the claim holds for , and the simulation is faithful for the thirteen operations and for programs in which no sum overflows, no simulated address leaves the block, and no instruction uses a field other than .
Analysis (the time of a simulated instruction, TAOCP Section 1.4.3.1). The time in the host for a simulated instruction is u, where is the time of the routine, and the values of are in the table.
Node: 1.4.3.1-anal-progM · Lean: not yet stated
| Simulated instruction | in u |
|---|---|
| ADD, SUB | 7 |
| LDA, LD1, STA, ST1 | 5 |
| CMPA, the result less or equal | 10 |
| CMPA, the result greater | 11 |
| INCA, DECA, INC1, DEC1 | 9 |
| ENTA, ENT1 | 7 |
| ENNA, ENN1 | 6 |
| JMP, JSJ, JNOV (code 39, ) | 4 |
| JOV | 2 |
| JL, JE, JG, JGE, JNE, JLE, taken | 10 |
| JL, JE, JG, JGE, JNE, JLE, not taken | 9 |
| JAN to JANP, J1N to J1NP, taken | 7 |
| JAN to JANP, J1N to J1NP, not taken | 6 |
| HLT | 1 |
Explanation. The number is the cost of the control. The lines of CYCLE before the jump through the table take u, the jump through the table takes u and the jump in the table takes u. The u for an index come from LD3 and INC1, which do not run when J2Z jumps. The values of are the sums of the times of the lines of the routine. For ADD they are .
Example. The simulated ADD at the location is indexed, so it takes u, as in the table of the run. The simulated DEC1 takes u. A taken J1NN takes u, and the last one, which is not taken, takes u.
Proof. The time of the control is the sum of the lines that always run, which is u, and u more when the index field is not zero. The routine then runs from the table cell to its jump back to CYCLE. The values of are the sums of the lines on that path. In the list, a number in parentheses is the time of the line. ADD and SUB: LDA (), ADD or SUB (), STA (), JMP (), which is . LDA, LD1, STA and ST1: a load or a store of u, a second of u and the jump of u, which is . CMPA: LDA (), CMPA (), then JL () and the three ways ENT3, JMP, ST3, JMP for greater (, with JE () before), and for less and equal one jump fewer, which gives for greater and for the others. The code and start with the jump through the table of the group () and the cell (). INCA and DECA continue with LDA (), INCA or DECA (), JMP SAVEA (), STA () and JMP CYCLE (), which is . ENTA has ENTA (), JMP SAVEA, STA and JMP CYCLE, which is , and ENNA has no jump to SAVEA, which is . INC1 and DEC1 have LD3 (), INC3 or DEC3 (), JMP SAVE1 (), ST3 () and JMP CYCLE (), which is . ENT1 is and ENN1 is in the same way. For the code , JMP J39TAB,2 and the table cell take u. For the cell goes to TAKE, where ENT6 and JMP CYCLE take u, so . For the cell is JMP CYCLE, so . For the cell goes to CMPJ, where LDA (), DEC2 () and JMP JRTAB,2 () run, then the cell of JRTAB () and the first word of the stub (). A taken test continues with TAKE ( u), which gives . A test that fails continues with the jump to CYCLE ( u), which gives . For the codes and the routine has LDA (), JMP JRTAB,2 (), the cell () and the first word of the stub (), which is u, and then TAKE () or the jump to CYCLE (), so or . HLT takes u.
The total. The run above has simulated instructions, and of them (the ADD) have an index. The time is plus the sum of the values of , where the first u is ENT6 at START. The values are ENTA , STA and ENT1 , which is . Then five times LDA , ADD , STA and DEC1 , which is . Then four taken J1NN of and one of , which is . Then LDA , CMPA with the result greater , JG taken and HLT , which is . The sum of is , and the total is u. The direct run takes u, so the simulation is times slower on this program. For a program with only the cheapest operations, such as LDA, the ratio is , and for ENTA it is .
The exercises
The table has the exercises of Section 1.4.3.1. The book marks no exercise of this section as recommended in the text that C1 reads. The kind is the class of spec 06.3 that C1 proposes.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.4.3.1-1 | 14 | informal | not yet stated | X1, 1.4.3.1-1 | |
| 1.4.3.1-2 | 20 | K6 | not yet stated | X1, 1.4.3.1-2 | |
| 1.4.3.1-3 | 22 | K6 | not yet stated | X1, 1.4.3.1-3 | |
| 1.4.3.1-4 | 14 | K6 | not yet stated | X1, 1.4.3.1-4 | |
| 1.4.3.1-5 | 24 | K6 | not yet stated | X1, 1.4.3.1-5 | |
| 1.4.3.1-6 | 28 | K6 | not yet stated | X1, 1.4.3.1-6 | |
| 1.4.3.1-7 | 32 | K6 | not yet stated | X1, 1.4.3.1-7 | |
| 1.4.3.1-8 | 20 | K3 | not yet stated | X1, 1.4.3.1-8 |
Exercises 2 and 3 ask for the two operations that the book's program leaves out, the shifts and the block move. Each is one more routine behind one more table word, and the pattern of the routines above shows the form: decode with the control, change the state, jump to CYCLE. Exercise 5 asks for the time to simulate LDA and ENTA against the machine. For the reduced simulator of this chapter the answer is u for LDA with no index, against u on the machine, and u for ENTA, against u. Exercise 8 is a true or false question about the range of the simulated counter, and its proof needs the invariant of the correctness proof above: the counter changes only by INC6 and by the jump routines. Exercises 6 and 7 add the input and output operators, and they need the model of the devices that Section 1.4.4 develops.
Lean
The Lean code of tamnd/taocp-lean at main has the model of MIX in TAOCP/MIX/ and the program logic in TAOCP/MIX/Logic/. It has no file for Section 1.4.3.1. The model of MIX in Lean is itself a function on the state of the machine, so it is the same kind of object as the simulator of this section. A theorem that a program of the simulator in MIX computes this function on the encoded state would be a refinement theorem between the Lean model and the program, and the logic of TAOCP/MIX/Logic/Hoare.lean does not state it yet. The Node lines of this section therefore name no declaration.
Notes and history
The use of a simulator to run the programs of one machine on another is old, and it was a common reason to write one when a computer was replaced. The section of the book argues that it is usually better to write the programs again, and it gives the simulator of MIX for the other two reasons: it is a clear example of an interpretive routine, and it is a tool for people who study the book and have no MIX machine.
A simulator of a computer is not a simulator of a discrete system, which keeps a list of events in time. The second kind is the subject of Section 2.2.5.
Section 1.4.3.1 writes a simulator of MIX in the language of MIX. The program takes a MIX program and its data from memory as plain data, and it carries out each instruction in the way that Section 1.3.1 defines. The section serves two aims: it is a worked example of an interpretive routine that has a central control, a table of routines and a few shared subroutines, and it is a tool for a reader who has no MIX machine. This chapter describes the design of the book's program in its own terms, and it builds and proves a reduced simulator of the same design. The reduced simulator handles thirteen operation codes, and the chapter derives the exact time that it needs for each simulated instruction.
The source of this page. Something is wrong? Report it.