I am trying to call a function declared in a C file from an assembly file, but I am getting a "Illegal Instruction" error.
我正在尝试调用从程序集文件中声明的C文件中的函数,但是我得到了一个“非法指令”错误。
My C code:
我的C代码:
#include <stdio.h>
int BubbleSort(int *v){
return 13;
}
int Run(int *, int *);
int main(){
int vetor[] = {1, 3, 5, 4, 10}, numTrocas = 0, res = 0;
numTrocas = Run(vetor, &res);
printf("numTrocas= %d\nf= %d\n", numTrocas, res);
return 0;
}
My Assembly code:
我的汇编代码:
.data
.text
.globl Run
Run:
addi $sp,$sp,-8
sw $ra,0($sp)
sw $a0,4($sp)
move $t4, $a1 #$t4 = $a1; address of variable res in C file
move $t6, $a0 #$t6 = address of the first vector element
lw $a0, ($t6)
add $t6, $t6, 4
lw $a1, ($t6)
add $t6, $t6, 4
lw $a2, ($t6)
add $t6, $t6, 4
lw $a3, ($t6)
add $t6, $t6, 4
lw $t3, ($t6)
jal PolyCalc
lw $a0,4($sp)
jal BubbleSort #-> Illegal Instruction
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
PolyCalc: #This function calculates the result of the expression 5(V[0] + V[1])(V[2] − 3xV[3]xV[4])
li $s0,5 #$s0 = 5
li $s1,3 #$s1 = 3
add $t1,$a0,$a1 #t1=(x1+x2)
mul $t1,$t1,$s0 #t1=5*(x1+x2)
mul $t2,$a3,$t3 #$t2 = x4*x5
mul $t2,$t2,$s1 #$t2 = 3*x4*x5
sub $t2,$a2,$t2 #$t2 = (x3-3x4*x5)
mul $t1,$t1,$t2
sw $t1, ($t4) #Save the result on the address of $t4
jr $ra
When I comment the line jal BubbleSort and add a random value to $v0 as a return of the BubbleSort function I stop getting that error and the program works fine.
当我注释行jal BubbleSort并将一个随机值添加到$v0作为BubbleSort函数的返回值时,我停止了那个错误,程序运行良好。
Can someone find the error in my code? Thanks
有人能找出我代码中的错误吗?谢谢
2 个解决方案
#1
2
For good measure, in your asm file, you should probably add:
为了更好地衡量,在asm文件中,您应该添加以下内容:
.extern BubbleSort
Then, the jal BubbleSort
should get relocated correctly.
然后,jal BubbleSort就应该被正确地重新定位。
But, the linker may place it too far away for the [limited] range of jal
, so you may have to replace it with:
但是,链接器可能会将它放置得离日航的(有限的)范围太远,因此您可能不得不将它替换为:
la $t0,BubbleSort
jalr $t0
#2
-1
The error you are experiencing is because the assembly code is not able to find the label called BubbleSort to jump. I think you should be using a different approach. You can embed assembly code in C using the asm function.
您正在经历的错误是由于程序集代码无法找到名为BubbleSort的标签来跳转。我认为你应该采用不同的方法。可以使用asm函数在C中嵌入汇编代码。
Something like:
喜欢的东西:
#include <stdio.h>
int BubbleSort(int *v){
asm("lw $a0, ($t6)"
"lw $a1, ($t6)");
}
int Run(int *, int *);
int main(){
int vetor[] = {1, 3, 5, 4, 10}, numTrocas = 0, res = 0;
numTrocas = Run(vetor, &res);
printf("numTrocas= %d\nf= %d\n", numTrocas, res);
return 0;
}
You can find more information about this function in the GCC documentation: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
您可以在GCC文档中找到关于这个函数的更多信息:https://gcc.gnu.org/onlinedocs/gcc/exten- asm.html
#1
2
For good measure, in your asm file, you should probably add:
为了更好地衡量,在asm文件中,您应该添加以下内容:
.extern BubbleSort
Then, the jal BubbleSort
should get relocated correctly.
然后,jal BubbleSort就应该被正确地重新定位。
But, the linker may place it too far away for the [limited] range of jal
, so you may have to replace it with:
但是,链接器可能会将它放置得离日航的(有限的)范围太远,因此您可能不得不将它替换为:
la $t0,BubbleSort
jalr $t0
#2
-1
The error you are experiencing is because the assembly code is not able to find the label called BubbleSort to jump. I think you should be using a different approach. You can embed assembly code in C using the asm function.
您正在经历的错误是由于程序集代码无法找到名为BubbleSort的标签来跳转。我认为你应该采用不同的方法。可以使用asm函数在C中嵌入汇编代码。
Something like:
喜欢的东西:
#include <stdio.h>
int BubbleSort(int *v){
asm("lw $a0, ($t6)"
"lw $a1, ($t6)");
}
int Run(int *, int *);
int main(){
int vetor[] = {1, 3, 5, 4, 10}, numTrocas = 0, res = 0;
numTrocas = Run(vetor, &res);
printf("numTrocas= %d\nf= %d\n", numTrocas, res);
return 0;
}
You can find more information about this function in the GCC documentation: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
您可以在GCC文档中找到关于这个函数的更多信息:https://gcc.gnu.org/onlinedocs/gcc/exten- asm.html