This message is for those who still do not understand the TRAP instruction.
If you really do understand the TRAP instruction, please hit *delete* and move on.

My office door is open, so I have had the pleasure of just helping two more students
who don't understand the TRAP instruction.  In fact, I am reaching the point where
I wish I had never introduced the assembler names GETC, PUTS, IN, OUT, and HALT.
For some reason, some students equate these names with the routines the operating
system executes in their behalf.  I think I will delete these names from the 3rd
edition, and insist that students carry in their pocket Table A.2 (page 543) that shows the 
trap vectors for each of trap service routines (with the second column deleted, 
of course.) 

If the user program wants the operating system to halt, the user program must
include the SINGLE instruction TRAP x25.  And, if the user program wants the operating
system to write a string of ASCII characters to the screen, starting at the location
specified in R0, the user program must include the SINGLE instruction
TRAP x22.  Apparently allowing some students to write HALT instead of TRAP x25, or
PUTS instead of TRAP x22 is confusing.

GETC, PUTS, IN, and OUT are not routines, they are mnemonic devices for TRAP x20, 
TRAP x22, TRAP x23, and TRAP x21, so the programmer does not have to memorize the
corresponding trap vectors.  They are each SINGLE instructions.

If the user program contains PUTS (for example), the Assembler will translate PUTS
into LC-3 machine language as:

                                1111 0000 0010 0010

1111 because PUTS is a mnemonic device for TRAP x22, and the opcode for TRAP is 1111.

0010 0010 because the trap vector is x22, and it goes in bits[7:0].

The TRAP instruction is ONE instruction.  See page 541 in the textbook.  Read the
four short sentences under "Description" that describes what the TRAP instruction
does.  The TRAP instruction does NOT execute the TRAP service routine.  Like all
instructions, the TRAP instruction goes through the same steps of the instruction 
cycle that all instructions go through.  It is fetched (using the PC), it is decoded, 
and based on the decode, the work of the SINGLE instruction whose opcode is TRAP is 
carried out.

The TRAP instruction does three things:

     a. It loads the incremented PC into R7, so the service routine will know where
        to go AFTER it (the service routine) is done.

     b. It uses the trapvector to find the starting address of the service routine. 

     c. It loads the starting address of the service routine into the PC, so that
        when the LC-3 starts the NEXT instruction, the address of the first instruction
        of the service routine will be in the PC (which is where the LC-3 ALWAYS looks
        to find the address of the instruction it wishes to process).

AND, that is ALL the TRAP instruction does.

By means of this email, I am instructing my TAs, when they see IN, OUT, PUTS, etc.
in a student program, to ask the student what those names mean.  If the student 
does not know, I want those names removed from the student's program and replaced
by the corresponding actual instruction that the student program wants executed.

In summary, the TRAP instruction is a SINGLE instruction that does the three things
specified above and ONLY those three things.  The TRAP instruction does not execute
the TRAP service routine.  The TRAP instruction provides the mechanism that enables
the LC-3 to execute the trap service routine by loading PC with its starting address.
...and the TRAP instruction loads R7 with the address of the instruction in the user program 
following the TRAP instruction, so that once the operating system is done executing
the TRAP service routine for the user, the operating system has a way to return to
the user program.

If this is still not clear, please talk to your TA or me.

Yale Patt