C 在汇编中,如何从调用函数中获取值?
这是我的MSP430的C代码。我正在尝试写C 在汇编中,如何从调用函数中获取值?,c,assembly,calling-convention,msp430,C,Assembly,Calling Convention,Msp430,这是我的MSP430的C代码。我正在尝试写str=word\u start(str)在汇编中,但我不确定这是否正确 char** tokenize(char* str){ int totalWords = count_words(str); printf("%d\n", totalWords); char **array; array = (char **)malloc(sizeof(char*) * (++totalWords));
str=word\u start(str)代码>在汇编中,但我不确定这是否正确
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;
}
当我在汇编中调用word\u start
时,我传递值str
的方式是否正确?如果没有,您能告诉我如何将汇编中的变量传递给函数并从该函数中获取返回值吗?这可能取决于您使用的C编译器。看,例如@Michael,我想我当时是对的,我所经历的似乎是R15-R12中曾经发生过的事情,因为我把价值放在R12中,所以我需要经历。调用字_start的值在R12中给出,返回值在R12中。@ErikEidt为什么在您提供的链接的装配零件的第3行中,我们要从R1中减去#10?R1
是堆栈指针,因此它为局部变量使用了一些堆栈空间。请看这里的第18页
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