Question
I need help with this MIPS assembly code, I need it to return only the first 1 when the user input is 1 and when
I need help with this MIPS assembly code, I need it to return only the first 1 when the user input is 1 and when the user inputs 2, I need it to output 1,1 instead of the current 1,1,2 when the user inputs 1 and 2.
# Prompt user for input and store in $t0 .globl main main: li $v0, 4 # system call for print_str la $a0, prompt # address of prompt string syscall
li $v0, 5 # system call for read_int syscall move $t0, $v0
# Check if n is at least 1 blt $t0, 1, prompt_user # jumps to prompt_user if the input is less than 1
# Display the first n numbers of the Fibonacci series li $t1, 1 # Initialize first number to 1
li $t2, 1 # Initialize second number to 1
# Display the first two numbers of the series li $v0, 1 # system call for print_int move $a0, $t1 #displays the value 1 syscall li $v0, 4 # system call for print_str la $a0, comma # prints comma between numbers syscall li $v0, 1 move $a0, $t2 # displays the value 1 syscall
# Display the remaining numbers of the series sub $t0, $t0, 2 # subtract 2 since we have already displayed the first 2 numbers
Loop: add $t3, $t1, $t2 # Calculate next number in the series move $t1, $t2 #moves t2 value to t1
move $t2, $t3 #moves t3 value to t2 li $v0, 4 # system call for print_str la $a0, comma # prints comma between numbers syscall move $a0, $t3 # Display the next number li $v0, 1 # system call for print_int syscall addi $t0, $t0, -1 # Decrement loop counter # Branch greater than 0 bgtz $t0, Loop # Loop until all n numbers have been displayed li $v0, 4 la $a0, option2 # Created an option 2 to display it on a new line syscall li $v0, 5 # read user input syscall
# check user input beq $v0, 0, exit beq $v0, 1, continue bne $v0, 1, exit
# When 10 is loaded into v0 it will exit #li $v0, 10 # system call for exit #syscall
# Prompt user to input n again if n < 1 prompt_user: li $v0, 4 # system call for print_str la $a0, error # address of error string syscall li $v0, 4 la $a0, option syscall # read user input li $v0, 5 syscall # check user input beq $v0, 0, exit beq $v0, 1, continue bne $v0, 1, exit j main exit: # exit program li $v0, 10 syscall continue: # continue program j main
#End of Program
# Data section for storing prompt and error strings .data prompt: .asciiz "Enter the number of Fibonacci series to display: " error: .asciiz "Error: n must be larger than or equal to 1. Please try again. " comma: .asciiz "," option: .asciiz "Press 0 to exit, or 1 to continue: " option2: .asciiz " Press 0 to exit, or 1 to continue: " #Needed a new version with new line to make it neater
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started