In: Computer Science
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0 and prints in a column all of its divisors including 1 and the number itself. Don't forget that printing a number and printing a character requires two different system calls.
Here is an example. If the number in a0 is 6 the following output appears:
1
2
3
6
solution:
given data:
code:
.text
main:
li $v0, 5 #this is the system call for reading an integer
syscall #the value will be read in $v0
move $s0, $v0 #set $s0=n
li $s1, 1 #this is the iterator variable, for iterating 1 to
n
while:
div $s0, $s1 #the remainder will be in HI
mfhi $s2 #load the remainder into $s2
bne $s2, $0, incr #don't print the number if remainder is not
0
move $a0, $s1 #the number is divisor, so print it
li $v0, 1
syscall
li $v0, 11 #print a newline
li $a0, 10
syscall
incr:
addi $s1, $s1, 1
bge $s0, $s1, while
li $v0, 10 #this exits the code
syscall
Here is a screenshot of the code:
here is a screenshot of some outputs (note that the first line
is the input I gave):
please give me thumb up