So i have to write a bubblesort programm in mips using QtSpim for a class but i get an Exception 4 [Adress error in inst/data fetch]. I have searched in other topics and use the .align 2 directive before defining an array for 5 integers but it still isn't getting fixed.
Here's the code:
.text
.globl main
main:
la $t1, array #sets the base adress of the array to t1
la $a0, in_prompt
li $v0, 4
syscall
li $t2, 0 #init to 1
read_loop:
beq $t2, 5, read_key #break if t2 = 5
la $a0, num_prompt
li $v0, 4
syscall #"Give number"
#move $a0, $t2
#li $v0, 1
#syscall #current number to be read
li $v0, 5 #read int. 5 times
syscall
sw $v0, ($t1) #move input from v0 to the array
addi $t1, $t1, 4 #move t1 to the next position in the array
addi $t2, $t2, 1 #increment counter (t2) by 1
j read_loop
read_key:
la $a0, search_q
li $v0, 4
syscall #print query prompt
li $v0, 5
syscall
move $t3, $v0 #number we're looking for (KEY)
move $a0, $t1 #array to pass as arguement
jal bubblesort
move $a0, $v0 #move into a0 the sorted array
move $a1, $t3 #move the KEY into a1 as arguement
jal binarysearch
move $t1, $a0 #move into t1 the array
move $t0, $a1 #move into t0 the KEY
move $t3, $v0 #move into t3 the result of binarySearch
beq $t3, -1, not_found #if key was not found
move $a0, $t0
li $v0, 1
syscall
la $a0, found_pr
li $v0, 4
syscall
j print_array
not_found:
move $a0, $t0
li $v0, 1
syscall
la $a0, not_found_pr
li $v0, 4
syscall
j print_array
print_array:
li $t2, 1 #init to 1
print_loop:
beq $t2, 5, EXIT
lw $a0, ($t1)
li $v0, 1
syscall
addi $t1, $t1, 4
addi $t2, $t2, 1
j print_loop
EXIT:
li $v0, 10
syscall
##############binarysearch#################
binarysearch:
addi $sp, $sp, -24 #reserve space for 6 elements
sw $a0, 0($sp) #push a0 (array)into stack
sw $a1, 4($sp) #push a1(KEY) into stack
sw $s0, 8($sp) #push s0
sw $s1, 12($sp) #push s1
sw $s2, 16($sp) #push s2
sw $ra, 20($sp) #push ra
sw $s3, 24($sp) #push s3
li $s0, 0 #low = 0
li $s1, 4 #high = 4
while:
bgt $s0, $s1, exit_search #if low > high branch
move $a1, $s0 #move into a1 low
move $a2, $s1 #move into a2 high
jal calc_middle #jump to calc_middle
move $s2, $v0 #move into s2 the return value of calc_middle
lw $a0, 0($sp) #restore into a0 the 1st stack el(array)
lw $a1, 4($sp) #restore into a1 the 2nd stack el(KEY)
add $a0, $a0, $s2 #move the array to middle
lw $s3, ($a0) #load into s3 the a[middle]
beq $a1, $s3, exit_search_found #break if KEY == a[middle]
blt $a1, $s3, less_t_middle #if the key is less than the middle element
addi $s0, $s2, 1 #if the key is greater than the middle element set new low
j while
less_t_middle:
addi $s1, $s2, -1 #new high
j while
exit_search_found:
move $v0, $s2 #return found
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
exit_search:
li $v0, -1 #return -1
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
##############calc_middle##################
calc_middle:
add $a1, $a1, $a2
sra $a1, $a1, 1
move $v0, $a1
jr $ra
##############bubblesort###################
bubblesort:
addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
li $s2, 4 #j = 5
li $s1, 0 #i = 0
outer_loop:
addi $s2, $s2, -1 #i = j - 1
blt $s2, $zero, exit_sort
inner_loop:
bgt $s1, $s2, outer_loop #if i > j - 1
lw $s3, 0($a0) #load into s3 the a[i]
lw $s4, 4($a0) #load into s4 the a[i+1]
bgt $s3, $s4, swap #if a[i] > a[i+1]
addi $s1, $s1, 1 #i++
j inner_loop
swap: move $s0, $s3 #tmp = a[i]
move $s3, $s4 #a[i] = a[i+1]
move $s4, $s0 #a[i+1] = tmp
addi $a0, $a0, 4 #point to the next element ????
addi $s1, $s1, 1 #i++
j inner_loop
exit_sort:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
addi $sp, $sp, 8
move $v0, $a0 #pass into v0 the sorted array
jr $ra
.data
.align 2
array: .space 20
in_prompt: .asciiz "Enter 5 numbers:\n"
num_prompt: .asciiz "Give number: "
search_q: .asciiz "what are you looking for?\n"
not_found_pr: .asciiz " not found in array: \n"
found_pr: .asciiz " found in array: \n"
Related
I was trying to write some MIPS code that simulates a C function, but it seems like I've come across an obstacle that I cant get over it.
int partition(int f, int l) {
int pivot = v[l];
int i = f;
for (int j = f; j < l; j++)
if(v[j] < pivot)
swap(i++,j);
swap(i, l);
return (i);
}
that was the function in C and this is what I have written so far in MIPS assembly language:
partition:
addi $sp, $sp, -8 #creates space for 2 words
sw $ra, 0($sp) #stores ra in stack
sw $a1, 4($sp) #stores a1 in stack
la $s0, v #stores ad. of v[0] in s0
sll $t0, $a1, 2 #stores 4 * l in t0
add $t0, $t0, $s0 #stores ad. of v[l] in t0
lw $s1, 0($t0) #loads v[l] in s1, s1 is pivot
add $t1, $a0, $zero #loads f in t1, t1 is 'i'
add $t2, $a0, $zero #loads f in t2, t2 is 'j'
for1: slt $t3, $t2, $a1 #checks if j < l
beq $t3, $zero, exit
sll $t4, $t2, 2 #t4 stores 4 * j
add $t4, $t4, $s0 #t4 stores ad. of v[j]
lw $t5, 0($t4) #t5 stores value of v[j]
slt $t3, $t5, $s1 #checks if v[j] < pivot
beq $t3, $zero, bfor #jumps to next repetition of the loop
add $a0, $t1, $zero #a0 is i
add $a1, $t2, $zero #a1 is j
jal swap #call swap
addi $t1, $t1, 1 # i++
j bfor #continue loop
bfor: lw $a1, 4($sp) #restores a1
addi $t2, $t2, 1 # j++
j for1 #continue loop
exit: add $a0, $t1, $zero #a0 is i
lw $a1, 4($sp) #restores initial a1
jal swap #call swap
add $v0, $t1, $zero #return i
lw $ra, 0($sp) #restore initial ra
addi $sp, $sp, 8
jr $ra
It is stated that f and l are stored in $a0 and $a1 respectively, the swap function is already created and the vector v is labeled in the memory. I cant understand where my mistake is and any help is welcome. Thanks in advance!
partition:
addi $sp, $sp, -16 #adjust stack for 4 items
sw $ra, 0($sp) #store ra in stack
sw $s0, 4($sp) #store s0
sw $a0, 8($sp) #store a0
sw $a1, 12($sp) #store a1
la $s0, v #load address of v0 in s0
sll $t0, $a1, 2 #t0 stores 4 * l
add $t0, $t0, $s0 #t0 stores ad. of v[l]
lw $t1, 0($t0) #t1 contains v[l] (t1 pivot)
add $t2, $a0, $zero #t2 is f (t2 i)
add $t3, $a0, $zero #t3 is f (t3 j)
for1: slt $t4, $t3, $a1 #sets 1 to t4 if j < l
beq $t4, $zero, exit
sll $t5, $t3, 2 #t5 is 4 * j
add $t5, $t5, $s0 #t5 stores ad. of v[j]
lw $t6, 0($t5) #t6 has the value of v[j]
slt $t4, $t6, $t1 #sets 1 to t4 if v[j] < pivot
beq $t4, $zero, bfor
add $a0, $t2, $zero #a0 is i
add $a1, $t3, $zero #a1 is j
addi $sp, $sp, -12 #adjust stack for 3 items
sw $t1, 0($sp)
sw $t2, 4($sp)
sw $t3, 8($sp) #store pivot, i, j in stack
jal swap #call swap
lw $t1, 0($sp)
lw $t2, 4($sp)
lw $t3, 8($sp) #restores pivot, i, j before the call
addi $sp, $sp, 12 #return items to stack
lw $a1, 12($sp) #restore initial a1
addi $t2, $t2, 1 #i++
j bfor
bfor: addi $t3, $t3, 1 #j++
j for1 #continue loop
exit: add $a0, $t2, $zero #a0 is i
addi $sp, $sp, -4 #adjust stack for 1 item
sw $t2, 0($sp) #store i
jal swap #calls swap
lw $v0, 0($sp) #returns i
addi $sp, $sp, 4 #returns item to stack
lw $ra, 0($sp) #restore initial ra in stack
lw $s0, 4($sp) #restore initial s0
lw $a0, 8($sp) #restore initial a0
lw $a1, 12($sp) #restore initial a1
addi $sp, $sp, 16 #return items to stack
jr $ra
I found the solution. Thank you to those who replied.
The important change is that the values in registers $t1, $t2, and $t3 are preserved across the call to swap, because swap modifies them.
I'm converting C code to Mips code
The C code is
int main(void) {
int i;
int data [10] = { 10, -2, 5, 22, 99, 0, -5, 8, 30, 7};
for(i=0; i<10; i++){
printf("%d\n", data[i]);
}
return 0;
}
my Mips code is
.data
data: .space 10
enter: .asciiz "\n"
.text
.globl main
main:
addi $s0, $zero, 10
add $t0, $zero, $zero
sw $s0, data($t0)
addi $s0, $zero, -2
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 5
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 22
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 99
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 0
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, -5
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 8
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 30
addi $t0, $t0, 4
sw $s0, data($t0)
addi $s0, $zero, 7
addi $t0, $t0, 4
sw $s0, data($t0)
addi $t0, $zero, 0
Loop: slti $t1, $t0, 10
beq $t1, $zero, Exit
mul $t2, $t0, 4
li $v0, 1
lw $a0, data($t2)
syscall
li $v0, 4
la $a0, enter
syscall
addi $t0, $t0, 1
j Loop
Exit:
end:
li $v0, 10
syscall
s1 is for saving a value.
t0 is for index
t1 is a flag for slt
t1 multiply 4 is t2
you should just focus on
li $v0, 4
la $a0, enter
syscall
it prints
10-2522990-58307
dddddddddddddddddddddddddddddddddddasdfasdfasdfasdfasdfasdfasdf
plz help me dddddddddddddddddddddddddddddddddddddd
You haven't reserved enough memory for your data array. .space 10 reserves 10 bytes of memory, but you're trying to store 10 words (40 bytes). So you end up overwriting the newline character.
If you change the declaration to data: .space 40 you should get the expected output.
Screenshot of my error
I have two computers and I do not get this error when running the same MIPS code in QtSpim on one of them.
I tried finding the instruction on the address in the error message, but I cannot find the address on the page... or anywhere.
I have also tried Single Stepping the instructions. The error message comes up on the first instruction.
Here is the code just in case:
.data
N: .word 4
m: .word 2, 5, 3, 4
.word 1, 7, 2, 7
.word 6, 6, 4, 5
.word 2, 5, 3, 4
a: .space 16
.align 2
# main program + show function
.data
msg1:
.asciiz "Matrix\n"
msg2:
.asciiz "Array\n"
.align 2
.text
.globl main
main:
addi $sp, $sp, -4
sw $fp, ($sp)
la $fp, ($sp)
addi $sp, $sp, -4
sw $ra, ($sp)
la $a0, m
lw $a1, N
la $a2, a
jal colSum # colSum(m, N, a)
la $a0, msg1
li $v0, 4
syscall # printf("Matrix\n")
la $a0, m
lw $a1, N
jal showMatrix # showMatrix(m, N)
la $a0, msg2
li $v0, 4
syscall # printf("Array\n")
la $a0, a
lw $a1, N
jal showArray # showArray(b, N)
lw $ra, ($sp)
addi $sp, $sp, 4
lw $fp, ($sp)
addi $sp, $sp, 4
j $ra
# end main()
# void showMatrix(m, N)
# params: m=$a0, N=$a1
# locals: m=$s0, N=$s1, row=$s2, col=$s3
showMatrix:
addi $sp, $sp, -4
sw $fp, ($sp)
la $fp, ($sp)
addi $sp, $sp, -4
sw $ra, ($sp)
addi $sp, $sp, -4
sw $s0, ($sp)
addi $sp, $sp, -4
sw $s1, ($sp)
addi $sp, $sp, -4
sw $s2, ($sp)
addi $sp, $sp, -4
sw $s3, ($sp)
move $s0, $a0
move $s1, $a1
li $s2, 0
show_matrix_loop1:
bge $s2, $s1, end_show_matrix_loop1
li $s3, 0
show_matrix_loop2:
bge $s3, $s1, end_show_matrix_loop2
li $a0, ' ' # putchar(' ')
li $v0, 11
syscall
move $t0, $s2
mul $t0, $t0, $s1
add $t0, $t0, $s3
li $t1, 4
mul $t0, $t0, $t1
add $t0, $t0, $s0
lw $a0, ($t0)
li $v0, 1 # printf("%d",m[row][col])
syscall
addi $s3, $s3, 1 # col++
j show_matrix_loop2
end_show_matrix_loop2:
li $a0, '\n' # putchar('\n')
li $v0, 11
syscall
addi $s2, $s2, 1 # row++
j show_matrix_loop1
end_show_matrix_loop1:
lw $s3, ($sp)
addi $sp, $sp, 4
lw $s2, ($sp)
addi $sp, $sp, 4
lw $s1, ($sp)
addi $sp, $sp, 4
lw $s0, ($sp)
addi $sp, $sp, 4
lw $ra, ($sp)
addi $sp, $sp, 4
lw $fp, ($sp)
addi $sp, $sp, 4
j $ra
# void showArray(a, N)
# params: a=$a0, N=$a1
# locals: a=$s0, N=$s1, i=$s2
showArray:
addi $sp, $sp, -4
sw $fp, ($sp)
la $fp, ($sp)
addi $sp, $sp, -4
sw $ra, ($sp)
addi $sp, $sp, -4
sw $s0, ($sp)
addi $sp, $sp, -4
sw $s1, ($sp)
addi $sp, $sp, -4
sw $s2, ($sp)
move $s0, $a0
move $s1, $a1
li $s2, 0 # i = 0
show_array_for:
bge $s2, $s1, end_show_array_for
li $a0, ' ' # putchar(' ')
li $v0, 11
syscall
move $t0, $s2
mul $t0, $t0, 4
add $t0, $t0, $s0
lw $a0, ($t0)
li $v0, 1 # printf("%d",a[i])
syscall
incr_show_array_for:
addi $s2, $s2, 1 # i++
j show_array_for
end_show_array_for:
li $a0, '\n'
li $v0, 11
syscall
lw $s2, ($sp)
addi $sp, $sp, 4
lw $s1, ($sp)
addi $sp, $sp, 4
lw $s0, ($sp)
addi $sp, $sp, 4
lw $ra, ($sp)
addi $sp, $sp, 4
lw $fp, ($sp)
addi $sp, $sp, 4
j $ra
# COMP1521 17s2 Final Exam
# void colSum(m, N, a)
.text
.globl colSum
# params: m=$a0, N=$a1, a=$a2
colSum:
# prologue
addi $sp, $sp, -4
sw $fp, ($sp)
la $fp, ($sp)
addi $sp, $sp, -4
sw $ra, ($sp)
addi $sp, $sp, -4
sw $s0, ($sp)
addi $sp, $sp, -4
sw $s1, ($sp)
addi $sp, $sp, -4
sw $s2, ($sp)
addi $sp, $sp, -4
sw $s3, ($sp)
addi $sp, $sp, -4
sw $s4, ($sp)
addi $sp, $sp, -4
sw $s5, ($sp)
# suggestion for local variables (based on C code):
# m=#s0, N=$s1, a=$s2, row=$s3, col=$s4, sum=$s5
li $s4, 0 # col
li $s5, 0 # sum
for_col:
bge $s4, $a1, for_col_end # if col >= N, end the loop
li $s5, 0 # sum = 0 ;
li $s3, 0 # row
for_row:
bge $s3, $a1, for_row_end # if row >= N, end the loop
# DO : sum += m[row][col];
li $t0, 4 # size of int
mul $t1, $a1, $s3 # curr row x number of columns
add $t1, $t1, $s4 # =+ current column
mul $t0, $t1, $t0 # =* size of int ////// $t0 is now the offset
# la $t1, ($a0) # load address of matrix into $t1
add $t0, $t0, $a0 # get the exact address of our current position in the matrix
lw $t1, ($t0) # get the word value from the address
add $s5, $s5, $t1 # sum += m[row][col];
addi $s3, $s3, 1 # row ++;
j for_row;
for_row_end:
# DO: a[col] = sum;
li $t0, 4
mul $t0, $t0, $s4 # 4xcol this is out offset from array a
addu $t0, $t0, $a2 # current address of a[col]
sw $s5, ($t0) # a[col] = sum; /// store the word in $s3 (sum) into the address at $t0
addu $s4, $s4, 1 # col ++
j for_col;
for_col_end:
# epilogue
lw $s5, ($sp)
addi $sp, $sp, 4
lw $s4, ($sp)
addi $sp, $sp, 4
lw $s3, ($sp)
addi $sp, $sp, 4
lw $s2, ($sp)
addi $sp, $sp, 4
lw $s1, ($sp)
addi $sp, $sp, 4
lw $s0, ($sp)
addi $sp, $sp, 4
lw $ra, ($sp)
addi $sp, $sp, 4
lw $fp, ($sp)
addi $sp, $sp, 4
j $ra
I have a feeling it might have something to do with the settings on my computer because I have tried this code on QtSpim on my colleague's computer and it works fine.
I can get a similar message if I have enabled delayed branches in QtSpim.
On the machine that isn't working, click on Simple Machine button in settings and try those settings.
Solved! turns out there have been parameters in QtSpim that I set ages ago and (stupidly) forgot to clear. For anyone with this issue, go to Simulator > Run Parameters > (clear the boxes and click on OK).
I have a little problem with my MIPS code, because this not work with "a", I explain better... If I write:
Heeeeello world --> I obtain this output "The 'e' has the biggest number of occurrences, 5"
hi Maaaaaark --> I obtain this output "The '' has the biggest number of occurrences, "
Can someone give me a solution? this is my code:
.data
m1: .asciiz "Give me a string: "
m2: .asciiz "\nThe letter '"
m3: .asciiz "' has the biggest number of occurrences, is present "
m4: .asciiz " times."
.align 2
myArray: .space 104
.text
.globl main
main:
li $v0, 4
la $a0, m1
syscall
addi $sp, $sp, -256
move $s0, $sp
move $a0, $sp
li $a1, 255
li $v0, 8
syscall
move $a0, $s0
jal check_case
move $a0, $v0
move $a1, $v1
jal analizza_stringa
move $a0, $v0
move $a1, $v1
jal stampa_risultato
end_program:
li $v0, 10
syscall
check_case:
move $t0, $a0
while:
lb $t1, ($t0)
beq $t1, 32, ignore_value_and_continue
beq $t1, 10, exit_check_case
bge $t1, 97, continue_check_case
bge $t1, 65, change_case
change_case:
addi $t2, $t1, 32
sb $t2, ($t0)
ignore_value_and_continue:
continue_check_case:
addi $t2, $t2, 1
addi $t0, $t0, 1
j while
exit_check_case:
move $v0, $t0
move $v1, $t2
jr $ra
analizza_stringa:
li $t1, 0
li $t2, 0
li $t3, 0
li $t4, 0
move $t7, $a0
sub $a0, $a0, $v1
sub $t7, $t7, $v1
while_string:
lb $t0, ($a0)
beq $t0, $zero, check_best
beq $t1, 32, ignore_value_and_continue2
ignore_value_and_continue2:
beq $t0, 10, check_best
subi $t3, $t0, 97
mul $t4, $t3, 4
move $t6, $t7
while_occorrenza:
beq $t1, 10, continue
lb $t1,($t6)
bne $t1, $t0, continue2
addi $t5, $t5, 1
continue2:
addi $t6, $t6, 1
j while_occorrenza
continue:
sw $t5, myArray($t4)
li $t5, 0
li $t3, 0
li $t1, 0
addi $a0, $a0, 1
j while_string
check_best:
li $t0, 0
lw $t1, myArray($t0)
addi $t0, $t0, 4
while_check_best:
beq $t0, 104, exit_check
addi $t5, $t5, 1
lw $t4, myArray($t0)
bge $t4, $t1, scambia
j continue_check
scambia:
blt $t4, $t2, continue_check
move $t2, $t4
add $t3, $t5, 97
continue_check:
addi $t0, $t0, 4
j while_check_best
exit_check:
move $v0, $t3
move $v1, $t2
jr $ra
stampa_risultato:
move $t0, $a0
li $v0, 4
la $a0, m2
syscall
li $v0, 11
move $a0, $t0
syscall
li $v0, 4
la $a0, m3
syscall
li $v0, 1
move $a0, $a1
syscall
li $v0, 4
la $a0, m4
syscall
jr $ra
I am trying to translate my fibonacci c code to MIPS assembly code, however, when I run the MIPS code to my MIPS simulator it doesn't seem to end.
C Code:
int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}
int main()
{
fibo(5);
}
Assembly Code:
main: addi $sp, $sp, -4
sw $ra, 0($sp)
addi $a0, $zero, 5
jal fibo;
lw $ra, 0($sp)
addi $sp, $sp, 4
fibo: addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $ra, 8($sp)
slti $t0, $a0, 2
beq $t0, $zero, ELSE
addi $v0, $zero, 1
jr $ra
ELSE: addi $s0, $a0, 0
addi $a0, $a0, -1
jal fibo;
addi $s1, $v0, 0
addi $a0, $s0, -2
jal fibo
add $s1, $s1, $v0
j EXIT
EXIT: lw $s0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
A few issues here.
First, your main function was missing its terminating jr $ra, meaning that main fell through into fibo after being executed.
Second, in your if statement, where you return 1, you call jr $ra directly, which means the stack goes unrestored. I swapped this out with a call to j EXIT.
Finally, at the end of your else you add f(n-1) and f(n-2) into $s1. This register should be $v0 as you are intending to return this result.
The fixed up code is as follows:
main:
addi $sp, $sp, -4
sw $ra, 0($sp)
addi $a0, $zero, 5
jal fibo
move $a0 $v0
li $v0 1
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
fibo:
addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $ra, 8($sp)
slti $t0, $a0, 2
beq $t0, $zero, ELSE
addi $v0, $zero, 1
j EXIT
ELSE:
addi $s0, $a0, 0
addi $a0, $a0, -1
jal fibo
addi $s1, $v0, 0
addi $a0, $s0, -2
jal fibo
add $v0, $s1, $v0
EXIT:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
Here is my code for the same,but it is written in RISC-V
.text
#x5 me n hai
#x7 me output
li x5 7
li x6 2
jal x1 FIB
j EXIT
FIB:
bge x5 x6 REC
addi x7 x5 0
jalr x0 0(x1)
#Handled Base Case
REC:
# Handling Rec. Case
addi sp sp -12
sw x1 0(sp)
sw x5 4(sp)
addi x5 x5 -1
jal x1 FIB
sw x7 8(sp)
lw x5 4(sp)
addi x5 x5 -2
jal x1 FIB
lw x13 8(sp)
add x7 x13 x7
lw x1 0(sp)
addi sp sp 12
jalr x0 0(x1)
EXIT:
In addition to the answer of Konrad (https://stackoverflow.com/a/23185991/3729904), I noticed that there is an error in your algorithm. If n<2 the fib algorithm should return n instead of 1.
This can be fixed changing the line:
addi $v0, $zero, 1
into:
add $v0, $zero, $a0