taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

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
  1. A MIX simulator

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 6464 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 aa (with its sign), the index field ii and the register Ii\mathrm{I}_i with the content rir_i, and let r0=0r_0 = 0. The control of the simulator below computes in rI1 the number M=a+riM = a + r_i, for i=0i = 0 and i=1i = 1.

Node: 1.4.3.1-thm-address · Lean: not yet stated

Explanation. The simulated index register I1\mathrm{I}_1 is a cell that holds a word. A table of cells IREGS, with the cell for i=0i = 0 holding zero, lets the control take the content with one load, whatever the value of ii. The cell for i=1i = 1 is the word I1REG, which follows IREGS.

Example. Let the simulated instruction be ADD 50,150,1 and let r1=4r_1 = 4. The control loads a=50a = 50 in rI1 and i=1i = 1 in rI2. The index is not zero, so it loads r1=4r_1 = 4 in rI3 from the cell IREGS plus 11, and INC1 0,3 gives rI1 =54= 54. The simulator then reads the simulated cell 5454, which is the host cell 10541054.

Proof. LD1 INST(0:2) puts the signed address aa in rI1, and LD2 INST(3:3) puts ii in rI2. If i=0i = 0, J2Z skips the next two lines, and rI1 =a=a+r0= a = a + r_0. If i=1i = 1, LD3 IREGS,2 loads the cell IREGS plus 11, which is the cell of I1\mathrm{I}_1, 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 =a+r1= a + r_1. □\square

Theorem (the groups of jumps, TAOCP Section 1.4.3.1). In the reduced simulator, the instruction with the code 4040 and the field f∈{0,…,5}f \in \{0, \ldots, 5\} sets the simulated counter to MM when the simulated register A is negative, zero, positive, not negative, not zero and not positive, in this order of ff, and leaves the counter at the next instruction otherwise. The code 4141 does the same for the register I1. The code 3939 with f=0f = 0 or 11 always jumps, with f=2f = 2 it never jumps and with f=3f = 3 it always jumps, since the simulator has no overflow. With f=4,…,9f = 4, \ldots, 9 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 4040 and 4141 and the last six of the code 3939 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, −1-1, 00 or 11, 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 4141 with f=3f = 3 (the test J1NN) and r1=−1r_1 = -1 after DEC1. The routine loads I1REG in rA, and jumps to the cell of the table with the number 33. That cell jumps to the stub JNN, whose first word is JANN TAKE. Since rA =−1= -1 is negative, JANN does not jump, and the second word of the stub goes back to CYCLE. The simulated counter is not changed. For r1=0r_1 = 0 the test is true, ENT6 0,1 puts the address in rI6, and the loop of the simulated program goes on.

Proof. The code 4141 loads the content of the simulated register I1 in rA and jumps through JRTAB with rI2 =f= f. 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 ff is the test with the number ff in the list of the statement, and it jumps to TAKE if and only if the test is true. TAKE sets rI6 =M= M. 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 4040 is the same with AREG in the place of I1REG. For the code 3939 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 f=0,1,2,3f = 0, 1, 2, 3. For f=4,…,9f = 4, \ldots, 9 the cells go to CMPJ. There rI2 is decreased by 44, 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. □\square

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 00 to 34993499 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 6464 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 3939 (JMP, JSJ, JOV, JNOV, JL, JE, JG, JGE, JNE and JLE), the register jumps of the codes 4040 and 4141 for A and I1, the loads and the increments of A and I1 in the codes 4848 and 4949 (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 10001000 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 5757 words, each a jump. The words for the codes 1,2,5,8,9,24,25,39,40,41,48,491, 2, 5, 8, 9, 24, 25, 39, 40, 41, 48, 49 and 5656 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 00 to 1313, the five data words 3,1,4,1,53, 1, 4, 1, 5 at 5050 to 5454, and the limit 1010 at 6262. It adds the five data words in the cell 6060, and it then compares the sum with the limit: if the sum is greater, it jumps to the HLT, and otherwise it would store 9999 in the cell 6363. The program is

Loc Instruction Meaning
0 ENTA 0 rA ←0\leftarrow 0
1 STA 60 M[60]←0\mathrm{M}[60] \leftarrow 0
2 ENT1 4 rI1 ←4\leftarrow 4
3 LDA 60 rA ←M[60]\leftarrow \mathrm{M}[60]
4 ADD 50,1 rA ←\leftarrow rA +M[50+rI1]+ \mathrm{M}[50 + \mathrm{rI1}]
5 STA 60 M[60]←\mathrm{M}[60] \leftarrow rA
6 DEC1 1 rI1 ←\leftarrow rI1 −1- 1
7 J1NN 3 go to 33 if rI1 ≥0\ge 0
8 LDA 60 rA ←M[60]\leftarrow \mathrm{M}[60]
9 CMPA 62 compare rA with M[62]\mathrm{M}[62]
10 JG 13 go to 1313 if greater
11 ENTA 99 rA ←99\leftarrow 99
12 STA 63 M[63]←99\mathrm{M}[63] \leftarrow 99
13 HLT stop

A run. The simulated program runs the locations 00 to 22 once, the locations 33 to 77 five times, and the locations 88, 99, 1010 and 1313 once, which is 3+25+4=323 + 25 + 4 = 32 simulated instructions. The program run directly in the host takes 5050 u. The run in the simulator takes 738738 u. At the end the cell 6060 of the simulated memory holds 1414 and the cell 6363 holds 00, 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 −1-1 668
29 CMPA 62 27 14 −1-1 695
30 JG 13 26 14 −1-1 721
31 HLT 17 14 −1-1 738

Correctness. The claim is that after kk passes of CYCLE, the cells AREG, I1REG, CIND and the block at 10001000 hold the state of the simulated machine after kk instructions, and rI6 holds the simulated counter. For k=0k = 0 the cells hold the starting state, which is the program and the data, and ENT6 00 at START sets the counter to zero. In a pass, the control reads the instruction at the counter, forms MM 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 MM 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 4848 and 4949 add or load the number MM in AREG or in the word of I1REG, with the fields of the instruction as the choice. CMPA stores −1-1, 00 or 11 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 MM in place of the next location. So the claim holds for k+1k + 1, 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 (0:5)(0{:}5). □\square

Analysis (the time of a simulated instruction, TAOCP Section 1.4.3.1). The time in the host for a simulated instruction is 16+3[i≠0]+h16 + 3[i \neq 0] + h u, where hh is the time of the routine, and the values of hh are in the table.

Node: 1.4.3.1-anal-progM · Lean: not yet stated

Simulated instruction hh 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, f=0,1,3f = 0, 1, 3) 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 1616 is the cost of the control. The lines of CYCLE before the jump through the table take 2+2+2+2+1+2+2+1=142 + 2 + 2 + 2 + 1 + 2 + 2 + 1 = 14 u, the jump through the table takes 11 u and the jump in the table takes 11 u. The 33 u for an index come from LD3 and INC1, which do not run when J2Z jumps. The values of hh are the sums of the times of the lines of the routine. For ADD they are 2+2+2+12 + 2 + 2 + 1.

Example. The simulated ADD 50,150,1 at the location 44 is indexed, so it takes 16+3+7=2616 + 3 + 7 = 26 u, as in the table of the run. The simulated DEC1 11 takes 16+9=2516 + 9 = 25 u. A taken J1NN takes 16+7=2316 + 7 = 23 u, and the last one, which is not taken, takes 16+6=2216 + 6 = 22 u.

Proof. The time of the control is the sum of the lines that always run, which is 1616 u, and 33 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 hh 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 (22), ADD or SUB (22), STA (22), JMP (11), which is 77. LDA, LD1, STA and ST1: a load or a store of 22 u, a second of 22 u and the jump of 11 u, which is 55. CMPA: LDA (22), CMPA (22), then JL (11) and the three ways ENT3, JMP, ST3, JMP for greater (1+1+2+11+1+2+1, with JE (11) before), and for less and equal one jump fewer, which gives 1111 for greater and 1010 for the others. The code 4848 and 4949 start with the jump through the table of the group (11) and the cell (11). INCA and DECA continue with LDA (22), INCA or DECA (11), JMP SAVEA (11), STA (22) and JMP CYCLE (11), which is 99. ENTA has ENTA (11), JMP SAVEA, STA and JMP CYCLE, which is 77, and ENNA has no jump to SAVEA, which is 66. INC1 and DEC1 have LD3 (22), INC3 or DEC3 (11), JMP SAVE1 (11), ST3 (22) and JMP CYCLE (11), which is 99. ENT1 is 77 and ENN1 is 66 in the same way. For the code 3939, JMP J39TAB,2 and the table cell take 22 u. For f=0,1,3f = 0, 1, 3 the cell goes to TAKE, where ENT6 and JMP CYCLE take 22 u, so h=4h = 4. For f=2f = 2 the cell is JMP CYCLE, so h=2h = 2. For f=4,…,9f = 4, \ldots, 9 the cell goes to CMPJ, where LDA (22), DEC2 (11) and JMP JRTAB,2 (11) run, then the cell of JRTAB (11) and the first word of the stub (11). A taken test continues with TAKE (22 u), which gives 2+4+2+2=102 + 4 + 2 + 2 = 10. A test that fails continues with the jump to CYCLE (11 u), which gives 99. For the codes 4040 and 4141 the routine has LDA (22), JMP JRTAB,2 (11), the cell (11) and the first word of the stub (11), which is 55 u, and then TAKE (22) or the jump to CYCLE (11), so 77 or 66. HLT takes 11 u. □\square

The total. The run above has 3232 simulated instructions, and 55 of them (the ADD) have an index. The time is 1+32⋅16+5⋅31 + 32 \cdot 16 + 5 \cdot 3 plus the sum of the values of hh, where the first 11 u is ENT6 at START. The values are ENTA 77, STA 55 and ENT1 77, which is 1919. Then five times LDA 55, ADD 77, STA 55 and DEC1 99, which is 130130. Then four taken J1NN of 77 and one of 66, which is 3434. Then LDA 55, CMPA with the result greater 1111, JG taken 1010 and HLT 11, which is 2727. The sum of hh is 210210, and the total is 1+512+15+210=7381 + 512 + 15 + 210 = 738 u. The direct run takes 5050 u, so the simulation is 14.814.8 times slower on this program. For a program with only the cheapest operations, such as LDA, the ratio is 21/2=10.521 / 2 = 10.5, and for ENTA it is 2323.

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 2121 u for LDA with no index, against 22 u on the machine, and 2323 u for ENTA, against 11 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.