Exercise · Volume 1 · 1.4.3.1-1
Exercise 1.4.3.1-1
not stated yet in Lean solution written
| Rating | 14 |
|---|---|
| Kind | Informal: the exercise has no formal statement, so Lean does not state it from the written solution |
| Section | Section 1.4.3.1 of Volume 1. Companion chapter: A MIX simulator |
The task
Study how the subroutine FCHECK is used in the simulator. Can the code be organized better?
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
Find all the places that call FCHECK, and look at what follows each call.
Hint 2
Read step 3 at the end of Section 1.4.1 on what to do when a subroutine is always used with another one.
Solution
Show the solution
FCHECK is called in two places only: in GETV, where it is the first step, and in STORE, where it is the first step too. In both places the next instruction is a call of MEMORY. So the two routines are always used as a pair, in the order FCHECK then MEMORY. In both places the caller wants the value in rI1 and later the value in rI2 (GETV loads by LD2N R itself, and STORE does the same).
A better organization joins the two routines into one, here called FETCH. It checks the field, checks the address, loads the memory word, and leaves four results: in rI1, in rI2, the sign in rX and the magnitude in rA. Then
- the instruction pair
JMP FCHECK,JMP MEMORYin GETV and in STORE becomes one call, and the loading of is also done once, inside FETCH; - one
STJand one exit jump less are run, and one call less. This saves about 8 time units for each simulated instruction that uses it, out of about 100; - the routine FCHECK stays inside FETCH as its first part. The error exits are the same.
The routine MEMORY is also needed alone: JSJ uses it to check that a jump address is valid. So MEMORY must stay a separate entrance. This is done by making MEMORY the second entrance of FETCH, just after the field check, and not by two copies of the code.
Remarks
The same view is the rule of Section 1.4.1: when a subroutine is called from one or two places only, and always together with the same other one, one should ask if it should be a subroutine at all. See the C1 chapter on Section 1.4.3.1.
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.