0

我正在编写一个基于 LC-3 汇编语言的 .asm 程序,该程序将遍历字符串列表,反转每个字符串,并将其存储回列表中的位置。

例如:

     STRINGS .STRINGZ "aabbb"
                      "bbcva"
                      "abcde"

该程序会将该列表翻转为“ bbbaa”、“ avcbb”和“ edcba”——因此,颠倒字符串但保持列表顺序。

我目前正在研究一个嵌套循环的想法,其中外循环将从一个字符串转到另一个字符串,而内循环将翻转它们,它正在踢我的屁股!我用 Java 编写了代码来做同样的事情,我花了 5 分钟,但出于某种原因,汇编只是在我的脑海里大便。有关如何执行此操作的任何指示?

到目前为止,这是我所拥有的,混合了伪和汇编:

    .ORIG x3000
    LEA R0, STRINGS            ; Load the address of the first char of the list of strings
    Loop until NOP is found, signaling end of the string.
    LEA R1, the address above  ; stores the address of the last char
    LDR R2, #0 Offset +1       ; load the first char to be flipped
    LDR R3, #0 Offset +2       ; load the last char to be flipped
    STR R3, #0 Offset +1       ; store the last char in the mem addr of the first
    STR R2, #0 Offset +2       ; store the first char in the addr of the last
    ADD R1, R1 + 1             ; increment the addr of the first char to move to the second
    ADD R2, R2 - 1             ; decrement the addr of the last char the move the second-to-last
    loop back to beginning somehow

而且我一点也不知道如何在字符串之间进行外循环。

TL;DR - 用于反转内存中字符串的汇编程序,请帮助。

4

1 回答 1

0

Maybe you should write inner loop that goes to the half of the string and swap the characters? The one from i places from the beginning with the one for i places from the end? The end NUL is left as is. If the number of characters is odd the middle one is left as is. If you divide the string length by 2 (shift right one bit) you'll get the number of characters to loop through. Reminder is either 0 or 1 (in which vase the excess one id the middle character of the string of odd number of characters). So you swap str[i] and str[last-i] (via a temporary store), where 'last' is the string length - 1. Often in assembly it's easier to loop backwards: start with len/2 and decrement until the index is zero (ending condition as well for conditional jump to loop beginning).

于 2014-03-28T23:03:08.657 回答