Exercise · Volume 1 · 1.4.1-5
Exercise 1.4.1-5
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
Invent a way to link to a subroutine on a machine without a J-register, and write MAX100 in this style with 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
The return address must reach the subroutine from the caller. Which register or cell can the caller load with it?
Hint 2
The result registers of MAX100 are rA, rI2, rI3. Which register does MAX100 not promise to keep?
Hint 3
The old convention stores the return address into the address field of the exit instruction. You can do this with an ordinary store.
Solution
Show the solution
Idea. The caller loads the return address into a register that the subroutine may destroy, and the subroutine stores it into the address field of its exit instruction. Here the register is rA, because MAX100 returns its result in rA anyway, so nothing that the caller wants to keep is lost.
Calling sequence:
ENTA *+2 rA <- address of the instruction after the JMP.
JMP MAX100
The subroutine:
MAX100 STA EXIT(0:2) Put the return address into the address field of EXIT.
ENT3 100
JMP 2F
1H CMPA X,3
JGE *+3
2H ENT2 0,3
LDA X,3
DEC3 1
J3P 1B
EXIT JMP * Address field was set by the first line.
The word that ENTA *+2 creates has the address in its two last bytes and sign plus. The store STA EXIT(0:2) copies the sign and the two last bytes into the address field of the exit instruction, and the other fields keep their values. The final JMP * therefore goes to the location after the caller's JMP. The instruction JMP after the store is an ordinary jump, so rJ is still changed by the hardware, but the subroutine never reads it.
Characteristics.
- Calling sequence:
ENTA *+2followed byJMP MAX100. - Entry conditions: none; rA is overwritten.
- Exit conditions: rA , rI3 , the comparison indicator is changed. Other registers are unchanged.
Check
Fill for and call the routine, with the instruction ENT4 77 after the call. A simulator run returned rA , rI2 (since ), and then executed ENT4 77, which shows that the return went to the right place.
Remarks
A machine without a link register usually puts the return address on a stack or in a general register, as modern machines do. The calling sequence here costs one extra instruction per call. See Exercise 1.4.1-7 for why one avoids the modification of the exit instruction.
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.