11/1/04

A couple of students noticed unruly behavior with R7 in their program and don't understand why? Since some of the rest of you might also, I thought I had better get this to you immediately. Frankly, I did not think anyone would need to use R7 (there are seven other registers!) so I was saving this for this week to talk about.

Shame on me. I should have known better. AND, I will fix it in the third edition of the book, which I realize will be too late for you all.

Anyhow, the missing piece of information is this: The service routine that is carrying out the work of x21 or x23 or whatever the operating system is doing for you must have a way to get the PC back to the address of the instruction after the TRAP instruction in your program so that your program can continue to execute. The LC-3 uses R7 for that purpose. We call that mechanism a "linkage." It links the operating system service routine to the place from which it was called.

Therefore, in executing the TRAP instruction, the PC of your program is saved in R7, and available to the operating system to branch back, using JMP R7. Therefore anything that was in R7 before TRAP executes is lost.

Two simple solutions:

1. Don't use R7 for your data. I now realize that this produces a hardship for some since you only have eight registers.

2. Before TRAP x21 (for example), save what is in R7 and then restore it after the operating system does its job. The code will look like:

                ...
                ST    R7, SaveR7
                TRAP  x21
                LD    R7, SaveR7
                ...
                ...
        SaveR7  .BLKW #1

Before the end of the week, I think we will see completely how the TRAP instruction fully works.

See you in class.

Yale Patt