cassemblysegmentation-faultarm7

ARM7 Raspberry pi Assembly Segmentation fault after copying string


Here is my ARM7 assembly snippet

.global strCopy
.text
strCopy:

strCopyloop:    
    LDRB R2, [R1], #1
    STRB R2, [R0], #1

    CMP R2, #0
    BNE strCopyloop

    Bx LR

Here is the C file that is using this function

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

extern void strCopy(char* strTo, const char* strFrom);
int main(){

        const char* str1 ="This one";
        char* str2;


        strCopy(str2,str1);


        return 0;
}

I cant for the life of me figure out why its giving me a segmentation fault.


Solution

  • You need to create space for str2

    This can be done using the malloc function.

    str2= char* malloc(strlen(str1)+1)

    You add the 1 for null character at the end of the string. This shows the end of the string. You can now go ahead and use the strcpy function to Scipy the 2 strings.