Saturday, October 24, 2009 3:52 AM,



This came from a student who admits that the question is not particularly 
relevant to what we are doing in class right now, but he is curious.

It is a good question, so I am copying it and my answer to you all.
HOWEVER, I would prefer you put it aside rather than read it now, unless you 
are done with the program and have spare time on your hands.




	 Dr Patt,
	 
	 This email has nothing whatsoever to do with the programming assignment; 
	 it's just a curious observation. I brought this issue up in a discussion 
	 group this morning and our conclusions on the topic were rather 
	 inconclusive.
	 
	 I was looking at some of the instructions we haven't discussed in class 
	 and I came across the JMP instruction. I've been pondering this concept 
	 with some confusion. What is the practical application of using JMP 
	 in assembly programs?
	 
	 Let's say I write an enormous program that's 400 instructions long in 
	 assembly. Let's also say I've written a function, called it "Quark," 
	 and that this function starts at the label "Quark" somewhere after the 
	 300th instruction in my program (I don't know exactly where). I'd like 
	 to go to this function on, say, the 20th instruction of my program. 
	 Once the program is loaded into memory and executed, the 20th instruction 
	 would need to set the PC to the location in memory where the label 
	 "Quark" is located. Here's my dilemma: I don't know where the label 
	 "Quark" is in memory.
	 
	 [Then the student conjectures a number of possible solutions that are very
	 tough to read through, so I will save you the task of doing that.  Needless
	 to say, none of them work, which is why he sent me the email.]
	 
	 <<name withheld to protect the student who is curious>>




Actually, the JMP instruction has lots of use in real programs.

I am disappointed in your reference to forward pointers.  You have 
been in class when we assembled programs, so surely you know that
the assembler makes two passes, one to pick up the labels, the other
to fill in the offsets.  That should take care of your BR QUARK.

As to jumping to somewhere in the full range of memory, JMP is most
useful in dealing with the CASE statement, which is part of many 
programming languages. With a CASE statement, you examine something 
and execute a different routine depending on the result of your 
examination.  A simple example to illustrate the point:

At some point in your program, you have 


	JMP R1


BASE	.FILL QUARK
	.FILL RUARK
	.FILL SUARK
	.FILL TUARK
	.FILL UUARK
 	.FILL VUARK
 	.FILL WUARK
 
and far, far away, we have labels QUARK, RUARK, etc.

In the first pass of the LC-3 assembler, the symbol table entries for
QUARK, RUARK, etc. are noted and in the second pass, the .FILLs are
specified in accordance with those symbol table entries.  So, no problem.

Now for its use.

Before we execute JMP R1, we examine some value (perhaps a code indicating
what to do).  Based on that value, we load an integer from 0 to 6 into R0.
From there, it is simply:


	LEA  R1, BASE
	ADD  R1,R1,R0
	LDR  R1,R1,#0
	JMP  R1

If we had loaded 0 into R0, we would jump to QUARK.
If we had loaded 1 into R0, we would jump to RUARK.
If we had loaded 2 into R0, we would jump to SUARK.
...

We call the six entries a "jump table," since each entry contains an
address to jump to, depending on some computation.  

Hope this helps.
Yale Patt