Question

In: Computer Science

Write a SPIM program to find weather two numbers are relatively prime. Two integers are said...

Write a SPIM program to find weather two numbers are relatively prime. Two integers are said to be relatively prime if there is no integer greater than one that divides them both. Sample I/O: Enter the first number: 14 Enter the second number: 15 The entered numbers are relatively prime. Sample I/O: Enter the first number: 12 Enter the second number: 15 The entered numbers are not relatively prime.

Solutions

Expert Solution

# is_prime.s

# Tells if a number is prime

                .data

num:     .word    743711                                                                  # This is the number we'll test to see

                                                                                                                # if it's prime.

                                                                                                                # It will be located by the label `num`

                .text

main:

                lw           $a0, num

                jal           is_prime                                                              # Send the number to the procedure!

                add        $a0, $zero, $v0                  # Send the result as an argument to...

                li              $v0, 1                                                    # ...print integer onscreen

                syscall

                li              $v0, 10                                                  # exit the program

                syscall  

## Tells if a number is prime

# $a0     The number to check if it's prime

# $v0     1 if the number is prime, 0 if it's not

is_prime:

                addi       $t0, $zero, 2                                                       # int x = 2

               

is_prime_test:

                slt           $t1, $t0, $a0                                                        # if (x > num)

                bne        $t1, $zero, is_prime_loop                           

                addi       $v0, $zero, 1                                                       # It's prime!

                jr             $ra                                                                                          # return 1

is_prime_loop:                                                                                 # else

                div          $a0, $t0                                                                               

                mfhi       $t3                                                                                          # c = (num % x)

                slti          $t4, $t3, 1                                                           

                beq        $t4, $zero, is_prime_loop_continue        # if (c == 0)

                add        $v0, $zero, $zero                                                              # its not a prime

                jr             $ra                                                                                                          # return 0

is_prime_loop_continue:                            

                addi $t0, $t0, 1                                                   # x++

                j               is_prime_test                                                    # continue the loop


Related Solutions

Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Write a Python program that print out the list of couples of prime numbers that are...
Write a Python program that print out the list of couples of prime numbers that are less than 50, but their sum is bigger than 40. For instance(29,13)or(37,17),etc. Your program should print all couples
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT