Question

In: Computer Science

Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest...

Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest common divisor.

*I am using Eclipse DS-5 Community Workspace with A64 Instruction Set)

Use the following algorithm:

// Given two integers m and n:

if (m < n)

gcd(n, m)

if n is a divisor of m

gcd(m, n) = n

else

gcd (m, n) = gcd (n, m % n)
Your program must be recursive. You must create a function that calls itself, and saves variables to the stack, and creates a stack frame. Your program should restore the stack as it returns from recursive calls.

Your program should have the following prompt:

"Enter Two Integers: "

Your program should then output the greatest common divisor then terminate.

Sample Test Cases

Enter Two Integers: 0 37
37

Enter Two Integers: 1 37
1

Enter Two Integers: -1 37
1

Enter Two Integers: 5 75
5

Enter Two Integers: -10 -100
10

Enter Two Integers: -10 100
10

Note: gcd(0,0) will not be tested.

Solutions

Expert Solution

CODE

                .text
        .globl  main
main:
        sub     $sp,$sp,12      # push stack
        sw      $ra,4($sp)      # save return address

        li      $v0,4       # Ready for string output
        la      $a0,pow     # Load address of pow
        syscall         # Print string
        
        li $v0,5        # Ready for integer input
        syscall         # Read integer from console
        move $t2,$v0    # Move into to $t2 temporarily
                
        li      $v0,4       # Ready for string output
        la      $a0,bas     # Load address of bas
        syscall         # Print string
        
        li $v0,5        # Ready for integer input
        syscall         # Read integer from console
        move $a0,$t2    # Move into to $a0
        move $a1,$v0    # Move power integer to $a1
        
        jal euc
        sw      $v0,8($sp)

# print the result
        
        li      $v0,4
        la      $a0,str
        syscall

        li      $v0,1
        lw      $a0,8($sp)
        syscall
        
        lw      $ra,4($sp)      # restore return address
        add     $sp,$sp,12      # pop stack
        jr      $ra

        .data
str:                    # label of address containing a string
        .asciiz "GCD = "  # Assembly directive used to create a null terminated ASCII string
bas:                    # label of address containing a string
        .asciiz "Enter second integer = "  # Assembly directive used to create a null terminated ASCII string
pow:                    # label of address containing a string
        .asciiz "Enter first integer = "  # Assembly directive used to create a null terminated ASCII string
        
        .text
euc:
        sub     $sp,$sp,8       # push stack
        sw      $ra,4($sp)      # save return address

        bne $a1, $zero, L1 # if b!=0 then exit
        add     $v0,$zero,$a0   # return a0
        add     $sp,$sp,8       # pop stack
        jr      $ra             # return to calling procedure
L1:
        move $t4,$a1         # set up c = b
        rem $a1,$a0,$a1      # b = a % b
        move $a0,$t4         # a = c
        jal euc            # recurse

        lw      $ra,4($sp)      # restore previous return addr
        move $v0,$a0    # Move a to $v0

        add     $sp,$sp,8       # pop stack
        jr      $ra             # return to calling procedure

Related Solutions

Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a program in python that takes a date as input and outputs the date's season....
Write a program in python that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Write a program that takes, as input, five numbers and outputs the mean (average) and standard...
Write a program that takes, as input, five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x₁, x₂, x₃, x₄, and x₅, then the mean is: x = (x₁+ x₂+ x₃+ x₄+x₅)/5 and the standard deviation is: s = √(((x₁-x)²+(x₂-x)²+(x₃-x)²+(x₄-x)²+(x₅-x)²)/5) Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation. Format your output with setprecision(2) to ensure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT