在程序集 LC-3 中保存一串字符
我正在尝试使用 LC-3 架构制作一个简单的程序。 我想做的就是从控制台读取一个字符串,以某种方式将其保存在内存中,然后将其打印回来。
这就是我到目前为止的
; This program attemps to read a string and then output it
.orig x3000
and r1,r1,0
lea r0,prompt
puts
loop:
getc
putc
add r4,r4,1
ld r7,nlcomp ; check for
add r7,r7,r0 ; end of line
brz finish
st r0,lets
br loop
finish:
lea r0,lets
puts
halt
lets: .blkw 20
prompt: .stringz "Emter String"
nlcomp .fill xfff6
.end
输出仅显示字符串中的最后一个字符。如果我输入“steve”,它会打印出“e”
显然我的问题是我需要以某种方式将我读入的每个字符保存到它自己的内存位置。我认为使用 .blkw 可以做到这一点,但显然它所做的一切都会覆盖该位置的位。
我的问题是如何将字符存储在连续的内存位置,然后将它们打印到控制台?
I am trying to make a simple program using the LC-3 Architecture.
All I am trying to do is read a string from the console, somehow save it in memory, and then print it back out.
This is what I have so Far
; This program attemps to read a string and then output it
.orig x3000
and r1,r1,0
lea r0,prompt
puts
loop:
getc
putc
add r4,r4,1
ld r7,nlcomp ; check for
add r7,r7,r0 ; end of line
brz finish
st r0,lets
br loop
finish:
lea r0,lets
puts
halt
lets: .blkw 20
prompt: .stringz "Emter String"
nlcomp .fill xfff6
.end
The output displays only the last char in the string. If I was to enter "steve" it would print out "e"
Obviously my problem is that I need to somehow save each char I read in, into its own memory location. I thought using the .blkw would do this, but apparently all it does it overwrite the bits that are in that position.
MY question is how do I store chars in sequential memory locations and then print them out to the console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 STR 指令,它允许您进行基址偏移寻址。 STR 的语法是:
因此,类似以下内容是有效的:
You need to use the STR instruction, which allows you to do base-offset addressing. The syntax for STR is:
So, something like the following would be valid:
我根本不熟悉 LC-3,但是当我通读你的代码时,我看到的一件事是你总是存储到“let”指向的地址,而不是“let”加上指向的地址一个偏移量。由于这被标记为家庭作业,我会让你弄清楚如何做到这一点! :)
I'm not familiar with LC-3 at all, but when I read through your code one thing I see is that you're always storing to the address pointed to by 'lets' and not the address pointed to by 'lets' plus an offset. Since this is marked as homework I'll let you figure out how to do that! :)