1

我有这个 C 驱动程序

#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 ) ;
}

我必须制作一个将字符串上的'e'更改为'x'的手臂程序,到目前为止这就是我所拥有的\

.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注册表为空......

4

2 回答 2

1

您的代码有几个问题。这是做功课吗?

mov v3, a3无条件地用新字符覆盖v3,并且永远不会写回内存(如 Pete Fordham 所说)。由于您v3在检查字符串结尾时也使用,因此您将永远不会终止循环,因为您刚刚更改v3a3. 您还为声明为的函数设置了返回值,void如果您尝试在 C 代码中使用返回值,则会出现问题。

我可能会做这样的事情(未经测试,但应该给你一些想法):

loop:
    ldrb v3, [a1], #1
    cmp a2, v3
    strbeq a2, [a1, #-1]
    cmp v3, #0
    bne loop
于 2012-11-08T15:20:53.257 回答
1

这条线是完全错误的

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

这应该是一个有条件的存储回字符串。

streqb a3, [a1, v1]
于 2012-11-06T19:04:43.450 回答