A student writes with a lot of good questions. I will handle these in line.

     Hello Dr. Patt
     I'm a student in your EE306.  I was read a Ch 9 and
     I had a few questions about some things.  

     1) On pg 227-228, you explained the code for the HALT service
     routine (TRAP x25)
     then you asked the question "What instruction
     (or trap service routine) can be
     used to start theh clock? At first I
     thought the answer is None since I
     remember that question before when you asked what
     instructin can restart the
     clock and no instruction could since the clock
     needs to be running for an instruction to be carried out.

Exactly right.

     So I thought the same reasoning would apply in
     this case but then when I looked at
     the code in Figure 9.6 of TRAP x25, after
     the HALT is carried out, R1,
     R0, and R7 are restored and then a return is
     executed so I thought how can anything be
     done after the HALT service routine is executed?  

Nothing can until someone pushes a button somewhere (like on the front panel, for example) to restart the clock. Until then, yup, the machine has halted. Now, then, when someone does push a button, what would you like the trap routine to do? You might want it to restore the registers and return to the routine that asked for the machine to halt in the first place. As in, suppose you were performing some maintenance function on the computer, perhaps under program control. ...and you needed to stop the machine at some point to examine the hardware before moving on. TRAP x25 in your maintenance program could stop the computer, you carry out the task, then push the button, the clock restarts, restores the registers, and returns to the maintenance routine. A subtlety for sure, ...but you asked! Not something I am likely to ask about on the exam.

     That must mean there' s got to be a way for instructions to be
     carried out after a HALT service routine.
     But unfortunately I couldn't figure
     out how? So my question how is
     it be done? (just a hint would be appreciated)

     2) Also I was reading pg 233
     -235 and it's explaining the TRAP routine for
     character Input but this time in terms of
     small subroutines within the TRAP so
     that got me wondering, is a TRAP service routine made up of different
     subroutines fragments put together to accomplish that specific
     TRAP service? 

TRAP routines are written by people. ...smart people, usually, but people none the less. So, the answer is it depends on the person writing the routine. Either way. Depends on the person, depends on the task the routine is to carry out. It is a matter of individual style.

     Also is a TRAP service routine ALWAYS a
     callee save since the TRAP takes care
     of saving registers before overwriting them and then restoring them?

I am always careful when I hear the word always, particularly when written as ALWAYS. Best answer is to ask you a question: How does the caller know which gprs the trap routine will need to carry out the work. Answer: it does not. Therefore, much easier for the callee to do it.

     3) Also on pg 236 Figure 9.9 line 05, R7 is saved and in line 
     19 it is restored, why is it saved and restored if its not used
     in that specific trap routine which writes a NULL-terminated string
     to the console.  I know that R7 in a trap is used to store the
     incremented PC pointing to the next instruction
     in the user's program so after
     the TRAP service routine is done it can later
     load that value back into the PC so
     it can keep going with the program normally
     but in this example R7 is stored in
     a memory location, why isn't the PC stored
     into R7 and then at the end of
     the service routine, why isn't R7 loaded back into the PC?

One thing I was trying to do when I wrote a lot of those trap routines (awhile ago, actually) was to automatically store R7 immediately just in case there was a nested TRAP or subroutine inside, or in case I needed R7 in the service routine. One can reasonably argue (as you are doing) that if none of the above, I should have not wasted the two instructions.

     4) I'm not 100% clear on the difference between
     a TRAP service routine and a
     subroutine.  My understanding of it is that
     they're pretty much the same except
     that a TRAP service routine asks the operating
     system to do something for you and the code for that trap vector is
     in memory but a subroutine call uses code
     that you the programmer wrote once in your
     program or one that's in another module and you link it to your module
     to use it.  Is my understanding right and is it complete?

That "except" you mentioned is a pretty important except! But your understanding seems pretty good to me.

     Sorry for the long email and if my questions
     seem confusing, I guess it's because I'm confused.  Hopefully it
     will all connect before the test.  Thank you.

I think you are doing fine. All good questions.

Yale Patt

     A Confused 306 Student
     << name withheld to protect one who claims to be confused >>