Question

Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change...

Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change if the smaller is subtracted from the larger.

Write a program (in MIPS assembly) that implements this algorithm to find the GCD of two integers. Follow this algorithm:

1. Call the two integers a and b.

2. If a and b are equal: stop: a is the GCD.

3. Subtract the smaller from the larger. Replace the larger with the result

4. Repeat steps 2 thru 4

The program asks the user for each of two integers (use exception handler services), then use the algorithm to compute the GCD, then write out the GCD. Then use service 10 to halt the program. You may wish to write the program in C first or perhaps create a structured flow chart before you write the assembler program. Assume that the user enters two positive integers:

Enter First Integer: 3791 Enter Second Integer: 8687 The GCD is: 17

Set MIPS settings to the following:

OFF Bare Machine OFF Enable Delayed Loads OFF Enable Delayed Branches ON Load Exception Handler OFF Enable Mapped IO ON Accept Pseudo Instructions

Set these options as specified or QtSpim will start up with options you don’t want. You may have to set the options, close QtSpim, and then restart it for the options to have an effect. Use only those instructions that have been discussed in the notes through chapter 22.

Include a register use table in the documentation at the top of your source program. The source program should be nicely formatted. Labels (symbolic addresses) should start in column one. Nearly every line of the program will have a meaningful comment. Labels start in column one. All mnemonics start in the same column, perhaps column 10. All comments should start in the same column, perhaps column 40. Make sure that the source file has no tab characters in it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Requirement: As per the problem, we need to write a program (in MIPS assembly) that implements this algorithm to find the GCD of two integers.

Understanding: We need to follow below algorithm to find the GCD of two integers:

1. Call the two integers a and b.

2. If a and b are equal: stop: a is the GCD.

3. Subtract the smaller from the larger. Replace the larger with the result

4. Repeat steps 2 thru 4

The program asks the user for each of two integers (use exception handler services), then use the algorithm to compute the GCD, then write out the GCD. Then use service 10 to halt the program.

Solution: Below is the implementation of above problem.

# Start with data declarations
#
   .data
str1: .asciiz "Enter first integer n1: "
str2: .asciiz "Enter first integer n2: "
str3: .asciiz "The greatest common divisor of n1 and n2 is "
newline: .asciiz "\n"

.align 2

.globl main
.text

main:

# ---------------------------------------------------------
# Receiving two integer inputs from user
# ---------------------------------------------------------
    la $a0, str1           # load str1 address into $a0
    li $v0, 4              # load I/O code for string output
    syscall                # output str1

    li $v0, 5              # load I/O code for integer input
    syscall                # input integer n1 into $v0
    add $a1, $v0, $zero    # store n1 in $a1

    la $a0, str2           # load str2 address into $a0
    li $v0, 4              # load I/O code for string output
    syscall                # output str2

    li $v0, 5              # load I/O code for integer input
    syscall                # input integer n2 into $v0
    add $a2, $v0, $zero    # store n2 in $a2

# ---------------------------------------------------------
# Calling gcd
# ---------------------------------------------------------
    addi $sp, $sp, -8      # adjust stack for 2 items
    sw $a1, 4($sp)         # save argument1
    sw $a2, 0($sp)         # save argument2
    jal gcd                # jump to subroutine 'gcd'
    lw $a2, 0($sp)         # load argument2
    lw $a1, 4($sp)         # load argument1
    addi $sp, $sp, 4       # pop 1 item from the stack; reuse stack space for $s0
    add $s0, $v0, $zero    # $s0 = result from gcd
    sw $s0, 0($sp)         # save the result from gcd

# ---------------------------------------------------------
# Outputting Results
# ---------------------------------------------------------
    la $a0, str3           # load str3 address into $a0
    li $v0, 4              # load I/O code for string output
    syscall                # output str3
    li $v0, 1              # load I/O code for integer output
    add $a0, $s0, $zero    # $a0 = $s0; put gcd result in $a0 for output
    syscall                # output result from gcd
    la $a0, str4           # load str4 address into $a0
    li $v0, 4              # load I/O code for string output
    syscall                # output a newline

    li $v0, 10             # load code for terminating program
    syscall                # terminate program


# ---------------------------------------------------------
# Calculates the greatest common divisor
# ---------------------------------------------------------
gcd:
    addi $sp, $sp, -8      # adjust stack for 2 items
    sw $ra, 4($sp)         # save the return address
# ---------------------------------------------------------
# Recursive Step (Part 1)
    div $a1, $a2           # divide arg1 by arg2; remainder in $HI
    mfhi $s0               # $s0 = arg 1 % arg2
    sw $s0, 0($sp)         # save the remainder
    bne $s0, $zero, L1     # recurse if $s0 != 0
# ---------------------------------------------------------
# Base Case
    add $v0, $a2, $zero   # argument2 holds result, store in $v0
    addi $sp, $sp, 8      # pop 2 items from stack
    jr $ra                # return
# ---------------------------------------------------------
# Recursive Step (Part 2)
L1:
    add $a1, $a2, $zero   # set arg1 to arg2
    lw $s0, 0($sp)        # load remainder
    add $a2, $s0, $zero   # set arg2 to remainder
    jal gcd               # recursive call
# ---------------------------------------------------------
# Exit Recursion
    lw $ra, 4($sp)        # load previous return address
    addi $sp, $sp, 8      # pop 2 items from the stack
    jr $ra                # return

Add a comment
Know the answer?
Add Answer to:
Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to evaluate the arithmetic expression: (25xy - 10x - 6y + 28)/7 Use...

    Write a program to evaluate the arithmetic expression: (25xy - 10x - 6y + 28)/7 Use symbolic addresses x, y, answer, and remainder. Pick two registers for x and y and load them from memory. At the end of the program, store the answer and remainder to memory. Assume that the values are small enough so that all results fit into 32 bits. Since load delays are turned on in SPIM be careful what instructions are placed in the load...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • Write a C++ program that reads in two integers and then computes the greatest common divisor...

    Write a C++ program that reads in two integers and then computes the greatest common divisor GCD using recursion. DO NOT USE A BUILT-IN FUNCTION SUCH AS "gcd" to do this.

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • The second phase of your semester project is to write pass one of a two‑pass assembler...

    The second phase of your semester project is to write pass one of a two‑pass assembler for the SIC assembler language program. As with Phase 1, this is to be written in C (not C++) and must run successfully on Linux. Pass one will read each line of the source file, and begin the process of translating it to object code. (Note: it will be to your advantage to have a separate procedure handle reading, and perhaps tokenizing, the source...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT