Question
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
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.
I have already written the code but it won;t execute correctly. Can you provide the correct code please?
.data MSG1: .asciiz "Enter an integer greater than or equal to 2: " MSG2: .asciiz "Perfect" MSG3: .asciiz "Prime" MSG4: .asciiz "Not prime" MSG5: .asciiz "+" MSG6: .asciiz "=" MSG7: .asciiz " "
.text .globl main # main function: prompts user for input, prints sum of factors, # and determines if input is perfect or prime main: li $v0, 4 # print message asking for input la $a0, MSG1 syscall
input_loop: li $v0, 5 # read input syscall move $t0, $v0 # save input in $t0
blt $t0, 2, exit_main # exit if input is less than 2
jal print_sum_of_factors # print sum of factors
# check if input is perfect or prime and print appropriate message li $t1, 0 # initialize sum of factors to 0 li $t2, 1 # initialize is_perfect to true li $t3, 2 # initialize divisor to 2
perfect_prime_loop: bge $t3, $t0, perfect_prime_done # exit loop if divisor exceeds input
jal is_a_factor # check if divisor is a factor of input beq $v0, 1, add_factor # if yes, add it to sum of factors and try the next divisor
addi $t3, $t3, 1 # otherwise, try the next divisor j perfect_prime_loop
add_factor: add $t1, $t1, $t3 # add divisor to sum of factors beq $t0, $t1, print_perfect # if sum of factors equals input, input is perfect addi $t3, $t3, 1 # otherwise, try the next divisor j perfect_prime_loop
print_perfect: li $v0, 4 # print "Perfect" la $a0, MSG2 syscall j input_loop
perfect_prime_done: bne $t1, 0, print_prime # if sum of factors is not zero, input is not prime
li $v0, 4 # print "Prime" la $a0, MSG3 syscall j input_loop
print_prime: li $v0, 4 # print "Not prime" la $a0, MSG4 syscall j input_loop
exit_main: li $v0, 10 # exit program syscall
is_a_factor:
addi $sp, $sp, -4 # allocate space on stack sw $ra, 0($sp) # save return address on stack div $a0, $a1 # check if $a1 divides $a0 evenly mfhi $t0 # remainder is in hi register beq $t0, $0, is_factor # if remainder is zero, $a1 is a factor li $v0, 0 # otherwise, $a1 is not a factor j exit_is_factor is_factor: li $v0, 1 # $a1 is a factor exit_is_factor: lw $ra, 0($sp) # restore return address from stack addi $sp, $sp, 4 # deallocate space on stack jr $ra # return to calling function
# print_sum_of_factors function: prints the factors of an integer value # separated by '+', followed by '=', and the sum of the factors # $a0 = integer value print_sum_of_factors: addi $sp, $sp, -12 # allocate space on stack sw $ra, 0($sp) # save return address on stack sw $a0, 4($sp) # save integer value on stack li $t0, 1 # start with 1 as a factor li $t1, 0 # initialize sum of factors to 0 print_loop: beq $t0, $a0, print_sum # exit loop when $t0 == $a0 jal is_a_factor # check if $t0 is a factor of $a0 beq $v0, 1, print_factor # if yes, print $t0 as a factor addi $t0, $t0, 1 # otherwise, try the next integer value j print_loop print_factor: li $v0, 1 # print integer value move $a0, $t0 syscall li $v0, 4 # print '+' la $a0, MSG5 syscall add $t1, $t1, $t0 # add factor to sum of factors addi $t0, $t0, 1 # try the next integer value j print_loop print_sum: add $t1, $t1, $a0 # add $a0 as a factor (it was skipped in the loop) li $v0, 4 # print '=' la $a0, MSG6 syscall li $v0, 1 # print sum of factors move $a0, $t1 syscall li $v0, 4 # print newline la $a0, MSG7 syscall exit_print_sum: lw $a0, 4($sp) # restore integer value from stack lw $ra, 0($sp) # restore return address from stack addi $sp, $sp, 12 # deallocate space on stack jr $ra # return
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