user1773469
user1773469

Reputation: 27

ARM assembly - code to replace character on a string

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...

Upvotes: 1

Views: 10422

Answers (2)

Pete Fordham
Pete Fordham

Reputation: 2343

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]

Upvotes: 1

Leo
Leo

Reputation: 2388

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

Upvotes: 1

Related Questions