问题
.ORIG x3000
COUNTER .FILL x0005
LEA R0, HELLO_WORLD
PUTS
HALT
HELLO_WORLD .stringz "Hello World this is John Cena!"
.END
This is the code I have so far for just writing the name once, I'm confused how to implement the loop into this code so that the name will be displayed 5 times.
回答1:
Printing Hello World! 5 Times using a loop:
; +++ Intro to LC-3 Programming Environment +++
; Print "Hello World!" 5 times
; Use Loops to achieve the aforementioned output
; Execution Phase
.ORIG x3000
LEA R0, HELLO ; R0 = "Hello....!"
LD R1, COUNTER ; R1 = 5
LOOP TRAP x22 ; Print Hello World
ADD R1, R1, #-1 ; Decrement Counter
BRp LOOP ; Returns to LOOP label until Counter is 0 (nonpositive)
HALT
; Non-Exec. phase
HELLO .STRINGZ "Hello World!\n" ; \n = new line
COUNTER .fill #5 ; Counter = 5
.END ; End Program
Good Luck on your assignments and learning LC-3 Assembly Language! :D
回答2:
The best way to accomplish this is to use the equivalent of a for-loop. Our loop limit variable has to be inverted using 2's complement, this gives us -5. We then add our loop count to -5 to see if they equal 0. If zero, then jump out of the for-loop.
.ORIG x3000
AND R1, R1, #0 ; clear R1, R1 is our loop count
LD R2, LIMIT ; load our loop max limit into R2
NOT R2, R2 ; Invert the bits in R2
ADD R2, R2, #1 ; because of 2's compliment we have
; to add 1 to R2 to get -5
FOR_LOOP
ADD R3, R1, R2 ; Adding R1, and R2 to see if they'll
; will equal zero
BRz LOOP_END ; If R1+R2=0 then we've looped 5
; times and need to exit
LEA R0, HELLO ; load our string pointer into R0
PUTs ; Print out the string in R0
LD R0, NEWLINE ; load the value of the newline
PUTc ; print a newline char
ADD R1, R1, #1 ; add one to our loop counter
BRnzp FOR_LOOP ; loop again
LOOP_END
HALT ; Trap x25
; Stored values
LIMIT .FILL x05 ; loop limit = 5
NEWLINE .FILL x0A ; ASCII char for a newline
HELLO .STRINGZ "Hello World, this is NAME!"
.END
回答3:
I think using a do while loop is a lot easier on the LC3 machine. And a whole lot of less coding too.
.ORIG x3000
AND R1,R1,#0 ;making sure register 1 has 0 before we start.
ADD R1,R1,#6 ;setting our counter to 6, i will explain why in a sec
LOOP LEA R0, HELLO_WORLD
ADD R1,R1,#-1 ;the counter is decremented before we start with the loop
BRZ DONE ;break condition and the start of the next process
PUTS
BR LOOP ;going back to the start of the loop while counter !=0
DONE HALT ;next process starts here, stopping the program
HELLO_WORLD .STRINGZ "HELLO WORLD\n"
.END
The reason I set the counter to 6 is it actually gets decremented before the loop actually starts, so when the loop starts it's actually 5. The reason why I did it is because BR instruction is tied to the last register you fiddled with. If you want to set it to 0, just change the line that has ADD R1, R1, #6 into ADD R1, R1, #5. Change the loop into this.
LOOP LEA R0, HELLO_WORLD
ADD R1, R1, #0
BRZ DONE
PUTS
ADD R1, R1, #-1
BR LOOP
Hope this helps!
来源:https://stackoverflow.com/questions/33600724/how-do-i-write-a-program-that-prints-out-hello-world-5-times-using-a-loop-in