Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, I have a written code and bolded the lines that give me error. Please provide the correct code Your assignment is to

For this assignment, I have a written code and bolded the lines that give me error. Please provide the correct code

Your assignment is to write a MIPS program that successfully executes on the QtSpim simulator that determines if numbers input by a user are perfect or prime. Your program should prompt the user, read an integer value, show the sum of the factors of the value that was read, and indicate whether or not the number is perfect. If it is not perfect, then you should also indicate whether or not it is prime. You should continue until a number less than 2 is entered.

You are required to write a separate function to determine if one integer value is a factor of another integer value.

Call this function is_a_factor. This function will accept two integer arguments, where the first is the larger value (received through $a0) and the second is the potential factor (received through $a1). It returns a value (through $v0)

of one if the second value is a factor of the first value, and otherwise returns a value of zero.

You are also required to write a separate function that prints the factors of a integer value, where a + separates each preceding factor and the next factor. The factor list should be followed by an = and the sum of the factors. Call this function print_sum_of_factors. It accepts an integer argument (through $a0) and return the sum of the factors of the argument (through $v0).

The is_a_factor, print_sum_of_factors, and main functions should follow the MIPS calling conventions described in the lecture slides and videos in the module "FP Instructions and Functions". Note you should only use a syscall instruction to perform I/O and should not use it to exit the program.

Be sure to provide a comment before each block of MIPS assembly code so others can understand your program.

Below is an example session where the output is generated by the program with the values entered by the user displayed in bold.

Enter a value greater than 1: 4 1+2=3 The number 4 is not perfect. Enter a value greater than 1: 6 1+2+3=6 The number 6 is perfect! Enter a value greater than 1: 10 1+2+5=8 The number 10 is not perfect. Enter a value greater than 1: 13 1=1 The number 13 is not perfect and prime. Enter a value greater than 1: 0 Exiting program.

Code:

.data # Define variables for input and output prompt: .asciiz "Enter a value greater than 1: " sum: .asciiz "= " not_perfect: .asciiz "The number is not perfect" perfect: .asciiz "The number is perfect!" not_prime: .asciiz " and not prime. " prime: .asciiz " and prime. "

# Define variables for input and output formatting factor_separator: .asciiz "+" newline: .asciiz " "

.text #25 # Main program main: li $t0, 100 # Initialize counter to max array size j prompt_loop # Jump to input prompt loop

prompt_loop: la $a0, prompt # Load input prompt string address li $v0, 1 # Load print integer system call code syscall # Print input prompt

li $v0, 5 # Load read integer system call code syscall # Read integer from user move $s0, $v0 # Save input value in $s0

# Exit program if input value is less than or equal to 1 blez $s0, exit_program

li $t1, 0 # Initialize factor sum to zero li $t2, 1 # Initialize factor check to 1 jal sum_of_factors # Compute sum of factors move $t1, $v0 # Save sum of factors in $t1

# Print the list of factors la $a0, newline # Load newline string address li $v0, 1 # Load print integer system call code50 syscall # Print newline character

#la $a0, $s0 # Load input value to print factors for jal print_factors # Call function to print factors

la $a0, sum # Load sum string address li $v0, 1 # Load print integer system call code syscall # Print equals sign to denote sum of factors move $a0, $t1 # Move sum of factors to argument register for printing li $v0, 1 # Load print integer system call code syscall # Print sum of factors

# Check if input value is perfect or prime beq $s0, $t1, print_perfect # Print perfect message if sum of factors equals input value jal is_perfect # Check if input value is perfect bne $v0, 1, print_not_prime # Print not prime message if input value is not prime j print_prime # Print prime message if input value is prime

sum_of_factors: # Calculate sum of factors of input value70 li $v0, 0 # Initialize sum to zero addi $sp, $sp, -4 # Allocate space on stack sw $ra, 0($sp) # Save return address on stack move $t3, $a0 # Save input value in $t3 li $t2, 1 # Initialize factor check to 1 loop: bgt $ # exit program exit_program: li $v0, 10 # syscall to exit program syscall # exit

# print_sum_of_factors function # accepts an integer argument through $a0 and returns the sum of the factors through $v0 print_sum_of_factors: # initialize sum to 0 li $v0, 0 # initialize counter to 1 li $t0, 1 # loop to check factors loop: # check if counter is greater than argument value bgt $t0, $a0, exit_loop # check if counter is a factor of argument value move $a1, $a0 # move argument value to $a1 for is_a_factor function call jal is_a_factor # call is_a_factor function beq $v0, 1, add_factor # if is_a_factor returns 1, add factor to sum addi $t0, $t0, 1 # increment counter j loop # jump to loop start # exit loop exit_loop: jr $ra # return to calling function # add factor to sum add_factor: add $v0, $v0, $t0 # add factor to sum addi $t0, $t0, 1 # increment counter j loop # jump to loop start

# is_a_factor function # accepts two integer arguments, where the first is the larger value (received through $a0) # and the second is the potential factor (received through $a1). # It returns a value (through $v0) of one if the second value is a factor of the first value, # and otherwise returns a value of zero. is_a_factor: # check if second value is a factor of first value div $a0, $a1 # divide first value by second value mfhi $t0 # get remainder beq $t0, 0, is_factor # if remainder is 0, second value is a factor of first value li $v0, 0 # otherwise, second value is not a factor of first value jr $ra # return to calling function # second value is a factor of first value is_factor: li $v0, 1 # set return value to 1 jr $ra # return to calling function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How was your life influenced by those events?

Answered: 1 week ago

Question

Which of these influenced your life most dramatically?

Answered: 1 week ago