Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an ARM assembly function that replaces a specific character in a string wi

ID: 3673734 • Letter: W

Question

Write an ARM assembly function that replaces a specific character in a string with another and counts the number of times the replacement occurs.

The function’s signature is:

   int replace( char * str, char lr, char rp )

       where   char * str is the pointer to the character string,

           char lr is the char that needs replacing, and

           char rp is the replacement character.

The C language program is:

#include <stdio.h>

#include <stdlib.h>

extern int replace( char * s, char lr, char rp ) ;

int main( int argc, char * argv[] )

{

   int result ;

   char lr = 'e' ;

   char rp = 'X' ;

   char str[] = "easy to replace" ;

  

   result = replace( str, lr, rp ) ;

   printf( "%d changes, %s ", result, str ) ;

  

   exit( 0 ) ;

}

On entry to the function register a1 will contain a pointer to the first character of the string, a2 will have the character to be replaced and a3 will have the replacement character.

Explanation / Answer

.global subs

subs:   
stmfd sp!, {v1-v6, lr} //string entry
mov v1, #0 //setting index of char in string to 0
mov v2, #0 // setting replacement count to 0

loop :
ldrb v3, [a1,v1] // get char at index v1 of string a1
cmp a2,v3 //compare char if its the same to the one that has to be replaced
streqb a3, [a1, v1] // store a3 in string a1 at index v1
addeq v2, v2, #1 //increment replacement 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote