Hi, I have just started using MIPS assembly language, and I am trying to convert a piece of C code to MIPS. The C code is as follows:
This code finds permutations as calculated by n!/(n-k)!
The MIPS code I have written has several errors that I cannot figure out:
The errors I get are:
Exception occurred at PC=0x00000000
Bad address in text read: 0x00000000
Attempt to execute non-instruction at 0x80000180
I don't understand what these mean. Any help is greatly appreciated!
int fact(int n){
if(n<1) return 1;
else return n * fact(n-1);
}
int perm(int n, int k)
{
return fact(n)/fact(n-k);//calls another procedure
}
int main(){
int n = 10, k=5;
printf(%d\n, perm(n,k));
return 1;
}
This code finds permutations as calculated by n!/(n-k)!
The MIPS code I have written has several errors that I cannot figure out:
.text .globl main main: ori $t1, $0, 0x10 #initialize n = 10 ori $t2, $0, 0x5 #initialize k = 5 ori $t3, $0, 0x0 #initizlize temp = 0 jal perm #call perm(n, k) move $t3, $v0 #save result to temp li $t3, 4 #specify print value syscall #print fact: addi $sp, $sp, -8 #allocate bytes sw $ra, 0($sp) #save return address sw $a0, 4($sp) #save argument slti $t0, $a0, 1 #test for n < 1 beq $t0, $0, L1 #if n >= 1, goto L1 addi $v0, $0, 1 #if n < 1, return 1 addi $sp, $sp, 8 #deallocate bytes jr $ra #return L1: addi $a0, $a0, -1 #decrement n jal fact #call fact (n) lw $ra, 0($sp) #load return address lw $a0, 4($sp) #load argument addi $sp, $sp, 8 #deallocate bytes mul $v0, $a0, $v0 #multiply n, n - 1 jr $ra #return perm: sub $sp, $sp, 16 #allocate bytes sw $ra, 0($sp) #save return address sw $s0, 4($sp) #save temp register sw $a0, 8($sp) #save arg0 (n) sw $a1, 12($sp) #save arg1 (k) jal fact #call fact(n) move $s0, $v0 #save result to temp lw $a0, 8($sp) #load arg0 (n) lw $a1, 12($sp) #load arg1 (k) sub $a0, $a0, $a1 #n - k jal fact #fact(n - k) div $s0, $s0, $v0 #fact(n)/fact(n - k) move $v0, $s0 #save temp to result lw $ra, 0($sp) #load return address lw $s0, 4($sp) #load temp register addi $sp, $sp, 16 #deallocate bytes jr $ra #return
The errors I get are:
Exception occurred at PC=0x00000000
Bad address in text read: 0x00000000
Attempt to execute non-instruction at 0x80000180
I don't understand what these mean. Any help is greatly appreciated!