In: Computer Science
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD **
Type a positive integer for A: -2
Sorry, you must enter a positive integer (>0). Please try again.
** Recursive Euclidean Algorithm to calculate GCD **
Type a positive integer for A: 35
Type a positive integer for B: 63
The GCD is 7
Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Recursive Euclidean Algorithm to calculate GCD (the Greatest Common Divisor). Please see below for the Recursive Euclidean Algorithm in Java and C++.
Make sure program the error for negative number.
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD **
Type a positive integer for A: -2
Sorry, you must enter a positive integer (>0). Please try again.
// Java: Euclidean Algorithm to calculate GCD // import java.util.Scanner; public class FindGCD { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("The Euclidean Algorithm to calculate GCD.\n"); System.out.println("Enter a positive integer A: "); int A = input.nextInt(); System.out.println("Enter a positive integer B: "); int B = input.nextInt(); if (A < 1 || B < 1) System.out.println("\nError, please enter a positive (>0) integer."); else { if (A < B) { int temp = A; A = B; B = temp; } System.out.println("The GCD is :" + GCD(A, B)); } } public static int GCD(int A, int B) { if (B == 0) return A; else return GCD(B, A % B); } }
// C++: Euclidean Algorithm to calculate GCD // #include using namespace std; int GCD(int A, int B) { if (B == 0) return A; else return GCD(B, A%B); } int main() { int A, B; cout > A; cout > B; if (A < 1 || B < 1) cout 0) integer.\n”; else { if (A < B) { int temp = A; A = B; B = temp; } cout << "The GCD is :" << GCD(A, B) << endl; } return 0; }
.text
.globl __start
__start: li $v0, 4
la $a0,
First
syscall
# Ask for the first integer
li $v0, 5
syscall
# Read an integer
add $t0, $0,
$v0 # and store it in $t0
li $v0, 4
la $a0,
Second
syscall
# Ask for the second integer
li $v0, 5
syscall
# Read an integer
add $t1, $0,
$v0 # and store it in $t1
# Include here one of the following two versions
# Version 1: based on integer division
loop: div $t0, $t1
mfhi
$t2
# $t2 = $t0 mod $t1
add $t0, $0,
$t1 # $t0 = $t1
add $t1, $0,
$t2 # $t1 = $t2
bne $t1, $0,
loop
# Version 2: based on subtract
#loop: beq $t0, $t1,
exit # if the numbers are equal then
exit
# bgt $t0, $t1,
skip # subtract the smaller from the
bigger
# sub $t1, $t1,
$t0 # and replace the bigger with the
result
# b
loop
# then continue
#skip: sub $t0, $t0, $t1
# b loop
# end of inclusion
exit: li $v0, 4
la $a0, Ans
syscall
# Print "The GCD is: "
add $a0, $0,
$t0
li $v0, 1
syscall
# Print GCD
li $v0, 4
la $a0, nl
syscall
# Print new line
b __start # Go to __start (Press Ctrl-C for exit)
.data
First: .asciiz "Calculating GCD of two integers.\nEnter
first integer: "
Second: .asciiz "Enter second integer: "
Ans: .asciiz "Their GCD is: "
nl: .asciiz "\n\n"