2014-03-27 271 views
0

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

例如:

 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 - 装配程序在内存中反转字符串,请大家帮忙。

回答

0

也许你应该编写内部循环到字符串的一半并交换字符? 来自我的那个从一开始就为我的地方从最后? 结束NUL保持原样。 如果字符数是奇数,则中间的字符保持原样。 如果您将字符串长度除以2(右移一位),您将得到循环的字符数。 提醒是0或1(其中花瓶多余的一个id为奇数字符串的中间字符)。 所以你换了str [i]和str [last-i](通过临时存储),其中'last'是字符串长度 - 1. 通常在组装中,向后循环更容易:以len/2开始并递减直到索引为零(结束条件以及条件跳转到循环开始)。