Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GCD algorithm This week we are working on the first part of the exercises in Chapter 7 . Assignment objective: After completing this assignment, you

GCD algorithm
This week we are working on the first part of the exercises in Chapter 7.
Assignment objective: After completing this assignment, you will have completed the following:
Analyze the shift and division applications
Implement multiplication and division instructions into a program
Create one program with all parts below. Be sure to copy your program into a .txt file for submission, with the output at the bottom. DON'T USE ANY CONDITIONAL CONTROL FLOW DIRECTIVES.
A. The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. Implement a function using the GCD algorithm that involves integer division in a loop, described by the following pseudocode:
C. Put all of this in a loop that performs the GCD 3 times. Make sure one of the times uses negative numbers to test the absolute value.
If you have any questions, you can post them on the discussion board or send me an email. You can email me programs to look at and provide help, at any time.
Keep it up!
I NEED MIPS VERSION THIS IS C VERSION I NEED MIPS VERSION BUT I GOT ERROR
.data
prompt: .asciiz "Enter two integers (one of them can be negative for testing): "
result_msg: .asciiz "GCD of %d and %d is: %d
"
newline: .asciiz "
"
.text
.globl main
# Function to calculate GCD
GCD:
li $t0,0 # Initialize remainder to 0
bgtz $a0, absX # Branch if x is positive
neg $a0, $a0 # Negate x if it's negative
absX:
bgtz $a1, absY # Branch if y is positive
neg $a1, $a1 # Negate y if it's negative
absY:
beq $a1, $zero, done # If y is zero, return x
gcdLoop:
div $a0, $a1 # Divide x by y
mfhi $t0 # Remainder stored in t0
move $a0, $a1 # Move y into x
move $a1, $t0 # Move remainder into y
bnez $a1, gcdLoop # If y !=0, repeat loop
done:
move $v0, $a0 # Return GCD in v0
jr $ra
main:
li $t2,3 # Loop counter
loop:
beqz $t2, exit_loop # Exit loop if it's been executed 3 times
# Print prompt
li $v0,4
la $a0, prompt
syscall
# Read x
li $v0,5
syscall
move $t0, $v0 # Store x in $t0
# Read y
li $v0,5
syscall
move $t1, $v0 # Store y in $t1
# Ensure the larger number is passed first
bge $t0, $t1, calcGCD # If x >= y, calculate GCD
move $t4, $t0 # Otherwise, swap x and y
move $t0, $t1
move $t1, $t4
calcGCD:
move $a0, $t0 # Pass x
move $a1, $t1 # Pass y
jal GCD # Call GCD function
move $t3, $v0 # Store GCD in $t3
# Print result message
li $v0,4
la $a0, result_msg
syscall
# Print x
li $v0,1
move $a0, $t0
syscall
# Print " and "
li $v0,4
la $a0, newline
syscall
# Print y
li $v0,1
move $a0, $t1
syscall
# Print " is: "
li $v0,4
la $a0, newline
syscall
# Print GCD
li $v0,1
move $a0, $t3
syscall
# Print newline
li $v0,4
la $a0, newline
syscall
sub $t2, $t2,1 # Decrement loop counter
j loop
exit_loop:
li $v0,10 # Exit program
syscall
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions