Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

simulator - Why isn't my assembly program setting r1 to the correct value?

I am writing an assembly program on the LC3 machine.

My assembly program is an LC3 program that multiplies R2 and R3 and stores the result in R1.

Here is my source code(with comments)

;Sets pc to this address at start of program 
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them 
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone 
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
HALT
.END

My test case is multiplying 4 * 3. The result should be 12 and that should be stored in R1. However when I run this program in my LC3 simulator, this is what i get for the outputenter image description here

R3 holds the correct value at the end but R1 holds -1.... Does anyone see an issue with my code? I made sure to clear R1 at the beginning and to keep adding R3 to R1 and storing the result to R1 while the counter, R3, or 3 in this case is greater than zero.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

HALT is just a "pseudo-instruction" for a TRAP instruction used to halt the machine.

You could write:

TRAP x25  ;HALT the machine

But in this way you need to remember the position in the TRAP vector, in this case x25. So is better to just use HALT instead.

Others common TRAPs also have pseduo-instructions: IN, OUT, etc.

I asume you want to store your results somewhere. You could do something like:

;Sets pc to this address at start of program 
.ORIG x3000
;R1 will store the result lets clear it(ANd with 0)
AND R1,R1,x0
;R2 will be multiplied by R3, let's clear both of them 
AND R2,R2,x0
AND R3,R3,x0
;Test case 4 * 3 = 12;
ADD R2,R2,4
ADD R3,R3,3
;Add to increment zone 
LOOP Add R1,R1,R2;
;Decrement the counter, in this case the 3 or R3
ADD R3,R3,x-1
BrP LOOP
ST R1, Result         ;STORE R1 at Result
HALT
Result .FILL x0000    ;this will be x000C=12h after execution
.END

---------------------EDIT--------------------------

About you last question (in comments):

If HALT stops my program, how will Reslt .FILL x0000 directive run then?

This is more a question about how assemblers works.

The answer is because: Assembly Time != Execution Time

Directives are considered at Assembly Time.

In fact, Assembly Time is composed in two passes:

  1. Resolve symbols creating a symbol table
  2. Convert instructions to "truly executable/machine code" using the symbol table.

This is a very common way to implement assemblers, and the LC3 assembler is not the exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...