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.2

Trace routines

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.2 of the book. The nodes and the exercises of the section.

Contents
  1. Trace routines

Trace routines

Overview

A trace routine is a debugging tool. It takes control of a program, lets each instruction of the program run on the real machine, and writes down, before each step, the location of the instruction and the state of the machine. The programmer reads the list and sees where the program went wrong. The method is different from the method of a simulator. A simulator carries out the instruction by its own code. A trace routine copies the instruction to a cell, runs it, and goes on, so the speed of one step is the speed of the instruction plus a fixed overhead.

Only two kinds of instruction cannot be run as they are. A jump would take the program counter away from the trace routine, and the instruction STJ would store the jump register of the trace routine in place of the jump register of the traced program. The routine of this chapter turns each jump into a jump to a stub that takes control back, and it turns STJ into a store of a simulated jump register. The routine uses one data register of the machine, the register A, and it does not change the comparison indicator or the other registers.

The ideas

The step is: fetch, record, run, advance. The routine has a cell for the location of the next instruction. It reads the instruction at this location, records the pair, runs the instruction in a cell of its own, and adds one to the location.

Registers belong to the traced program. The registers, the comparison indicator and the overflow toggle must have the values that the traced program expects when its instruction runs. The routine therefore keeps its own data in memory cells, and it loads and stores the register A around each instruction. It uses no other register and no instruction that sets the comparison indicator.

A jump is turned into a call of the routine. The address and the index of the jump are replaced by the address of a stub of the routine, and the original address and index are copied to an instruction that computes the target. If the condition of the jump holds, the machine goes to the stub with the registers unchanged, and the stub finds the target. If it does not hold, the machine goes on to the next instruction of the routine, and the step ends as for a plain instruction.

The jump register is simulated. A taken jump sets the real jump register to a place in the routine, and the traced program would find this value wrong. The routine keeps the value that the traced program should see in a cell, and it changes this cell at each taken jump that is not JSJ. The instruction STJ is run as a store of that cell.

The routine does not trace itself. The traced program must not store in the cells of the routine, and it must not use the device on which the record is written.

Definitions, results and proofs

Definition (trace routine, TAOCP Section 1.4.3.2). Let PP be a program of a machine and let s0,s1,…s_0, s_1, \ldots be the states of the machine when it runs PP alone, with ℓk\ell_k the location of the instruction that runs at the state sks_k. A trace routine is a program that, when it is given the starting location of PP, runs PP so that PP goes through the same states, and that writes down, for each kk, the location ℓk\ell_k and the instruction at ℓk\ell_k, and some of the state, before the instruction runs.

Node: 1.4.3.2-def-trace · Lean: not yet stated

Explanation. The states that PP goes through are the states of PP alone, so the trace is a transparent copy: it does not change what PP computes. The extra record is the reason to use it. Some parts of the state cannot be exactly the same. The jump register and the registers of the routine are in the state of the real machine, and the routine has to hold the jump register of the traced program in a cell. The next theorem says which parts are held.

Example. Take the program of two instructions ENT1 2 and DEC1 1 that starts at location 100100 and then stops at 102102. Run alone, it has the locations ℓ0=100\ell_0 = 100, ℓ1=101\ell_1 = 101, and the states with rI1 =0= 0, 22, 11. A trace routine for it records the pairs (100,ENT1 2)(100, \text{ENT1 2}) and (101,DEC1 1)(101, \text{DEC1 1}), and then lets the program reach 102102 with rI1 =1= 1.

Proof that the example agrees with the definition. The program alone does ENT1 2 at 100100, which gives rI1 =2= 2, then DEC1 1 at 101101, which gives rI1 =1= 1. The trace routine runs these two instructions in the same order with the registers of the program. So the sequence of states is the same, and the list of pairs has one entry for each instruction that ran. □\square

Theorem (transparency of the trace routine, TAOCP Section 1.4.3.2). Let a program PP not store in any cell of the trace routine, let it not use the instructions JBUS, JRED or IOC with the device of the log, and let it not read the cell of the jump register of the routine. Then the routine below runs PP so that, before each instruction of PP, the register A, the registers I1 to I6 and X, the comparison indicator and the overflow toggle of the machine have the values that they have when PP runs alone, and the simulated jump register has the value of the real jump register of PP alone.

Node: 1.4.3.2-thm-transparent · Lean: not yet stated

Explanation. The theorem lists what is preserved, and the restrictions state the condition. The routine stores the register A in the cell AREG after each plain instruction and loads it before. It does not touch the other registers. The comparison indicator and the overflow toggle are changed only by the instructions of PP and by an arithmetic instruction of the routine that overflows, which does not occur, since the numbers in the routine are the small codes of instructions and locations.

Example. Let PP contain DEC1 11 followed by J1P. When the DEC1 runs, rI1 is the register of PP. The routine has not changed it, since all its own work is done in the cells and in the register A. The instruction J1P then sees this value of rI1, because the routine runs the jump with the original condition on the real rI1.

Proof. The proof follows the two kinds of instruction. A plain instruction runs in the cell INST after LDA AREG, and after it STA AREG. Since the routine between two instructions uses only the register A and the instructions LDA, STA, INCA, DECA, JAZ, JAN and JAP, which change no other register and not the comparison indicator, the registers I1 to I6, X, the indicator and the toggle are as PP left them. The register A is saved in AREG by STA AREG after the instruction of PP and loaded before the next. So the state before the next instruction is the state that the instruction of PP produced. A jump instruction runs in INST with the address and index of the stub JUMP. The condition of the jump tests the registers, and A has been loaded before the test. So the jump is taken exactly when it would be taken in PP. When it is taken, no register has changed, and the stub reads the target by ENTA from the copy of the original address and index, which uses the same index register, and the value in the register A is the target. The cell AREG still holds the register A of PP, because the jump does not change it. If it is not taken, the instruction takes its place as a plain instruction, and STA AREG stores the unchanged A. For the jump register, the stub sets the cell JREG to the location of the jump plus 11 when the instruction is not JSJ. This is the value that the machine sets in a taken jump, so the cell holds the real value of the jump register of PP alone. The instruction STJ is run as a store of the register A in the field of its own, and the register A holds JREG, which is the value that the store STJ would write. □\square

Algorithms and programs

Program (a trace routine in MIX, TAOCP Section 1.4.3.2). The routine has the entry TRACE. A program calls it by JMP TRACE, and the routine starts to trace the instruction after the call. It writes, for each instruction, two words in a log: the location in the field (1:2)(1{:}2) of the first word, and the instruction in the second. It stops when the traced program jumps to the location LEAVE, and then the traced program continues with the instruction after that jump, outside the trace.

Node: 1.4.3.2-prog-trace · Lean: not yet stated

The control. The first part of each step is the same for every instruction. The location comes in rA. The routine stores it, writes it with the word at that location in the log, which has a pointer LOGP, and stores the word in INST. The writing into the log and the reading of the instruction use two instructions whose address field is changed by the routine, PUT1 STA * and FETCH LDA *.

CYCLE  STA  PREG(0:2)    the location of this step
       STA  FETCH(0:2)
       LDA  LOGP(0:2)    the place in the log
       STA  PUT1(0:2)
       INCA 1
       STA  PUT2(0:2)
       INCA 1
       STA  LOGP(0:2)
       LDA  PREG
PUT1   STA  *            write the location
FETCH  LDA  *            read the instruction
       STA  INST
PUT2   STA  *            write the instruction

The second part decides the kind of instruction by its code. The routine loads the code, takes away 3232 and goes to ISSTJ when the result is zero. It takes away 77 more and goes to PLAIN when the result is negative, which is the case of the codes below 3939. It takes away 88 more and goes to PLAIN when the result is positive, which is the case of the codes above 4747. The codes 3939 to 4747 fall through to the part that makes a stub.

       LDA  INST(5:5)
       DECA 32
       JAZ  ISSTJ
       DECA 7
       JAN  PLAIN
       DECA 8
       JAP  PLAIN
       LDA  INST(0:3)    a jump: keep the address and index
       STA  INST1(0:3)
       LDA  8F(0:3)      put the address of JUMP in its place
       STA  INST(0:3)
PLAIN  LDA  AREG
INST   NOP  *            the instruction runs here
       STA  AREG
INCP   LDA  PREG(0:2)    next location
       INCA 1
       JMP  CYCLE

The word at the label 8H is the instruction JSJ JUMP, and the fields (0:3)(0{:}3) of it are the address of the stub and the index zero. The stub is at JUMP.

JUMP   LDA  8B(4:5)      was it a JSJ?
       SUB  INST(4:5)
       JAZ  *+4
       LDA  PREG(0:2)    no: JREG gets the location plus one
       INCA 1
       STA  JREG(0:2)
INST1  ENTA *            rA gets the address of the jump
       DECA LEAVE
       JAZ  LEAVE1
       INCA LEAVE
       JMP  CYCLE

The instruction STJ is run as a store of the simulated jump register. The routine changes its code to the code of STA, loads the register A from JREG, runs the copy in the cell INSTS, and does not store A, so that AREG keeps its value.

ISSTJ  ENTA 24
       STA  INST(5:5)
       LDA  INST
       STA  INSTS
       LDA  JREG(0:2)
INSTS  NOP  *
       JMP  INCP

At LEAVE1 the routine sets the exit to the location after the jump to LEAVE, loads the register A and jumps out.

The traced program. The test program has the instructions in the table at the locations 20012001 to 20062006 and a subroutine at 20102010. The call at 20002000 is the call of the trace routine. The subroutine adds 77 to the register A and returns through the cell EXIT by the linkage of Section 1.4.1.

Loc Instruction Meaning
2000 JMP TRACE start the trace
2001 ENT1 2 rI1 ←2\leftarrow 2
2002 JMP 2010 call the subroutine
2003 DEC1 1 rI1 ←\leftarrow rI1 −1- 1
2004 J1P 2002 repeat while rI1 >0> 0
2005 JMP LEAVE end of the trace
2006 HLT stop
2010 STJ 2012 the entrance of the subroutine
2011 INCA 7 rA ←\leftarrow rA +7+ 7
2012 JMP * the exit, set by STJ

A run. The register A is 55 when the trace starts, and rI3 and rX hold 3333 and 7777. The run was made in a simulator of MIX. It reaches the HLT at 20062006 at time 706706 with rA =19= 19, rI1 =0= 0, rI3 =33= 33 and rX =77= 77, which are the values of the program run alone. The log has 1414 pairs, and its locations are 2001,2002,2010,2011,2012,2003,2004,2002,2010,2011,2012,2003,2004,20052001, 2002, 2010, 2011, 2012, 2003, 2004, 2002, 2010, 2011, 2012, 2003, 2004, 2005, in the same order as the locations of the run alone. The word that STJ stored in the cell 20122012 is the jump to 20032003, so the first return is right. The next table shows the start of each step. The column Clock is the clock at the entrance of CYCLE for that step, and rJ is the simulated jump register at that time.

Step Loc Instruction Clock rA rI1 rJ
1 2001 ENT1 2 9 5 0 2001
2 2002 JMP 2010 50 5 2 2001
3 2010 STJ 2012 108 5 2 2003
4 2011 INCA 7 152 5 2 2003
5 2012 JMP * 193 12 2 2003
6 2003 DEC1 1 251 12 2 2013
7 2004 J1P 2002 292 12 1 2013
8 2002 JMP 2010 350 12 1 2005
9 2010 STJ 2012 408 12 1 2003
10 2011 INCA 7 452 12 1 2003
11 2012 JMP * 493 19 1 2003
12 2003 DEC1 1 551 19 1 2013
13 2004 J1P 2002 592 19 0 2013
14 2005 JMP LEAVE 641 19 0 2013

Correctness. The correctness is the theorem of transparency, together with these two points. First, the log. At each entrance of CYCLE, rA is the location of the next instruction: the entry sets it from the call, INCP sets it to the location plus one, and the stub sets it to the target. The routine stores the location, reads the cell at it, and stores both in the log at the place LOGP, which it advances by two. The reading uses an instruction whose address is changed by the routine, and the change is made before the instruction runs. So the log has the location and the instruction for each step, in the order of the run. Second, the end. The target of a jump is compared with LEAVE by DECA and JAZ, which do not change the comparison indicator. When it is equal, the exit is set to the location of the jump plus one, which is the instruction after JMP LEAVE, and the register A is loaded from AREG, so that the traced program continues with its own A.

Time. Let xx be the time of the instruction that runs. The common start of CYCLE, up to the test for STJ, takes 2828 u. The time of a step is then

Kind of step Time in u
a plain instruction with code below 3939 38+x38 + x
a plain instruction with code above 4747 40+x40 + x
STJ 4444
a jump that is not taken 48+x48 + x
a jump that is taken and is not JSJ 5858
a taken JSJ 5353
a taken jump to LEAVE, with the leaving 6464

The total for the run is 99 u for the call and the entry, then the times of the 1414 steps: the sum 41+58+44+41+58+41+58+58+44+41+58+41+49+64=69641 + 58 + 44 + 41 + 58 + 41 + 58 + 58 + 44 + 41 + 58 + 41 + 49 + 64 = 696, and 11 u for the HLT. The total is 9+696+1=7069 + 696 + 1 = 706 u. The traced instructions take 1616 u when they run alone, so the average overhead is about 4848 u for each step, and the factor of slowing is 696/16≈43.5696 / 16 \approx 43.5.

Derivation of the entries of the table. The 2828 u are STA (22), STA (22), LDA (22), STA (22), INCA (11), STA (22), INCA (11), STA (22), LDA (22), the store of the location (22), the read (22), STA INST (22), the store of the instruction (22), LDA (22), DECA (11) and JAZ (11). A plain instruction with a code below 3939 then runs DECA (11) and JAN (11), which is 22 u. The part from PLAIN takes LDA AREG (22), the instruction (xx), STA AREG (22), and INCP, which is LDA (22), INCA (11) and JMP (11). The sum is 28+2+2+x+2+4=38+x28 + 2 + 2 + x + 2 + 4 = 38 + x. For the codes above 4747 two more lines run, DECA and JAP, so the sum is 40+x40 + x. A jump runs four tests (44 u) and the four lines that make the stub (88 u), and then PLAIN as above: 28+4+8+2+x+2+4=48+x28 + 4 + 8 + 2 + x + 2 + 4 = 48 + x. A taken jump costs 11 u for the jump itself in place of x+2+4x + 2 + 4, and then the stub: LDA (22), SUB (22), JAZ (11), LDA, INCA and STA of JREG (55), ENTA (11), DECA (11), JAZ (11), INCA (11) and JMP CYCLE (11), which is 1515 u. So the step takes 28+4+8+2+1+15=5828 + 4 + 8 + 2 + 1 + 15 = 58 u. A JSJ skips the 55 u of the JREG lines, which gives 5353. A taken jump to LEAVE has JAZ taken in place of INCA and JMP CYCLE, and the lines of LEAVE1 (LDA, INCA, STA, LDA, JMP, which are 88 u), so it takes 28+4+8+2+1+13+8=6428 + 4 + 8 + 2 + 1 + 13 + 8 = 64. The STJ step takes 2828, then ENTA (11), STA (22), LDA (22), STA (22), LDA (22), the store of the instruction (22), the jump to INCP (11) and INCP (44), which is 4444. □\square

The exercises

The table has the exercises of Section 1.4.3.2. 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.2-1 22 K6 not yet stated X1, 1.4.3.2-1
1.4.3.2-2 26 K6 not yet stated X1, 1.4.3.2-2
1.4.3.2-3 10 informal not yet stated X1, 1.4.3.2-3
1.4.3.2-4 25 K6 not yet stated X1, 1.4.3.2-4
1.4.3.2-5 28 K6 not yet stated X1, 1.4.3.2-5
1.4.3.2-6 40 K6 not yet stated X1, 1.4.3.2-6
1.4.3.2-7 25 informal not yet stated X1, 1.4.3.2-7

Exercise 1 asks for a trace routine that restores the jump register when it leaves. The routine above has the right value in the cell JREG. Only a taken jump sets the real register, so the exit must make a jump that gives it this value, and the exercise is to find how. Exercise 2 asks for a record on tape in a fixed format, and the log of this chapter is the first step: a record of fixed size for each step. Exercise 3 asks why a tape is better than the printer for the record, and the reason is the speed of the output: the program is slowed by the output more than by anything else. Exercises 4, 5 and 6 are about the routine that traces itself. The restriction of the theorem, that the traced program must not store in the cells of the routine, is exactly what a tracing routine breaks, since the inner routine runs and changes its own cells. Exercise 7 asks for a jump trace. The routine above shows the base: the stub JUMP is the only place that sees a taken jump, so a jump trace logs there and does not log in CYCLE.

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.2. The definition of a trace is about the sequence of states of the machine, which the Lean model has as the iterate of its step function. A trace routine would be a program with a statement that its run maps to the same sequence, and the logic of TAOCP/MIX/Logic/Hoare.lean would carry the statement for one step as a triple whose frame says that the registers I1 to I6, X and the indicator are unchanged. The frame lemmas of TAOCP/MIX/Logic/Frame.lean have this kind of statement for one instruction. The theorem of transparency is not stated, and the Node lines name no declaration.

Notes and history

A trace on a printer or on a tape was a usual way to find an error before interactive debuggers were common. The design rules are given in the book as a short list: the routine should be short, so that it can coexist with a large program, because the time is spent on the output anyway; it should use as few registers as possible, because the less it uses the less it must restore; and the traced program should have only the restrictions that cannot be removed.

The jump register is the hard part of a trace on this machine, because it is a register that the machine changes as a side effect of a jump, and a trace routine is itself made of jumps. The solution of this chapter, to simulate the register in a cell and to run STJ as a store of that cell, is the same one that every simulator has to use.

Section 1.4.3.2 describes a program that runs another program one instruction at a time and records what happens. It is the simplest useful simulator: it does not model the machine, it lets the machine do the work and keeps control between instructions. The only instructions that the trace routine cannot run as they are, the jumps and the store of the jump register, need special care. This chapter defines a trace routine, then builds one of its own design in MIX that logs the location and the word of each step, proves that it is transparent for the traced program, runs it on a program with a subroutine call, and counts its time.

The source of this page. Something is wrong? Report it.