In Assembly how would I get the value from a call function?

Viewed 115

This my C code for my MSP430. I am trying to write str = word_start(str); in assembly but I am not sure if this is correct.

char** tokenize(char* str){
    int totalWords = count_words(str);
    printf("%d\n", totalWords);
    char **array;
    array = (char **)malloc(sizeof(char*) * (++totalWords));

    //filling the array with individual words
    int diff = 0;
    int i;
    for(i = 0; i < totalWords-1; i++){
        str = word_start(str);
        // find difference in length
        diff = word_terminator(str) - str;
        // add new allocated string to array
        array[i] = copy_str(str, diff);
        // update pointer p to next word
        str = word_terminator(str);
        
    }
    array[i] = '\0';
    return array;
}

This is my assembly code, I want to convert the above method from C to MSP430 assembly.

 tokenize:
    mov r12, 0(r1) ;    put str in str
    
    call #count_words
    mov r12 2(r1) ;     
    mov 2(r1), r12 ;    get totalWords
    
    
    add #1, r12 totalword
    add r12, r12
    call #malloc
    mov r12 4(r1)

    mov #0 6(r1) ;      int i = 0;
    mov #0 8(r1) ;      int diff = 0;

top: cmp 0(r1), 6(r1)
    JL end
    mov 0(r1), r12
    call #word_start    ; calling word start
    mov r12, 0(r1)      ; getting the value returned 
    mov 0(r1), r12    

    call #word_terminator
    mov r12, 8(r1)
    mov 0(r1), r12
    mov 8(r1), r13
    sub r12, r13
    
    call #copy_str
    mov r12, 10(r1) ;   what we get from cpoy_str put in r12
    mov 6(r1), r12 ;    put I in r12
    add r12, r12 ;       add r12
    add 4(r1), r12 ;    add 4(r1) to whats in r12
    mov 10(r1), @r12 ;  put what we r12 is in 10(r1) 

    mov 0(r1), r12
    call #word_terminator
    move r12, 0(r1)
    mov 0(r1), r12
    add #1, 6(r1) ;     increment i

end:
    mov 6(r1), r12
    add r12, r12
    add 4(r1), r12
    mov #0, @r12,
    add #12, r1
    ret

When I call word_start in assembly, is the way I am passing the value str and getting the value returned done correctly? If not can you show me how you're supposed to pass in variables in assembly to a function and get the returned value from that function?

0 Answers
Related