taocp

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

Exercise · Volume 1 · 1.3.1-18

Exercise 1.3.1-18

not stated yet in Lean solution written

Rating22
KindK8, compute by machine: find a number or an object by a large computation from the written solution
SectionSection 1.3.1 of Volume 1. Companion chapter: Description of MIX

The task

Trace the "number one" program and give the final values of rI1, rA, rX, the toggle, the indicator and the memory cells that it changed.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Start with a table of the registers and cells that the program can change: the cells 1 and 2, rA, rX, rI1.

Hint 2

Follow the instructions in order. The instruction ADD 1 is the interesting one: look at the sizes of the two numbers.

Hint 3

NUM and CHAR work on the ten bytes of rA and rX: bytes vv are taken mod 10 by NUM.

Solution

Show the solution

Assume that the program does not stand in the cells 0, 1 and 2. Step by step:

  • STZ 1: cell 1 is +0+0.
  • ENNX 1: rX is −0 0 0 0 1-0\,0\,0\,0\,1.
  • STX 1(0:1): the field (0:1)(0:1) of cell 1 gets the sign and the last byte of rX. So cell 1 is − 1 0 0 0 0-\,1\,0\,0\,0\,0, which is the number −b4-b^4.
  • SLAX 1: rX becomes − 0 0 0 1 0-\,0\,0\,0\,1\,0.
  • ENNA 1: rA is − 0 0 0 0 1-\,0\,0\,0\,0\,1.
  • INCX 1: rX becomes −b+1-b + 1, which is − 0 0 0 0 (b−1)-\,0\,0\,0\,0\,(b-1).
  • ENT1 1: rI1 is +1+1.
  • SRC 1: the ten bytes rotate one place right. The byte b−1b - 1 goes from the end of rX to the front of rA. So rA is − (b−1) 0 0 0 0-\,(b-1)\,0\,0\,0\,0 and rX is − 1 0 0 0 0-\,1\,0\,0\,0\,0.
  • ADD 1: rA gets −(b−1)b4−b4=−b5-(b-1)b^4 - b^4 = -b^5. This does not fit, so the overflow toggle is on and rA is − 0 0 0 0 0-\,0\,0\,0\,0\,0.
  • DEC1 -1: rI1 is +2+2.
  • STZ 1: cell 1 is +0+0.
  • CMPA 1: rA is −0-0 and the cell is +0+0: they are equal, so the indicator is EQUAL.
  • MOVE -1,1(1): the address is −1+2=1-1 + 2 = 1. One word goes from cell 1 to cell 2, and rI1 is +3+3. So cell 2 is +0+0.
  • NUM 1: the ten bytes are 0 0 0 0 0 1 0 0 0 00\,0\,0\,0\,0\,1\,0\,0\,0\,0, so the number is 1000010000, with the sign of rA. So rA is −10000-10000.
  • CHAR 1: the ten digits of 1000010000 are written as character codes, 00000100000000010000. So rA becomes − 30 30 30 30 30-\,30\,30\,30\,30\,30 and rX becomes − 31 30 30 30 30-\,31\,30\,30\,30\,30 (the sign of rX is not changed by CHAR; it was −-).
  • HLT 1: the machine stops.

The final state: overflow toggle on, indicator EQUAL, rA=− 30 30 30 30 30\text{rA} = -\,30\,30\,30\,30\,30, rX=− 31 30 30 30 30\text{rX} = -\,31\,30\,30\,30\,30, rI1=+3\text{rI1} = +3, cells 1 and 2 hold +0+0, all other cells unchanged. The result does not depend on bb.

Check

The program was run in a simulator with b=64b = 64 and b=100b = 100. Both runs gave the final state above and the register contents after each step as in the list. Note that in the step NUM 1 the number 1000010000 shows as bytes 0 0 2 28 160\,0\,2\,28\,16 for b=64b = 64 and 0 0 1 0 00\,0\,1\,0\,0 for b=100b = 100, because 2⋅642+28⋅64+16=100002 \cdot 64^2 + 28 \cdot 64 + 16 = 10000.

Remarks

The next exercise adds the execution times of the steps. In the run, the ADD is the only overflow.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.