Exercise · Volume 1 · 1.4.1-4
Exercise 1.4.1-4
not stated yet in Lean solution written
| Rating | 21 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 1.4.1 of Volume 1. Companion chapter: Subroutines |
The task
Write a subroutine that finds the maximum of with , with a second entrance for , and state its characteristics.
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
Take MAXN of (5) and change only the decrement of the counter in the loop.
Hint 2
The counter runs through while it stays positive. Find the value at which it stops.
Hint 3
The extra entrance only has to load into the register that holds , and then fall into the general entrance.
Solution
Show the solution
Take rI1 as the register for and rI3 as the counter, as in MAXN.
SMAX1 ENT1 1 Entrance for r = 1.
SMAX STJ EXIT Entrance for general r.
JMP 2F
1H CMPA X,3
JGE *+3
2H ENT2 0,3 Position of the largest so far.
LDA X,3 Value of the largest so far.
DEC3 0,1 k <- k - r.
J3P 1B
EXIT JMP *
The instruction DEC3 0,1 subtracts rI1, which is , from rI3. The values of the counter are , and the loop goes on while the counter is positive. The last positive value is the smallest positive number congruent to modulo , which is . The next value, , ends the loop.
Characteristics.
- Calling sequence:
JMP SMAX, orJMP SMAX1if . - Entry conditions: rI3 . For the entrance
SMAX, rI1 . - Exit conditions: rA with rI2 the largest index where it occurs, and rI3 . After
SMAX1, rI1 . AfterSMAX, rI1 is unchanged. The registers rJ and the comparison indicator are changed.
Check
Take and . The counter takes the values , so and the elements are examined. The final value is rI3 , and the formula gives . A simulator run for all and with random tables agreed with a direct computation of the maximum, of its index and of rI3.
Remarks
For the routine is MAXN again, with one extra instruction. The book of this exercise takes the same route. A call with examines only .
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.