ARM assembly - code to replace character on a string

﹥>﹥吖頭↗ 提交于 2019-12-11 06:48:33

问题


I have this C driver program

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

I have to make an arm program that changes the 'e' on the String to 'x', so far this is what I have \

.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

This is giving me an infinite loop though and I'm stuck, can anyone help me figure out where the problem is??


So this is what i have so far,

.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

it runs but the character is never changed..., the output and the end is the same one as the input, for some reason, the a2 registry is null...


回答1:


There are a couple of problems with your code. Is this for homework?

mov v3, a3 overwrites v3 with the new character unconditionally and is never written back to memory (like Pete Fordham noted). Since you also use v3 when checking for the end of the string, you will never terminate the loop since you just changed v3 into a3. You are also setting a return value for a function which is declared as void which will give problems if you ever attempt to use the return value in the C-code.

I would probably do something like this instead (untested but should give you some ideas):

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



回答2:


This line is completely wrong

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

This should be a conditional store back into the string.

streqb a3, [a1, v1]


来源:https://stackoverflow.com/questions/13257266/arm-assembly-code-to-replace-character-on-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!