Wed, 4 Dec 2013, 21:23



My students,

I have gotten several emails from students saying they were unable to
execute RTI because the computer was in user mode.  In EVERY case, the
problem was the same - and it was the programmer, not the computer
who was at fault!

At risk of going on and on and on, I can not miss this opportunity to
teach you.  So, I will!

First, the short answer: When you call a subroutine and R7 already has
a return address in it, be sure to save R7 before calling the subroutine.
Otherwise you have just destroyed the link that exists in R7.  THAT is at
the heart of the problem for those student programs that I have seen.

Now the long story.  But first, a little warning.  This is long.  If you
are not seeing the RTI privilege mode error, you are probably either not
using JSR inside your interrupt service routine, OR you are saving and 
restoring R7 properly with respect to using JSR.

NOW the long story.

1. You are in user mode, running the "Texas Checkerboard" program and someone
hits a key on the keyboard.  More than likely (i.e., very, very likely) you 
are in the delay subroutine.  ...which means R7 contains the return to the
main program that is printing the two alternating strings.  Let's call that
address which is currently in R7: address A.

2. Someone hits a key on the keyboard while you are executing the instruction
in memory location B, which is part of the delay subroutine.  You finish 
executing the instruction at memory location B and initiate the interrupt.  
The computer changes PSR[15] to 0, pushes PC (i.e., B+1) and PSR on the stack 
and loads PC with the starting address of the keyboard interrupt service 
routine.  Everything is fine so far!

3. At some point within the interrupt service routine, you decide to JSR to 
a subroutine, BUT you do not save R7 before doing so.  Let's say the JSR 
instruction was in memory location C.  You have just killed what was in R7 
(i.e., A) and replaced it with C+1.

4. At some point you return from the subroutine to C+1, and continue on,
eventually reaching the RTI, which pops the stack and takes you back to B+1
which is in the middle of the delay subroutine.  

5. At some point you finish the delay subroutine, wherein RET takes you, NOT
to A which is where you want to go, but to C+1 which is in the middle of the 
interrupt service routine.  So this time you start executing from the middle
of the interrupt service routine, BUT since you did not enter via the
initiation of an interrupt, you never set PSR[15] to 0.  

6. You execute the rest of the subroutine to completion, terminating with 
an RTI.  EXCEPT, since PSR[15] is 1, the computer can not execute RTI and
you get the privileged mode violation.

It had NOTHING to do with RTI!  It had everything to do with the fact that
when you returned from the delay program, instead of going to A, you went to
C+1 BECAUSE you did not save and restore R7 before and after the JSR in the
interrupt service routine.

OKAY?

Good luck finishing the program and studying for the final.

Yale Patt