Problem Statement Write a MIPS assembly program that prompts
Problem Statement:
Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Recursive (the Greatest Common Divisor). Please see next page for the Recursive
in C++. Sample Execution 1:
Euclidean Algorithm to calculate GCD
Euclidean Algorithm
** Recursive Euclidean Algorithm to calculate GCD **
Type a positive integer for A: 35
Type a positive integer for B: 63
The GCD is 7
Sample Execution 2:
** Recursive Euclidean Algorithm to calculate GCD **
Type a positive integer for A: -2
Sorry, you must enter a positive integer (>0). Please try again.
Note:
- If your program produces a correct answer without using function calls, the best grade you will receive is a “C”.
 - If your program produces a correct answer and uses function calls (but not recursive function calls), the best grade
you will receive is a “B”.
 - In order to receive an “A”, your program must produces a correct answer, uses RECURSIVE function calls, and is
well commented.
//
// #include \"stdafx.h\"
#include <iostream>
using namespace std;
}
int main() {
int A, B;
if (B == 0)
else
return A;
return GCD(B, A%B);
cout << \"The Euclidean Algorithm to calculate GCD.\ \";
cout << \"Enter a positive integer A: \";
cin >> A;
cout << \"Enter a positive integer B: \";
cin >> B;
if (A < 1 || B < 1)
cout << \"\ Error, please enter a positive (>0) integer.\" << endl;
else
{
if (A < B)
{
int temp = A;
A = B;
B = temp;
}
cout << \"The GCD is \" << GCD(A, B) << endl;
}
return 0; }
Solution
Answer:
MIPS Assembly Language Code For G C D Using Recursion:
.zero 1
 L0:
 push rbp
 mov rbp, rsp
 sub rsp, 16
 mov DWORD PTR [rbp-4], edi
 mov DWORD PTR [rbp-8], esi
 cmp DWORD PTR [rbp-8], 0
 je .L2
 mov eax, DWORD PTR [rbp-4]
 cdq
 idiv DWORD PTR [rbp-8]
 mov eax, DWORD PTR [rbp-8]
 mov esi, edx
 mov edi, eax
 call L0
 jmp .L4
 .L2:
 mov eax, DWORD PTR [rbp-4]
 .L4:
 leave
 ret
 .LC0:
 .string \"Enter two numbers to find GCD using Euclidean algorithm: \"
 .LC1:
 .string \"\ The GCD of \"
 .LC2:
 .string \" and \"
 .LC3:
 .string \" is: \"
 main:
 push rbp
 mov rbp, rsp
 push r12
 push rbx
 sub rsp, 16
 mov esi, OFFSET FLAT:.LC0
 mov edi, OFFSET FLAT:std::cout
 call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
 lea rax, [rbp-24]
 mov rsi, rax
 mov edi, OFFSET FLAT:std::cin
 call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
 mov rdx, rax
 lea rax, [rbp-28]
 mov rsi, rax
 mov rdi, rdx
 call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
 mov edx, DWORD PTR [rbp-28]
 mov eax, DWORD PTR [rbp-24]
 mov esi, edx
 mov edi, eax
 call L0
 mov DWORD PTR [rbp-20], eax
 mov ebx, DWORD PTR [rbp-28]
 mov r12d, DWORD PTR [rbp-24]
 mov esi, OFFSET FLAT:.LC1
 mov edi, OFFSET FLAT:std::cout
 call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
 mov esi, r12d
 mov rdi, rax
 call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
 mov esi, OFFSET FLAT:.LC2
 mov rdi, rax
 call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
 mov esi, ebx
 mov rdi, rax
 call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
 mov esi, OFFSET FLAT:.LC3
 mov rdi, rax
 call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
 mov rdx, rax
 mov eax, DWORD PTR [rbp-20]
 mov esi, eax
 mov rdi, rdx
 call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
 mov esi, OFFSET FLAT:std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
 mov rdi, rax
 call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
 mov eax, 0
 add rsp, 16
 pop rbx
 pop r12
 pop rbp
 ret
 __static_initialization_and_destruction_0(int, int):
 push rbp
 mov rbp, rsp
 sub rsp, 16
 mov DWORD PTR [rbp-4], edi
 mov DWORD PTR [rbp-8], esi
 cmp DWORD PTR [rbp-4], 1
 jne .L9
 cmp DWORD PTR [rbp-8], 65535
 jne .L9
 mov edi, OFFSET FLAT:std::__ioinit
 call std::ios_base::Init::Init()
 mov edx, OFFSET FLAT:__dso_handle
 mov esi, OFFSET FLAT:std::__ioinit
 mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
 call __cxa_atexit
 .L9:
 nop
 leave
 ret
 push rbp
 mov rbp, rsp
 mov esi, 65535
 mov edi, 1
 call __static_initialization_and_destruction_0(int, int)
 pop rbp
 ret




