Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have been able to code part of this but it still is not working please tell me what i did wrong and help me

I have been able to code part of this but it still is not working please tell me what i did wrong and help me to fix this code thank you. Heres the assignment: Please code using MIPS ASSEMBLY and show me the output: 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.

Heres what I have so far:

# Prompt the user to enter a value greater than 1

li $v0, 4 # syscall code for printing a string

la $a0, prompt # load address of prompt string

syscall

# Read an integer value

li $v0, 5 # syscall code for reading an integer

syscall

move $t0, $v0 # move user input to temporary register t0

# Exit if the input value is less than 2

blt $t0, 2, exit # branch if less than, exit if true

# Call the print_sum_of_factors function to print the factors and calculate the sum

move $a0, $t0 # pass the user input to the function

jal print_sum_of_factors # jump and link to the function

move $t1, $v0 # move the sum of factors to temporary register t1

# Check if the input value is perfect

beq $t0, $t1, perfect # branch if equal, the number is perfect

# If the number is not perfect, call the is_a_factor function to check if it's prime

move $a0, $t0 # pass the user input to the function

li $a1, 2 # start checking for factors from 2

jal is_a_factor # jump and link to the function

beq $v0, 0, not_prime # branch if equal, the number is not prime

# Print the message indicating the number is not perfect and prime

li $v0, 4 # syscall code for printing a string

la $a0, not_perfect_prime # load address of not perfect and prime message string

syscall

j next_input # jump to next input

perfect:

# Print the message indicating the number is perfect

li $v0, 4 # syscall code for printing a string

la $a0, perfect_msg # load address of perfect message string

syscall

j next_input # jump to next input

not_prime:

# Print the message indicating the number is not perfect and not prime

li $v0, 4 # syscall code for printing a string

la $a0, not_perfect_not_prime # load address of not perfect and not prime message string

syscall

j next_input # jump to next input

next_input:

# Prompt the user to enter another value greater than 1

li $v0, 4 # syscall code for printing a string

la $a0, next_prompt # load address of next prompt string

syscall

# Read another integer value

li $v0, 5 # syscall code for reading an integer

syscall

move $t0, $v0 # move user input to temporary register t0

# Exit if the input value is less than 2

blt $t0, 2, exit # branch if less than, exit if true

# Repeat the process for the next input value

j main # jump back to main

exit:

# Print the message indicating program exit

li $v0, 4 # syscall code for printing a string

la $a0, exit_msg # load address of exit message string

syscall

# Exit the program

li $v0, 10 # syscall code for exiting the program

syscall

# data segment

.data

prompt: .asciiz "Enter a value greater than 1: "

perfect_msg: .asciiz "The number is perfect! "

not_perfect_msg: .asciiz "The number is not perfect"

prime_msg: .asciiz " and prime. "

not_prime_msg: .asciiz " and not prime. "

# text segment

.text

main:

# prompt user for input

li $v0, 4 # print_string syscall code

la $a0, prompt # load address of prompt

syscall

# read integer value from user

li $v0, 5 # read_int syscall code

syscall

move $s0, $v0 # move input value to $s0

# loop until value less than 2 is entered

bgt $s0, 1, loop

# exit program

li $v0, 10 # exit syscall code

syscall

loop:

# print input value

li $v0, 1 # print_int syscall code

move $a0, $s0 # move input value to $a0

syscall

# print sum of factors and check if number is perfect or prime

jal print_sum_of_factors

# prompt user for input

li $v0, 4 # print_string syscall code

la $a0, prompt # load address of prompt

syscall

# read integer value from user

li $v0, 5 # read_int syscall code

syscall

move $s0, $v0 # move input value to $s0

# loop until value less than 2 is entered

bgt $s0, 1, loop

# exit program

li $v0, 10 # exit syscall code

syscall

# function to determine if one integer value is a factor of another integer value

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

# arguments: $a0 - larger value, $a1 - potential factor

is_a_factor:

# TODO: implement is_a_factor function

jr $ra # return to calling function

# function to print the factors of a integer value and calculate the sum of factors

# prints the factor list separated by '+' and the sum of factors followed by '='

# arguments: $a0 - input value

# returns the sum of factors in $v0

print_sum_of_factors:

# TODO: implement print_sum_of_factors function

jr $ra # return to calling function

# data segment

.data

prompt: .asciiz "Enter a value greater than 1: "

perfect_msg: .asciiz "The number is perfect! "

not_perfect_msg: .asciiz "The number is not perfect"

prime_msg: .asciiz " and prime. "

not_prime_msg: .asciiz " and not prime. "

factor_separator: .asciiz "+"

sum_msg: .asciiz "="

# text segment

.text

main:

# prompt user for input

li $v0, 4 # print_string syscall code

la $a0, prompt # load address of prompt

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

Describe a persuasive message.

Answered: 1 week ago

Question

Identify and use the five steps for conducting research.

Answered: 1 week ago

Question

List the goals of a persuasive message.

Answered: 1 week ago