3

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
acebelowzero
  • 107
  • 1
  • 5
  • 1
    That may depend on which C compiler you're using. See e.g. https://www.ti.com/lit/an/slaa664/slaa664.pdf – Michael Aug 17 '20 at 17:17
  • @Michael well I think I got this right then, It seems what I passed through is what ever is in R15-R12 and since I am putting value I need to pass through in R12. the call word_start is given the value in R12 and the return value is placed in R12. – acebelowzero Aug 17 '20 at 17:38
  • 3
    https://godbolt.org/z/nKaK45 – Erik Eidt Aug 17 '20 at 17:42
  • @ErikEidt why in line 3 in the assembly part from link you provided do we subtract #10 from R1? – acebelowzero Aug 17 '20 at 18:50
  • 1
    `R1` is the stack pointer, so it is using some stack space for local variables. Have a look at page 18 here https://www.ti.com/lit/an/slaa534a/slaa534a.pdf – Erik Eidt Aug 17 '20 at 19:16
  • writing for mashines from 93 ? good luck finding somebody who can help you – clockw0rk Aug 17 '20 at 23:22
  • machines from 93? The msp430 is still a very active product line (you know how many 8051s and z80s were involved between the server that holds these bytes and your computer and web browser...very many...and they make the msp430 look young). – old_timer Aug 21 '20 at 06:57

0 Answers0