An answer:

A subroutine guarantees that some aspects of the caller's state of the machine are preserved, e.g. the values of all registers other than R0 in the linkage model we're using. It doesn't guarantee everything, though. It makes things much easier for us to say that it's allowable for a subroutine to change the condition code flags.

However, in the case of an ISR (interrupt service routine), there is no calling main program to make a "deal" with. Any aspect of the machine which the interrupted program relies on must be preserved. Since an interrupt can occur at any time, anything we say the interrupted program can't rely on can never be used.

We don't need to be able to rely on the condition codes being preserved when we make a deliberate subroutine call, but we need to be able to rely on the condition codes being preserved from one instruction to the next, even if an interrupt occurs between them, because we can't plan for that. We need to be able to use the condition codes in the ISR, thus we need to save and restore them.

There's another kind of answer which I also gave full marks to. Some students pointed out that an ISR changes the priority level (it's actually in the interrupt vector, so this happens as soon as the interrupt occurs, not as a deliberate action of the ISR, but that's a side point), so there has to be some mechanism to restore the old priority level. If this was explained clearly I gave this answer four out of four as well.


[exam] [CSC 258 additional problems] [main course page]