Copy string using ARM / C

so I'm trying to learn ARM and practice by taking a pointer to an array of characters from C, copying this line and returning a pointer to another array of characters. I wrote this code (commented that I assume I am doing this):

    .global copy                    @Let the linker know what going on

copy:                           @Start
    stmfd sp!, {v1-v6, lr}      @Push stuff onto stack
    mov r6, a1                  @Put the pointer to the original string in r6
    bl length                   @Get the length of the string
    mov a1, r4                  @Put length into the input parameter
    bl malloc                   @Allocate enough memory for our new string
    mov r9, a1                  @Move the first memory location to r9

loop:                           @Loop to copy string
    ldrb r8, [r6], #1           @Load first character from string and move pointer
    strb r8, [a1], #1           @Store character in new string and move character
    subs r4, r4, #1             @Subtract 1 from length
    bne loop                    @Stop looping if string is done
    mov a1, r9                  @Move the start of the new string to the return value
    b ending                    @Go to the ending


length:                         @Length function
    mov r4, #0                  @counter set to 0
countLoop: 
    ldrb r5, [r6], #1           @Load first character
    cmp r5, #0                  @Check for null character
    add r4, r4, #1              @Add 1 to the length
    bne countLoop               @Loop if we're not at the end
    mov pc, lr                  @Return the program

ending:
    ldmfd sp!, {v1-v6, pc}      @Pop stuff off the stack
.end

With this C driver:

#include <stdlib.h>
extern char * copy( char str[] ) ; /* declare the assembly routine */
int main( int argc, char * argv[] )
{
   char str[] = "abcd" ;
   char * result;
   result = copy( str ) ; /* call the assembly language routine */
   printf("Will this work? %s", result);
   exit(0);
}

However, I save the result (null). Obviously, something is wrong in my thinking, but I do not know what it is. Any help would be appreciated!

+4
source share
2 answers

At the beginning, you moved the pointer to the original string on r6, however after that you immediately overwrite r6 in the length function. I would suggest either storing it elsewhere, or using a1 directly in the call to this function

+1
source

' length .

  • NUL
  • ,

:

length:                         @Length function
    mov r4, #1                  @Init counter (always have trailing null)
    mov r5, r6                  @Load ptr to first character

lengthLoop: 
    cmp [r5], #0                @Check for null character
    beq lengthEnd               @done

    add r4, r4, #1              @Add 1 to the length
    add r5, r5, #1              @step to next char in source
    b   lengthLoop              @Loop

lengthEnd:
    mov pc, lr                  @return via link register
0

Source: https://habr.com/ru/post/1688371/


All Articles