2012-11-06 52 views
0

我有這個C驅動程序ARM彙編 - 代碼在一個字符串

#include <stdlib.h> 
#include <stdio.h> 

extern void subs(char *string, char this_c, char that_cr) ; 

int main(int argc, char *argv[]) 
{ 
    char this_c= 'e' ; 
    char that_c = 'X' ; 
    char orgstr[] = "sir sid easily teases sea sick seals" ; 

    subs(orgstr, this_c, that_c) ; 
    printf("Changed string: %s\n", orgstr) ; 

    exit(0) ; 
} 

我必須做的一個ARM程序改變的字符串「X」和「e」替換字符,到目前爲止,這是我有\

.global subs 

subs: 
    stmfd sp!, {v1-v6, lr} //string entry 
    mov v1, #0 //set index to 0 
    mov v2, #0 // set count to 0 


loop : 
    ldrb v3, [a1,v1] // get the first char 
    cmp  a2,v3 //compare char if its the same to the one that has to b chang 
    mov v3, a3 // change the character for the new character 
    addeq v2, v2, #1 //increment count 
    add  v1, v1, #1 // increment index 
    cmp v3,#0 //end of string 
    bne  loop 
    mov  a1,v2 //return value 

    ldmfd sp!, {v1-v6, pc} 
.end 

雖然這給了我一個無限循環,我卡住了,誰能幫我找出問題出在哪裏?


因此,這是我到目前爲止,

.global subs 

subs: 
    stmfd sp!, {v1-v6, lr} //string entry 
    mov v1, #0 //set index to 0 
    mov v2, #0 // set count to 0 


loop : 
    ldrb v3, [a1,v1] // get the first char 
    cmp  a2,v3 //compare char if its the same to the one that has to b chang 
    moveq v3, a3 // change the character for the new character 
    addeq v2, v2, #1 //increment count 
    add  v1, v1, #1 // increment index 
    cmp v3,#0 //end of string 
    bne  loop 
    mov  a1,v2 //return value 

    ldmfd sp!, {v1-v6, pc} 
.end 

它運行但性格從未改變......,輸出端與端是同一個作爲輸入,對於一些因此,A2註冊表是空...

回答

1

這條線是完全錯誤的

mov v3, a3 // change the character for the new character 

這應該是有條件存儲背進入字符串。

streqb a3, [a1, v1] 
+0

我改變了MOV的MOVEQ,現在它不給我一個無限循環了,但是串出來的只是像原來的那樣,沒有任何改變 – user1773469

+0

它需要是STR而不是mov。 MOV不會改變內存中的字符串,只是寄存器的內容。 –

+0

發生這種錯誤類型:OFFSET_IMM – user1773469

1

您的代碼有幾個問題。這是作業嗎?

mov v3, a3用新字符無條件覆蓋v3並且永遠不會回寫到內存中(如Pete Fordham指出的那樣)。由於您在檢查字符串結尾時也使用v3,因此您將不會終止循環,因爲您已將v3更改爲a3。您還要爲聲明爲void的函數設置返回值,如果您嘗試使用C代碼中的返回值,則會出現問題。

我可能會做這樣的事情,而不是(未經測試,但應該給你一些想法):

loop: 
    ldrb v3, [a1], #1 
    cmp a2, v3 
    strbeq a2, [a1, #-1] 
    cmp v3, #0 
    bne loop 
+1

mmm,這給了我一個想法,但在該循環中,你永遠不會改變字符到新char ...因爲X字符存儲在a3中,你從來沒有在那裏使用它,如果你我沒有看到它,我是新的手臂 – user1773469

+0

@Leo你應該在strb上做後增量來簡化事情。如果是ARMv7,你也可以使用CBNZ而不是CMP + BNE。 –

+0

@PeteFordham:由於strb是有條件的,我不能真正看到如何做後增量有效。關於CBNZ:感謝提示,沒有注意到他們已經爲此添加了指示。 – Leo