Exercise · Volume 1 · 1.4.1-3
Exercise 1.4.1-3
not stated yet in Lean solution written
| Rating | M15 |
|---|---|
| Markers | M (mathematical) |
| Kind | K3, find a value: compute a number, a set or a closed form from the written solution |
| Section | Section 1.4.1 of Volume 1. Companion chapter: Subroutines |
The task
Complete the exit conditions of the subroutine MAXIMUM: say exactly what rJ and the comparison indicator contain at the end, and what the subroutine does when rI1 is not positive.
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
Which instruction is the last one that changes rJ, and what address does it store?
Hint 2
Find the last comparison made by the loop, and which two values it compares.
Hint 3
For follow the code by hand through the first pass of the three starting instructions.
Solution
Show the solution
Let the names be those of Program 1.3.2M, with EXIT the location of the final JMP *.
The register rJ. A jump instruction sets rJ to the address of the next instruction whenever the jump is taken, except JSJ. The return JMP * at EXIT is taken, so at the end rJ , independent of the data.
The comparison indicator, when . If the loop does no comparison, so the indicator does not change. If the last comparison is CMPA X,3 with rI3 . At this time rA holds the maximum of (the last of the maximal elements), and the comparison is between and . So the indicator ends as
- greater, if ,
- equal, if (then
JGEskips the change and rI2 stays larger than ), - less, if (then rI2 is set to , and is the unique maximum).
The case . Now ENT3 0,1 gives rI3 , and JMP CHANGEM runs the instructions ENT2 0,3, LDA X,3, DEC3 1 without any test. So rI2 , rA and rI3 . Since rI3 is negative, J3P does not jump and the subroutine exits. The indicator is not changed. This assumes that is a valid address.
Summary of the exit conditions. For : rA, rI2, rI3 as stated, rJ , and the indicator as above. For : rA , rI2 , rI3 , rJ and the indicator unchanged.
Check
Table (): the loop compares with (greater) and with (equal, no change). The result is rA , rI2 and the indicator is equal. Table : the last comparison is of with , which is less, and rI2 . For with : rA , rI2 , rI3 . A simulator run of these cases and of random tables gave the same results.
Remarks
The behaviour for is not a feature to rely on. It is a result of the loop test being at the end. The exit value of rI3 for a loop of this form is always the first value of the counter that fails the test.
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.