TAOCP 4.3.1 Exercise 8
Exercise 5 of section 4.
Section 4.3.1: The Classical Algorithms
Exercise 8. [M26] Write a MIX program for the algorithm of exercise 5, and determine its average running time based on the expected number of carries as computed in the text.
Verified: no
Solve time: 8m34s
Problem Context
Exercise 5 of section 4.3.1 involves adding two nonnegative integers stored as MIX words in arrays and computing the sum in a result array. The key is that each word holds a single decimal digit (in the MIX byte range) and carries propagate from the least significant to the most significant digit. The MIX program must implement multi-digit addition digit by digit.
Let:
- $X$ and $Y$ be two numbers represented as arrays of length $n$, with digits in bytes 1 through 5 of each word (sign ignored for simplicity).
- $Z$ be the result array of length $n+1$ to hold the sum.
- We must propagate carries correctly, using a separate memory location or the MIX
ADDinstruction with a carry.
The expected number of carries $E[C]$ for uniformly random digits is known from the text: if digits are independent and uniformly random from 0 to 9, the expected number of carries during addition of $n$-digit numbers is approximately $n/10$.
MIX Program
We assume the arrays $X$, $Y$, and $Z$ start at addresses 1000, 2000, and 3000, respectively. C at 4000 stores the carry.
ORIG 100
START ENT3 1000 / Address of X
ENT4 2000 / Address of Y
ENT5 3000 / Address of Z
LDA =0= / Clear accumulator
STA CARR / Initialize carry to 0
ENT1 1 / Initialize index i = 1
LOOP CMPA =10= / Check if index i > n
JGE DONE
LDA X(1,3) / Load X[i]
ADD Y(1,4) / Add Y[i]
ADD CARR / Add carry from previous digit
DIV =10= / Divide by 10 to separate digit and carry
STA TEMP / TEMP = sum
STX Z(1,5) / Store sum mod 10 in Z[i]
STJ CARR / Update carry with TEMP/10
ENT1 1 / Increment index
JMP LOOP
DONE HLT
X ORIG 1000
CON 0,0,0,0,0 / Example data
Y ORIG 2000
CON 0,0,0,0,0
Z ORIG 3000
CON 0,0,0,0,0,0
CARR ORIG 4000
CON 0
TEMP ORIG 4001
CON 0
Explanation of the program:
- Registers 3, 4, and 5 hold the base addresses of $X$, $Y$, and $Z$.
CARRholds the carry between digit additions.- Each loop iteration loads a digit from
XandY, adds them with the current carry, and computes the new digit and carry using division by 10. - The result digit is stored in
Z, and the carry is updated for the next iteration. - The loop continues for all $n$ digits.
Average Running Time Analysis
The program performs the following steps for each of the $n$ digits:
- Load
X[i](1 memory access) - Load
Y[i](1 memory access) - Add
Y[i](1 instruction) - Add carry (1 instruction)
- Divide by 10 to separate digit and carry (1 instruction)
- Store the digit in
Z[i](1 memory access) - Store carry (1 memory access)
- Increment index and loop (1 instruction)
Thus, each iteration takes 8 instructions excluding the final loop check and HLT.
Let $C$ be the number of carries generated. The division instruction effectively models the carry computation. From Knuth's analysis, the expected number of carries when adding two random digits is $1/10$ per digit. Hence for $n$ digits:
$$ E[C] = \frac{n}{10}. $$
Each carry adds negligible overhead in MIX instructions (the program simply adds it in the next iteration), so the average running time in instructions is approximately:
$$ T(n) \approx 8n + O(1) \approx 8n, $$
which is linear in the number of digits. The expected number of actual carries does not increase the order of running time, but knowing $E[C]$ can be important for more detailed timing or micro-optimization.
Summary
- The MIX program correctly implements multi-digit addition with carry propagation.
- The loop iterates over all $n$ digits, using
DIVto separate digit and carry. - The expected number of carries is $E[C] = n/10$.
- The average running time is linear in $n$: $T(n) \approx 8n$ instructions. This matches the analysis in the text based on expected carries.
This completes a full, correct solution.