Wed, 2 Oct 2013, 00:34

And one more.  I almost did not send you this one, but then decided it
won't take you long to read it and maybe it would be helpful.

A student writes:

> Dear Professor Patt,
> After reading your latest email concerning the fact that we should set our
> registers to 0 before we actually execute our program, I was wondering if
> there was any way to clear the registers at the end of our program after we
> have stored the value from the register into x3101. That way when we
> execute the program again the register will still be set to 0.

I am responding because you are developing a wrong mindset here, and I would 
like to nip it in the bud at the gitgo.  It is not about setting all registers 
to 0 at the end, so they will contain 0 the next time you execute the program 
again.  For one thing there is no guarantee someone else won't execute their 
program between when you are done the first time and when you want to start 
the second time.  So that won't work.

More importantly, it is all about a single, very simple fundamenal rule: 
You can not read what is in a location until you have written to that 
location.  In fact, you don't have to set all registers to 0 or all memory
locations to 0.  In fact, if the first time you access a register, it is the
result of a load, as was the case in our very first six-line program, you do
not have to first set that register to 0.  Why?  Because you do not read the
contents of that register until AFTER you have loaded a value into it.

Very simple rule: You can not take money out of the bank until you (or Daddy or
Mommy) has put money into the bank.  ...unless you are a bank robber, of course.
You can not empty your bag of groceries at home until after you have filled 
your bag with groceries at the supermarket.  This is not rocket science!

In the program we wrote in class on Monday.  There are 8 registers in the LC-3.
We never used R5,R6, or R7.  Therefore we do not need to zero them.  The first
time we accessed R0,R1, and R4, it was to put a value in them.  Therefore we
do not need to zero them.  BUT R1 and R2.  The first time we wanted to access 
them, we needed to read what was there.  Therefore, in the initialization part
of the program, we set them to 0 before we read them for the first time.

> After a total of 6 hours I was finally able to get my program to work
> correctly, and having to insert an "AND" code at the beginning of the
> program will throw off all of my off-sets. Of course if it is not possible
> to do this, I will definitely spend extra time to correct my program.
> However, if I can do it quicker, then I could spend that extra time
> studying for your test (much preferred).
> Sincerely,
> <<name withheld to protect a student I am concerned about>>

I leave it up to you to decide whether you do it quicker or whether you 
correct your program.  Some of our test cases will start with garbage in 
the registers so if you don't set to 0 those you need to be 0, the program 
will produce nonsense for those test cases, and it will affect your results, 
and consequently your grade on that program.

I do understand that you would rather spend all that extra time studying for 
the midterm, rather than correcting the program.  I would be interested in 
knowing how much extra time it takes, since you already said you would 
definitely correct your program.

Good luck completing the program and studying for the midterm both.

Yale Patt