Question: MIPS assembly: Your assignment is to write a MIPS program that successfully executes on the QtSpim simulator that determines if numbers input by a user

MIPS assembly:

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.

Can you tell me what is missing i my code?

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, "+" 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, "=" syscall li $v0, 1 # print sum of factors move $a0, $t1 syscall li $v0, 4 # print newline la $a0, " " 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!