LC-3从.blkw输出字符串
我正在处理的程序中只需在存储器中存储的16位字符串,该字符串在存储器上标记为模式
,并计算字符串中的1s数,并将其值存储在num_ones < /代码>。我需要在屏幕上打印16位字符串,但我坚持如何完成此操作。
我将16位字符串存储在.blkw
中。我尝试将模式
的地址加载到R0中,并使用puts
,但这并没有为我打印出任何东西。
.orig x3100
; PROMPTING
LEA R0, PROMPT_STRING
PUTS
LEA R1, PATTERN ; saves the address of the memory
loop
GETC
OUT
ADD R3, R0, x0 ;
LD R6, DIFF ;
ADD R3,R3,R6 ;
STR R3, R1, #0 ;
ADD R1, R1, #1 ;
ADD R5, R0, #-10 ;
BRZ countOneStart
BR loop
countOneStart
ADD R2 R2 #15 ; Initialize loop count for CountDigits
AND R7,R7,#0 ; Clear R7
LEA R0, PATTERN ; Load Memory Space address into R0
LDR R3,R0,#0 ; Load contents of PATTERN into R3
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits ; if zero go to CountDigits
addOne
ADD R7,R7,#1 ; Increment R7 to keep count
BR CountDigits
CountDigits
ADD R2 R2 #-1 ;
BRN EndProgram
ADD R0,R0,#1 ; Increment address
LDR R3,R0,#0 ; Load contents
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits
EndProgram
ST R7, NUM_ONES
LEA R0 , PATTERN;
AND R7,R7,#0 ; Clear R7
STR R7,R0,#0 ; store Null at end of array
LEA R0, PATTERN ; store addr in R0
TRAP x22 ; display output from memory address from R0 untill it founds Null
HALT
PROMPT_STRING .STRINGZ "Please enter a 16-character string consisting of only 0s and 1s: "
PATTERN .blkw #17 ; declares empty space to store the string
DIFF .fill #-48
NUM_ONES
.END
The program I am working on takes a 16-digit string of only 1s and 0s and stores that in memory labeled PATTERN
and counts the number of 1s in the string and stores that value in NUM_ONES
. I need to print the 16-digit string on the screen but am stuck on how to accomplish this.
I am storing the 16-digit string in a .BLKW
. I tried loading the address of PATTERN
into R0 and using PUTS
but that does not print anything out for me.
.orig x3100
; PROMPTING
LEA R0, PROMPT_STRING
PUTS
LEA R1, PATTERN ; saves the address of the memory
loop
GETC
OUT
ADD R3, R0, x0 ;
LD R6, DIFF ;
ADD R3,R3,R6 ;
STR R3, R1, #0 ;
ADD R1, R1, #1 ;
ADD R5, R0, #-10 ;
BRZ countOneStart
BR loop
countOneStart
ADD R2 R2 #15 ; Initialize loop count for CountDigits
AND R7,R7,#0 ; Clear R7
LEA R0, PATTERN ; Load Memory Space address into R0
LDR R3,R0,#0 ; Load contents of PATTERN into R3
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits ; if zero go to CountDigits
addOne
ADD R7,R7,#1 ; Increment R7 to keep count
BR CountDigits
CountDigits
ADD R2 R2 #-1 ;
BRN EndProgram
ADD R0,R0,#1 ; Increment address
LDR R3,R0,#0 ; Load contents
AND R4,R3,#1 ; Check if digit is 1 or 0
BRP addOne ; if positive go to addOne
BRZ CountDigits
EndProgram
ST R7, NUM_ONES
LEA R0 , PATTERN;
AND R7,R7,#0 ; Clear R7
STR R7,R0,#0 ; store Null at end of array
LEA R0, PATTERN ; store addr in R0
TRAP x22 ; display output from memory address from R0 untill it founds Null
HALT
PROMPT_STRING .STRINGZ "Please enter a 16-character string consisting of only 0s and 1s: "
PATTERN .blkw #17 ; declares empty space to store the string
DIFF .fill #-48
NUM_ONES
.END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的算法看起来还不错,但是您错过了一些细节。
如果您不知道调试,现在是时候学习了。&nbsp;您应该能够单步调试。&nbsp;在单步中,这个想法是确定您期望发生的事情与实际发生的事情之间的区别。&nbsp;我们验证每个说明的执行,因为如果任何一个指令是错误的,则整个程序将无法正常工作!
这是我所做的:跳过您描述为有问题的领域,我在
endprogram
。 R7中的1个计数似乎正确。&nbsp;现在,查看数据存储器中的字符串。&nbsp;您可以在地址X3166上看到它,并且还可以看到它是由数字1和0组成的数字值的序列 - 解释为字符打印,这些是控制字符,并且不会使用任何一个putc正确打印
或puts
。&nbsp;您需要ASCII字符来打印。接下来,代码nul终止了“字符串”,但这是错误的,因为它没有使NUL字符处于正确的位置。&nbsp;您会查看nul终止后是否立即查看字符串。&nbsp;尽管该错误不是由ASCII字符组成的事实,但其中许多NUL已经在其中,而NUL的0覆盖却不会真正显示出来。
您必须根据他们希望该程序执行的操作来决定是否使用
puts
用ASCII字符(在模式
或复制到其他地方),或要使用putc
的循环,其中将数字从模式
转换为ASCII以进行输出。Your algorithm looks ok, but you have missed some details.
If you don't know about debugging, now is the time to learn. You should be able to single step debug this. During single step, the idea is to determine the difference between what you expect to happen and what actually happened. We verify the execution of each instruction, because if any one instruction is wrong, the whole program won't work!
Here's what I did: skipping ahead a bit to the area you described as problematic, I put a breakpoint at
EndProgram
. The count of 1's in R7 appears correct. Now, look at the string in data memory. You can see it at address x3166, and also you can see that it is a sequence of numeric values consisting of the numbers 1 and 0 — interpreted for character printing, those are control characters and won't print properly using eitherputc
orputs
. You need ascii characters for printing.Next the code nul-terminates the "string", but that is in error, since it doesn't put the nul character at the correct position. You'll see if you look at the string immediately after the nul termination. Though this error is hiding by the fact that the string is not composed of ascii characters, so has many nul's already therein, and a 0 overwritten with a nul won't really show.
You'll have to decide, based on what they want the program to do, whether to use
puts
with ascii characters (there atPATTERN
or copied to somewhere else), or to use a loop withputc
where you convert numbers fromPATTERN
to ascii for output.