如何在.stringz(LC3)中更改一个字符

发布于 2025-01-22 14:41:49 字数 679 浏览 0 评论 0原文

我正在寻找一种在LC3中的Stringz内更改单数字符的方法。

例如,如果我们有:

SUBR
    LEA R0, STR ;Loading string into R0
    ADD R6, R0, A_31 ;Seeing if its above/below '1'
    BRn ND
    ADD R6, R0, A_39 ;Seeing if its above/below '9'
    BRP ND

    ;
    ;Some code that would change the current 'num' to ascii 0
    ;
    
ND  RET

A_31 .fill -31 ;Ascii -'1'
A_39 .fill -39 ;Ascii -'9'
.blkw 100
STR .stringz "aBcDeFg12345abC?-"
    .end

现在,这有点伪代码,因为我不确定如何实际做我想做的事(我知道我不能只是lea string string,然后进行添加进行检查)。在此示例中,我希望能够读取字符串中的每个字符,并将其更改为零,如果它还不是1-9的ASCII值。

我有一些想法是,它需要以某种方式使用LDR和Str以某种方式通过内存地址进行递增(即在检查每个字符时在两个寄存器之间不来回交换),但我真的不确定如何实际上做到这一点。

任何帮助都非常感谢3; 3

I'm looking for a way to change singular characters inside a stringz in the LC3.

For example if we have:

SUBR
    LEA R0, STR ;Loading string into R0
    ADD R6, R0, A_31 ;Seeing if its above/below '1'
    BRn ND
    ADD R6, R0, A_39 ;Seeing if its above/below '9'
    BRP ND

    ;
    ;Some code that would change the current 'num' to ascii 0
    ;
    
ND  RET

A_31 .fill -31 ;Ascii -'1'
A_39 .fill -39 ;Ascii -'9'
.blkw 100
STR .stringz "aBcDeFg12345abC?-"
    .end

Now, this is somewhat pseudocode because I'm not sure how to actually do what I want(I understand I can't just LEA the string and then do the ADD to check). In this example, I'd like to be able to read each character in the string, and change it to a zero if it isn't already the ASCII value for a number 1-9.

I have some ideas that it needs to use LDR and STR somehow to increment through the memory addresses while changing the chars in-place (i.e. not swapping back and forth between two registers while checking each char), but I'm really not sure how to do it practically.

Any help is greatly appreciated <3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

如若梦似彩虹 2025-01-29 14:41:49

您需要一种使用指针搜索数组元素的算法。

通常是用一个循环完成的,该循环一次提取一个元素,检查该元素的素质,然后移至下一个元素。

您拥有的LEA用于初始化CPU寄存器作为指针以参考标签。

该指针只是一个数字,但是一旦拥有它,您就可以(1)将其放置以获取元素,(2)将其递增以参考字符串的下一个字节。

LDR和STR完成了删除。&nbsp;由于指针只是一个数字,因此常规添加可以推进它。

让我们看一下计算最大值的示例循环:

        LEA R0, ARRAY      // pointer variable
        AND R1, R1, #0     // current max, clear to zero (good for numbers >= 0)

LOOP,   LDR R2, R0, #0     // fetch element from array
        NOT R3, R2         // negate via two's complement (part 1)
        ADD R3, R3, #1     // negate via two's complement (part 2)
        ADD R3, R1, R3     // compare the current max with element
        BRp SKIP           // is current max larger than the array element?
        ADD R1, R2, #0     // yes: capture new max (no: skip this)
SKIP,   ADD R0, R0, #1     // either way: continue on and increment pointer
        BR LOOP

未显示的是如何退出此类循环(当达到数组的末端到达阵列并因此处理了所有元素时),因为这不是您问题的一部分。

You want an algorithm that uses a pointer to search the array elements for certain qualities.

That's usually done with a loop that extracts one element at a time, checks the qualities of that element, and then moves on to the next.

The LEA you have is used for initializing a CPU register as pointer to refer to a label.

That pointer is just a number, but once you've have it, you can (1) dereference it to get an element, (2) increment it to refer to the next byte of the string.

LDR and STR accomplish dereferencing.  Since a pointer is just a number, regular addition can advance it.

Let's look at a sample loop that computes max:

        LEA R0, ARRAY      // pointer variable
        AND R1, R1, #0     // current max, clear to zero (good for numbers >= 0)

LOOP,   LDR R2, R0, #0     // fetch element from array
        NOT R3, R2         // negate via two's complement (part 1)
        ADD R3, R3, #1     // negate via two's complement (part 2)
        ADD R3, R1, R3     // compare the current max with element
        BRp SKIP           // is current max larger than the array element?
        ADD R1, R2, #0     // yes: capture new max (no: skip this)
SKIP,   ADD R0, R0, #1     // either way: continue on and increment pointer
        BR LOOP

Not shown is how to exit such loop (when the end of the array is reached and thus all element have been processed) since that was not part of your question.

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