Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MIPS assembly program that prompts the user for 2 positive integers (>0)

ID: 675445 • Letter: W

Question

Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Euclidean Algorithm to calculate GCD (the Greatest Common Divisor). Please see next page for the Euclidean Algorithm in C++.

// Euclidean Algorithm to calculate GCD

//

// #include "stdafx.h"

#include <iostream>

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 << "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;

}

Explanation / Answer

.Ltext0: .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .globl _Z3GCDii _Z3GCDii: .LFB971: .cfi_startproc 0000 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 0001 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 0004 4883EC10 subq $16, %rsp 0008 897DFC movl %edi, -4(%rbp) 000b 8975F8 movl %esi, -8(%rbp) 000e 837DF800 cmpl $0, -8(%rbp) 0012 7505 jne .L2 0014 8B45FC movl -4(%rbp), %eax 0017 EB13 jmp .L3 .L2: 0019 8B45FC movl -4(%rbp), %eax 001c 99 cltd 001d F77DF8 idivl -8(%rbp) 0020 8B45F8 movl -8(%rbp), %eax 0023 89D6 movl %edx, %esi 0025 89C7 movl %eax, %edi 0027 E8000000 call _Z3GCDii 00 .L3: 002c C9 leave .cfi_def_cfa 7, 8 002d C3 ret .cfi_endproc .LFE971: .section .rodata .align 8 .LC0: 0000 54686520 .string "The Euclidean Algorithm to calculate GCD. " 4575636C 69646561 6E20416C 676F7269 .LC1: 002b 456E7465 .string "Enter a positive integer A: " 72206120 706F7369 74697665 20696E74 .LC2: 0048 456E7465 .string "Enter a positive integer B: " 72206120 706F7369 74697665 20696E74 0065 000000 .align 8 .LC3: 0068 0A457272 .string " Error, please enter a positive (>0) integer." 6F722C20 706C6561 73652065 6E746572 .LC4: 0096 54686520 .string "The GCD is " 47434420 69732000 .text .globl main main: .LFB972: .cfi_startproc 002e 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 002f 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 0032 53 pushq %rbx 0033 4883EC18 subq $24, %rsp .cfi_offset 3, -24 .LBB2: 0037 BE000000 movl $.LC0, %esi 00 003c BF000000 movl $_ZSt4cout, %edi 00 0041 E8000000 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 00 0046 BE000000 movl $.LC1, %esi 00 004b BF000000 movl $_ZSt4cout, %edi 00 0050 E8000000 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 00 0055 488D45E4 leaq -28(%rbp), %rax 0059 4889C6 movq %rax, %rsi 005c BF000000 movl $_ZSt3cin, %edi 00 0061 E8000000 call _ZNSirsERi 00 0066 BE000000 movl $.LC2, %esi 00 006b BF000000 movl $_ZSt4cout, %edi 00 0070 E8000000 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 00 0075 488D45E8 leaq -24(%rbp), %rax 0079 4889C6 movq %rax, %rsi 007c BF000000 movl $_ZSt3cin, %edi 00 0081 E8000000 call _ZNSirsERi 00 .LBB3: 0086 8B45E4 movl -28(%rbp), %eax 0089 85C0 testl %eax, %eax 008b 7E07 jle .L5 008d 8B45E8 movl -24(%rbp), %eax 0090 85C0 testl %eax, %eax 0092 7F1E jg .L6 .L5: 0094 BE000000 movl $.LC3, %esi 00 0099 BF000000 movl $_ZSt4cout, %edi 00 009e E8000000 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 00 00a3 BE000000 movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi 00 00a8 4889C7 movq %rax, %rdi 00ab E8000000 call _ZNSolsEPFRSoS_E 00 00b0 EB53 jmp .L7 .L6: .LBB4: .LBB5: 00b2 8B55E4 movl -28(%rbp), %edx 00b5 8B45E8 movl -24(%rbp), %eax 00b8 39C2 cmpl %eax, %edx 00ba 7D12 jge .L8 .LBB6: 00bc 8B45E4 movl -28(%rbp), %eax 00bf 8945EC movl %eax, -20(%rbp) 00c2 8B45E8 movl -24(%rbp), %eax 00c5 8945E4 movl %eax, -28(%rbp) 00c8 8B45EC movl -20(%rbp), %eax 00cb 8945E8 movl %eax, -24(%rbp) .L8: .LBE6: .LBE5: 00ce 8B55E8 movl -24(%rbp), %edx 00d1 8B45E4 movl -28(%rbp), %eax 00d4 89D6 movl %edx, %esi 00d6 89C7 movl %eax, %edi 00d8 E8000000 call _Z3GCDii 00 00dd 89C3 movl %eax, %ebx 00df BE000000 movl $.LC4, %esi 00 00e4 BF000000 movl $_ZSt4cout, %edi 00 00e9 E8000000 call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 00 00ee 89DE movl %ebx, %esi 00f0 4889C7 movq %rax, %rdi 00f3 E8000000 call _ZNSolsEi 00 00f8 BE000000 movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi 00 00fd 4889C7 movq %rax, %rdi 0100 E8000000 call _ZNSolsEPFRSoS_E 00 .L7: .LBE4: .LBE3: 0105 B8000000 movl $0, %eax 00 .LBE2: 010a 4883C418 addq $24, %rsp 010e 5B popq %rbx 010f 5D popq %rbp .cfi_def_cfa 7, 8 0110 C3 ret .cfi_endproc .LFE972: _Z41__static_initialization_and_destruction_0ii: .LFB981: 012e BF000000 movl $_ZStL8__ioinit, %edi 00 0133 E8000000 call _ZNSt8ios_base4InitC1Ev 00 0138 BA000000 movl $__dso_handle, %edx 00 013d BE000000 movl $_ZStL8__ioinit, %esi 00 0142 BF000000 movl $_ZNSt8ios_base4InitD1Ev, %edi 00 0147 E8000000 call __cxa_atexit 00 .cfi_startproc 0111 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 0112 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 0115 4883EC10 subq $16, %rsp 0119 897DFC movl %edi, -4(%rbp) 011c 8975F8 movl %esi, -8(%rbp) 011f 837DFC01 cmpl $1, -4(%rbp) 0123 7527 jne .L10 0125 817DF8FF cmpl $65535, -8(%rbp) FF0000 012c 751E jne .L10 .L10: 014c C9 leave .cfi_def_cfa 7, 8 014d C3 ret .cfi_endproc .LFE981: _GLOBAL__sub_I__Z3GCDii: .LFB982: .cfi_startproc 014e 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 014f 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 0152 BEFFFF00 movl $65535, %esi 00 0157 BF010000 movl $1, %edi 00 015c E8B0FFFF call _Z41__static_initialization_and_destruction_0ii FF 0161 5D popq %rbp .cfi_def_cfa 7, 8 0162 C3 ret .cfi_endproc .LFE982: .section .init_array,"aw" .align 8 0000 00000000 .quad _GLOBAL__sub_I__Z3GCDii 00000000 .text .Letext0:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote