MIPS question Write a program to print out a series of numbers. First input a nu
ID: 3746765 • Letter: M
Question
MIPS question
Write a program to print out a series of numbers. First input a number N for the size of the list. This should not be more than 20. Next, you need to calculate F(i) for each number 0..N. This means you should print out F(0), F(1), …, F(N-1) for a total of N numbers including the zeroth number. Your series is defined as follows:
F(0) = 2
F(1) = 5
F(i) = F(i-1) + F(i-2) + 1
Like problem #2, the program should exit if the value entered for N is 0, otherwise the program should run again.
So the following would be a sample program output:
Please enter a value for N: 7
2 5 8 14 23 38 62
Please enter a value for N: 6
2 5 8 14 23 38
Explanation / Answer
.data
out_string: .asciiz "/nPlease enter a value for N"
error: .asciiz "/nEntered value is greater than 20"
.text
li $t1, 2
li $t2, 5
li $t3, 1
li $t4 20
## Printing User msg
while:
li $v0, 4
la $a0, out_string
syscall
##Getting user input
li $v0, 5
syscall
#move the value to t0
move $t0, $a0
#Checking if value is greater than 20
bgt $t0, $t4, Exception
#Computing and printing the Series
li $t4, 0
j Print1
for_loop:
beq $t4, $t3, Print2
add $t2, $t2, $t1
add $t2, $t2, $t3
#Printing the series
li $v0, 1
move $a0, $t2
syscall
#Printing space
li $v0, 11
li $a0, ' '
syscall
inc_t3:
add $t4, $t4, $t3
bge $t4, $t0, Next_input
j for_loop
#checking the value
Next_input:
bez $t0, Exit
j while
Print1:
li $v0, 1
move $a0, $t1
syscall
#Printing space
li $v0, 11
li $a0, ' '
syscall
j inc_t3
Print2:
li $v0, 1
move $a0, $t2
syscall
#Printing space
li $v0, 11
li $a0, ' '
syscall
j inc_t3
Exception:
li $v0, 4
la $a0, error
syscall
Exit:
li $v0, 10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.