A factor of an integer N is an integer that divides N evenly. Note that 1 and N
ID: 3788325 • Letter: A
Question
A factor of an integer N is an integer that divides N evenly. Note that 1 and N are always factors of N. For this task, You will need to write a MIPS program that accepts a positive integer from the standard input, counts and reports how many factors of this integer are even and how many are odd. See the sample run below for required format. Sample runs (user input in blue): Please enter a positive int: 20 User input: 20 Number of odd factors: 2 Number of even factors: 4 Please enter a positive int: 27 User input: 27 Number of odd factors: 4 Number of even factors: 0Explanation / Answer
Given below is the MIPS code and output for the question. Please do rate if it helped. Thank you.
.data
prompt: .asciiz "Please enter a positive integer: "
message: .asciiz "User input is : "
space: .asciiz " "
outputeven: .asciiz " Number of even factors:"
outputodd: .asciiz " Number of odd factors:"
.text
main:
#prompt the user
li $v0, 4
la $a0, prompt
syscall
#read int input from user
li $v0, 5
syscall
move $t1,$v0 # store n in $t1
#display the user input
li $v0, 4
la $a0, message
syscall
li $v0, 1
move $a0, $t1
syscall
add $t2,$zero, 2
move $t3,$zero #counter for even factors
add $t4, $zero, 1 #counter for odd factors
loop:
#check if the current number t2 divides n
remu $t5,$t1,$t2
bne $t5,$zero,next #not divisible, goto next
#divisible, so check if $t2 is an odd factor or even factor
#for this divide by 2 and check remainder
add $t5,$zero, 2
remu $t5,$t2,$t5
bne $t5,$zero,increment_odd
add $t3, $t3, 1 #increment even counter
j next
increment_odd:
add $t4, $t4, 1
next:
add $t2,$t2,1
ble $t2,$t1,loop
#display odd factors
li $v0,4
la $a0,outputodd
syscall
li $v0, 1
move $a0,$t4
syscall
#display the even factors
li $v0, 4
la $a0, outputeven
syscall
li $v0, 1
move $a0, $t3
syscall
#exit
li $v0,10
syscall
output
Please enter a positive integer: 20
User input is : 20
Number of odd factors:2
Number of even factors:4
-- program is finished running --
Please enter a positive integer: 27
User input is : 27
Number of odd factors:4
Number of even factors:0
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.