Loops 如何编写打印出“;你好,世界”;,在LC3中使用循环5次?
这是我到目前为止只写一次名称的代码,我不知道如何在代码中实现循环,以便名称显示5次 实现这一点的最佳方法是使用等效的for循环。我们的循环限制变量必须使用2的补码进行反转,这给了我们-5。然后我们将循环计数添加到-5,看看它们是否等于0。如果为零,则跳出for循环Loops 如何编写打印出“;你好,世界”;,在LC3中使用循环5次?,loops,lc3,Loops,Lc3,这是我到目前为止只写一次名称的代码,我不知道如何在代码中实现循环,以便名称显示5次 实现这一点的最佳方法是使用等效的for循环。我们的循环限制变量必须使用2的补码进行反转,这给了我们-5。然后我们将循环计数添加到-5,看看它们是否等于0。如果为零,则跳出for循环 我认为在LC3机器上使用do while循环要容易得多。而且编码也少了很多 .ORIG x3000 AND R1, R1, #0 ; clear R1, R1 is our loop count LD R2, LI
我认为在LC3机器上使用do while循环要容易得多。而且编码也少了很多
.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
我将计数器设置为6的原因是,它实际上在循环实际开始之前递减,所以当循环开始时,它实际上是5。我这样做的原因是因为BR指令绑定到了您处理的最后一个寄存器。如果要将其设置为0,只需将具有ADD R1,R1,#6的行更改为
加上R1,R1,#5。把循环换成这个
.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
希望这有帮助 打印Hello World!使用循环5次:
LOOP LEA R0, HELLO_WORLD
ADD R1, R1, #0
BRZ DONE
PUTS
ADD R1, R1, #-1
BR LOOP
祝你的作业顺利,学习LC-3汇编语言D
LOOP LEA R0, HELLO_WORLD
ADD R1, R1, #0
BRZ DONE
PUTS
ADD R1, R1, #-1
BR 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