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],
strb r8, [a1],
subs r4, r4,
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,
countLoop:
ldrb r5, [r6],
cmp r5,
add r4, r4,
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[] ) ;
int main( int argc, char * argv[] )
{
char str[] = "abcd" ;
char * result;
result = copy( str ) ;
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!
source
share