2014-03-27 118 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開始並遞減直到索引爲零(結束條件以及條件跳轉到循環開始)。