-2

For this problem we are given a problem in LC-3 and told to describe what it does and state what is contained in RESULT. Unfortunately, I am struggling hardcore with this language and I am really confused by it. I just don't understand what these different things are for. Here is the code.

        .ORIG x3000
        LD R2, ZERO
        LD R0, M0
        LD R1, M1
LOOP    BRz DONE
        ADD R2, R2, R0
        ADD R1, R1, -1
        BR LOOP
DONE    ST R2, RESULT
HALT
RESULT .FILL x0000
ZERO   .FILL x0000
MO     .FILL x0004
M1     .FILL x0803
       .END

I don't even know where to begin. Like, what is ".ORIG x3000"? I think x3000 is a memory location? If that's the case, then down at the bottom where it says ".FILL ..." aren't those memory locations too? Or are those numbers that you are filling into a register? I am confused.

So I THINK the second line loads whatever is in ZERO into register R2? Then M0 is loaded into R0, followed by M1 being loaded into R1? I am confused on what is being loaded. Are we loading values, or are we just placing memory addresses into registers? After all the LD instructions, we come to a loop. I think the BRz means to go back up an re-iterate through the loop as long as R1 is not 0? So, I guess R1 is our loop control element? R2 is being added to whatever is in R0 and the result is being stored in R2. After the loop ends, then RESULT is stored into R2?

What does DONE signal? I thought if you wanted to exit a loop, you would instruct the computer to HALT which is the line right after it. So why have DONE followed by HALT?

The last few lines I assume are initializations? Like, filling RESULT with x0000. What is x0000? Is it a memory location, or is a it a number?

Can someone help explain this to me and maybe guide me on what happens when this program is run?

I'm sorry if this is a lot at once. I am struggling big time and don't know where to even begin.

  • We could try to explain all this, but you should probably revisit your available material and consult with your teacher instead. (I assume you are a student.) Looks like you are missing very basic knowledge. – Jester Aug 26 '15 at 23:44
  • I am a student. I have read the chapter in the text book that this corresponds to probably over 6-7 times and I still don't understand a thing. I am going to consult with my professor, but I figured maybe getting some explanation before hand couldn't hurt. – generic user007 Aug 26 '15 at 23:47

1 Answers1

2

what is ".ORIG x3000"

Sets the origin of the program, that is where these instructions should go in memory. x3000 seems to be the usual starting address for LC3 programs.

where it says ".FILL ..." aren't those memory locations too? Or are those numbers that you are filling into a register? The last few lines I assume are initializations? Like, filling RESULT with x0000. What is x0000? Is it a memory location, or is a it a number?

.FILL is used to define initalized data. The number itself will be stored into memory.

the second line loads whatever is in ZERO into register R2

Yes, and since ZERO is a label for .FILL x0000 that will of course be zero.

Are we loading values, or are we just placing memory addresses into registers?

Loading values.

I think the BRz means to go back up an re-iterate through the loop as long as R1 is not 0? So, I guess R1 is our loop control element?

Actually BRz DONE means to go out to DONE when the previous result is zero, so in effect, yeah, loop until R1 becomes zero.

After the loop ends, then RESULT is stored into R2?

No, R2 is stored into RESULT.

What does DONE signal?

It's a label for exiting the loop. It's not an instruction, you could have called it anything. HALT on the other hand is an instruction, and it stops the program. Exiting a loop doesn't stop the program, it just stops repeating a block by continuing execution at a different location marked by a label.

Since the loop runs R1 times, and each time adds R0 to R2, it calculates R1 * R0, which due to the initializations is just x0803 * x0004.

Jester
  • 56,577
  • 4
  • 81
  • 125