I'm trying to implement the recursive equation here, but I have no idea what's going wrong with my code.
It keeps output 1 no matter what input I gave.
The function is as follows:
T(n) = T(n-100) + 2*T(n/2) + 5 if n > 1
= 1 otherwise
.globl __start
.text
__start:
# read from standard input a0 = n
addi a0, zero, 5
ecall
################################################################################
# Recursive function #
# T(n) = T(n-100)+2*T((n/2))+5 if n>1 #
# = 1 otherwise #
################################################################################
addi s2,x0,1 #for base case
addi s3,x0,2 #for comparison
blt a0,s3,result
recur:
addi a2,a2,5
addi a3,a3,9
addi sp,sp,-8 #store 2 registers into stack
sw x1,4(sp)
sw a0,0(sp)
bge a0,s3,L1
bge a0,s3,L2
addi s2,x0,1
addi sp,sp,8
jal x0,result
L1: #to do T(n-100)
addi a0,a0,-100
jal x1,recur
addi x6,x10,0 #T(n-100) stored in x6
lw x10,0(sp)
lw x1,4(sp)
addi sp,sp,8
jalr x0,0(x1)
L2: #to do T(n/2)
div a0,a0,s3
jal x1,recur
addi x7,x10,0 #T(n/2) stored in x7
lw a0,0(sp)
lw x1,4(sp)
addi sp,sp,8
mul x7,x7,s3
add s2,x6,x7
addi s2,s2,5
jalr x0,0(x1)
################################################################################
result:
# prints the result in s2
addi a0, zero, 1
addi a1, s2, 0
ecall
# ends the program with status code 0
addi a0, zero, 10
ecall
Related
I want to write a program that calculates the square of n using recursion based on the equation n^2 = (n - 1)^2 + 2(n - 1) + 1 But I don't know how to write the nonbasecase: part. Can anyone help?
A python program would be
def square(n) {
if (n==0):
return 0
else:
return square(n-1) + 2*(n-1) + 1
}
Here is what I got so far.
start:
li $a0, 0x0003 #$a0 contains the number to be squared
jal square # recursive call
square:
subi $sp, $sp, 8 # decrement the stack pointer $sp
sw $ra, 4($sp) # push the return address register $ra
sw $a0, 0($sp) # push argument register $a0
li $t0, 0x0001 # load $t0 with 1 as part of test for base case
bne $a0, $t0, nonbasecase # branch if not the base case
li $v0, 0x0001 # return base result in $v0
addi $sp, $sp 8 # recover stack space
jr $ra # jump to return address in $ra
nonbasecase:
#not sure how to write when it is not the base case
jr $ra # jump to contents of return address register $ra
Write the function so that it inputs n2 and n and returns n2+2*n+1 and n+1.
I'm doing a factorial recursive function in MIPS. Here is the code:
fact: #a1 = n a2 = res
addi $t1,$zero,1
beq $a1,$t1,end # if n == 1 return res
#else
mul $a2,$a2,$a1
subi $a1,$a1,1
jal fact
end:
sb $a2,res
jr $ra
The problem is, this code only works when i write j fact instead of jal fact, with jal fact the code run infinitely for some reason.
fact: #a1 = n a2 = res
addi $t1,$zero,1
beq $a1,$t1,end # if n == 1 return res
#else
addi $sp, $sp, 8
sw $a1, 0($sp) # in the top of the stack store the argument you called fact
sw $ra, 4($sp) # ra holds the return adress ( I do not know whether you know it)
mul $a2,$a2,$a1
subi $a1,$a1,1
jr $ra
jal fact
lw $a1, 0($sp) # load the value from memory adress that the top stack pointer shows to back to the register a1
lw $ra, 4($sp)
addi $sp, $sp, 8 # delete current top , top-1 from the stack
jr $ra
end:
sb $a2,res
jr $ra
I am trying to pratice how recursion works in Mips. So I tried to write a fibonacci function fib.
At first I had addi $a0, $a0, n to write a general solution, but I thought that if I want to check my results in Qtspim , maybe i need to add a real number as an argument. I do not want a full answer , if the thought behind the code is wrong, but some help in order to run it in Qtspim, and find my mistakes (logic mistakes) on my own
This is my code:
.globl main
.text
main:
addi $a0, $a0, 4
fib:
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
slti $t0, $a0, 1
beq $t0, 1, L2 #if n<1
beq $a0, 1, L2 # if n=1
beq $t0, 0, L1 # if n>1
#what to do when n<=1
L2:
addi $v0, $v0, 1
jr $ra
#what to do when n>1
L1:
addi $a0, $a0, -1
jal fib
lw $a0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
lw $t1, 0($v0)
add $v0, $t1, $v0
jr $ra
li $v0,10
syscall
I get an error message as such:
Bad address in data/stack read: 0x00000000
.text
main:
addi $a0, $a0, 4
#### you've place fib inline inside main,
#### you should have all of main here, and "call" fib using jal instruction
#### and the syscall for exit goes up here as well
fib:
addi $sp, $sp, -8 # you will want one more word of stack space
sw $ra, 4($sp)
sw $a0, 0($sp)
slti $t0, $a0, 1 # i would have use 2 here instead of 1
beq $t0, 1, L2 #if n<1 # this is ok, but better to use bne $t0, $0, L2
beq $a0, 1, L2 # if n=1 # and then this would not be needed
beq $t0, 0, L1 # if n>1 # this doesn't need to be conditional, if the program reaches here then L1 is the thing to do next.
#what to do when n<=1
L2:
addi $v0, $v0, 1 # here you want to return just 1 not v0+1
jr $ra
#what to do when n>1
L1:
addi $a0, $a0, -1
jal fib # fib(n-1), good
lw $a0, 0($sp) # this reloads $a0 the original n
lw $ra, 4($sp)
addi $sp, $sp, 8
lw $t1, 0($v0) # after a call to fib $v0 holds fib(n)
# an integer value but you're treating it like a pointer and dereferencing it
add $v0, $t1, $v0 # here doing fib(n-1) + n
# you want fib(n-1) + fib(n-2) instead
# so you're missing a fib(n-2)
jr $ra
li $v0,10 # this is part of main, so move it to where main is
syscall # realize that code located here is unreachable (aka dead)
# anything after an unconditional branch (here the jr $ra just above)
# and without a label is very suspicious as unreachable
update , some improved (i hope ) code
.globl main
.text
main:
addi $a0, $a0, 4
jal fib
li $v0, 10
syscall
fib:
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
slti $t0, $a0, 2
beq $t0, 1, L2
#what to do when n<=1
L2:
addi $v0, $v0, 1
jr $ra
#what to do when n>1
L1:
addi $a0, $a0, -1 # a0 -> n-1
jal fib # fib(n-1)
lw $a0, 0($sp) #load word from memory adress 0($sp) to register $a0
lw $ra, 4($sp) #load word from memory adress 4($sp) to register $ra
addi $sp, $sp, 8
add $t1, $zero, $v1 # in register $t1 store current $v1
add $v0, $t2, $v0 # v0_ n+1 =v0 _n + v0_n-1
add $t2, $t1, $zero # in $t2 save the previous value of v1
.data
prompt: .ascii "Fibonacci Program\n"
.asciiz "Enter N value: "
results: .asciiz "\nFibonacci of N = "
n: .word 0
answer: .word 0
.text
.globl main
.ent main
main:
# Read n value from user
li $v0, 4 # print prompt string
la $a0, prompt
syscall
li $v0, 5 # read N (as integer)
syscall
sw $v0, n
# Call Fibonacci function.
lw $a0, n
jal fib
sw $v0, answer
# Display result
li $v0, 4 # print prompt string
la $a0, results
syscall
li $v0, 1 # print integer
lw $a0, answer
syscall
# Done, terminate program.
li $v0, 10 # terminate
syscall # system call
.end main
# Fibonacci function
# Recursive definition:
# = 0 if n = 0
# = 1 if n = 1
# = fib(n-1) + fib(n-2) if n > 2
# Arguments
# $a0 - n
# Returns
# $v0 set to fib(n)
.globl fib
.ent fib
fib:
subu $sp, $sp, 8
sw $ra, ($sp)
sw $s0, 4($sp)
move $v0, $a0 # check for base cases
ble $a0, 1, fibDone
move $s0, $a0 # get fib(n-1)
sub $a0, $a0, 1
jal fib
move $a0, $s0
sub $a0, $a0, 2 # set n-2
move $s0, $v0 # save fib(n-1)
jal fib # get fib(n-2)
add $v0, $s0, $v0 # fib(n-1)+fib(n-2)
fibDone:
lw $ra, ($sp)
lw $s0, 4($sp)
addu $sp, $sp, 8
jr $ra
.end fib
I am trying to code a Assembly program that will ask for an integer and perform a recursive function: if n>5 f(n) = n*f(n-1)-f(n-3)+n-23 else n<=5 f(n)=15-2n I managed to get the first instance of recursion of the n*f(n-1)but when it goes for the second recursion of f(n-3) it performs the second portion improperly.
During the calculations, if I input 8 as the integer, the result should be 20 which I get but, when I input anything greater than 8 because then the second recursion hits an integer larger than 5 (the base case) I get an incorrect answer. Basically any input larger than 8 doesn't work
For example the correct answers are:
f(9) = 162
f(10) = 1602
f(11) = 17590
The answers I get
f(9)= 27
f(10)=22
f(11)=23
Here is my code:
```#data declarations: declare variable names used in program, storage allocated in RAM
.data
prompt1: .asciiz "\nEnter an Integer:\n" #Ending Index
message1: .asciiz "\nThe Solution is:\n"
answer: .word 0
#program code is contained below under .text
.text
.globl main #define a global function main
# the program begins execution at main()
main:
la $a0, prompt1 #load address of prompt1
li $v0, 4 #prepare print string
syscall
li $v0, 5 #prepare receive int
syscall
move $a0, $v0
addi $sp, $sp, -4
sw $ra, 0($sp)
jal Function
sw $v0, answer #move returned answer to a new memory
lw $ra, 0($sp)
addi $sp, $sp, 4
la $a0, message1 #load address of message1
li $v0, 4 #prepare print string
syscall
lw $a0, answer
li $v0, 1
syscall
jr $ra
############################################################################
# Procedure/Function Function1
# Description: recursive math function
# parameters: $a0 = value of n,
# return value: $v0 = answer
# registers to be used: $s3 and $s4 will be used.
############################################################################
Function:
addi $sp, $sp, -12 #adjust stack pointer
sw $ra, 8($sp) #save return address
sw $a0, 4($sp) #save n
sw $s0, 0($sp) #save immediate value (used for storing function(n-1))
li $t2, 15
slti $t0, $a0, 6
beq $t0, $zero, GreaterThanFive
LessThanFive:
add $t1, $a0, $zero
add $t1, $t1, $a0
sub $t2, $t2, $t1
move $v0, $t2
j Conclusion
GreaterThanFive:
addi $a0, $a0, -1
jal Function
move $s0, $v0
lw $a0, 4($sp)
addi $a0, $a0, -3
jal Function
lw $a0, 4($sp)
mul $t3, $a0, $s0
sub $t4, $t3, $v0
add $t5, $t4, $a0
addi $t6, $t5, -23
move $v0, $t6
Conclusion:
lw $s1,0($sp) # restore intermediate value
lw $a0,4($sp)
lw $ra,8($sp)
addi $sp,$sp,12 # restore stack
jr $ra #return to caller```
I'm currently trying to figure out how to code a function of finding the lowest integer in MIPS following this algorithm...
int Min( int[] A, int low, int high)
{ if (low== high) return A[low];
int mid = (low+high)/2;
int min1 = Min( int[] A, low, mid);
int min2 =Min( int[] A, mid +1, high);
if(min1>min2) return min2;
return min1;
}
I'm receiving problems as I attempt to code this in MIPS. Here is my current MIPS code. The user inputs up to 6 integers which are stored in an array. The registers $a0, $a1, and $a2 are used as arguments for the function.
$a0 = int[]A
$a1 = int low //index
$a2 = int high //index
Here is the recursion function...
min:
bne $a1, $a2, recur
mul $t4, $a1, 4
add $a0, $a0, $t4
lw $v1, 0($a0)
jr $ra
# recursion start
recur:
addiu $sp, $sp, -12 #reserve 12 bytes on stack
sw $ra, 0($sp) #push return address
# mid = (low+high)/2 t0 = mid t1= min1 t2=min2 t3 = mid+1
add $t0, $a1, $a2 # t0 = low + high
div $t0, $t0, 2 # t0 = (low+high)/2
# mid1 = min(int[]A,low,mid)
min1:
sw $a2, 4($sp) #push high
addi $t3, $t0, 1 # mid+1
sw $t3, 8($sp) #store mid+1
move $a2, $t0 #change high to mid
jal min #compute
# check
move $t1, $v1 #set up the min1 = return value
# mid2 = min(int[]A,mid+1,high)
min2:
lw $a2, 4($sp) #reload high prior call
lw $a1, 8($sp) #change low to mid+1
jal min #compute
move $t2, $v1 #set as the min2 = return value
confirm:
# return mid2 if mid1 > mid2
bgt $t1, $t2, returnMid2
# else return mid1
move $v1, $t1
j minFinal
returnMid2:
move $v1, $t2
minFinal:
lw $ra, 0($sp)
addiu $sp, $sp, 12 #release stack
jr $ra
The problem is whatever combination of integers I input during the program, I never get the minimum value but rather the number "543976553". I've been looking over my code and notes and I still don't have a clue.
For mid1, try putting the return value on the stack and then moving it to $t1 AFTER mid2 is called. Then compare $v0 with $t1 instead of comparing $t1 with $t2
When using the div command in MIPS, doesn't one need to acquire the quotient from $LO ?
thus your logic for (high+low)/2 might look something like
add $t0, $a1, $a2 # t0 = low + high
addi $t5, $zero, 2 # t5=2
div $t0, $t5 # LO = t0/t5, HI = t0%t5
addi $t0, $LO, 0 # t0 = LO (t0/t5)
some of those lines are written just how I learned to do MIPS but you may have a different style for loading immediate values into a register.
I also can't promise this will fix everything, but at first glance, that is something I noticed.