Sat, 2 Nov 2013, 22:35

My students,

Still some unsuredness about the TRAP instruction.  In a couple of weeks
you will be reading the LC-3 code that the LC-3 operating system executes
to carry out each of the trap service routines.  But that will be then,
and now is now.  So, I have another email asking what happens when your program
has a TRAP instruction in it.  If you are solid on the TRAP instruction, then
feel free to delete this and move on.

A student writes:

> Hello Professor,
> I have a couple questions regarding the TRAP instructions and specifically
> how to use them for a couple parts of this program and LC3 programming in
> general.
> Firstly, I see that we need to make sure that the enter button is pressed
> before moving forward with the program. 

Yes, that is what the statement of the Programming Assignment says.  Type
a character, then type <<Enter>> (which essentially says you are done typing
the input that the program needs).  On this assignment, since the input is only
one character, we could have gotten away with just taking the one character
(decimal digit or d) and moving on.  In general, input will be a variable number
of characters so we need some way for the person at the keyboard to tell the
program that he/she has finished the input.  <<Enter>> is a convenient way.
> How exactly does the program read the Enter key when it is typed? 

TRAP x20 asks the operating system for help, specifically it asks the operating
system to just sit there waiting until someone hits a character, and then put 
the ascii code for that character into R0.  Once that is done, the TRAP routine
has finished executing and your program can continue to execute.  If you press 
the "2" key, the operating system will put x0032 into R0.  If you press the 
<<Enter>> key, the operating system will put x000A into R0.  Almost all keys 
on the keyboard have two corresponding ascii codes, one if the shift key is 
engaged when you press the key, and one if the shift key is not engaged.
For the <<Enter>> key, the shift key is irrelevant, so there is only one
corresponding ascii code.

> I know new line is simply printing x0A to the keyboard, 

We never print to the keyboard.  We read from the keyboard, we write to the

> but does hitting <Enter> itself have any impacts numerically
> on R0? I guess my question, in a more direct context, is do I need to save
> the contents of R0 within my program before the second set of TRAP
> instructions is written for the <enter>? 

Given what I have written above, I think you can answer this for yourself.
If not, please see a TA for one-on-one help.

> And right after the Enter is
> executed R0 should first get the value x0A when a new line is printed
> right?

I am not sure what you are asking.  Please talk to a TA.

> Secondly, I read about TRAP x23 instruction and my understanding is it
> reads a character typed from the keyboard, puts it into R0 and displays it
> onto the screen also. Is this correct? And if so, is this different than
> using both the TRAP x20 and TRAP x21 instructions for reading and printing
> a character in for the ID or "d"?

Read again the description of what the operating system will do for you if
you execute TRAP x23 and read again what it will do if you execute TRAP x20.  
Are they the same or are they different?

> Thank you for your time.

Good luck finishing the program by tomorrow night.

Yale Patt

> Sincerely,
> <<name withheld to protect the student wanting to understand TRAPs better>>