在程序集 LC-3 中保存一串字符

发布于 2024-12-19 02:48:28 字数 723 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-12-26 02:48:28

您需要使用 STR 指令,它允许您进行基址偏移寻址。 STR 的语法是:

STR <src register> <base register> <immediate offset>

因此,类似以下内容是有效的:

    LEA R1,MEMORYSPACE ; saves the address of the storage memory block
loop:
    GETC               ; input character -> r0
    PUTC               ; r0 -> console
    STR R0,R1,#0       ; r0 -> ( memory address stored in r1 + 0 )
    ADD R1,R1,#1       ; increments the memory pointer so that it
                       ; always points at the next available block
    BR loop

MEMORYSPACE .blkw 100  ; declares empty space to store the string

You need to use the STR instruction, which allows you to do base-offset addressing. The syntax for STR is:

STR <src register> <base register> <immediate offset>

So, something like the following would be valid:

    LEA R1,MEMORYSPACE ; saves the address of the storage memory block
loop:
    GETC               ; input character -> r0
    PUTC               ; r0 -> console
    STR R0,R1,#0       ; r0 -> ( memory address stored in r1 + 0 )
    ADD R1,R1,#1       ; increments the memory pointer so that it
                       ; always points at the next available block
    BR loop

MEMORYSPACE .blkw 100  ; declares empty space to store the string
黄昏下泛黄的笔记 2024-12-26 02:48:28

我根本不熟悉 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! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文