Answered step by step
Verified Expert Solution
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
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 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:
resultmsg: asciiz GCD of d and d is: d
newline: asciiz
text
globl main
# Function to calculate GCD
GCD:
li $t # Initialize remainder to
bgtz $a absX # Branch if x is positive
neg $a $a # Negate x if it's negative
absX:
bgtz $a absY # Branch if y is positive
neg $a $a # Negate y if it's negative
absY:
beq $a $zero, done # If y is zero, return x
gcdLoop:
div $a $a # Divide x by y
mfhi $t # Remainder stored in t
move $a $a # Move y into x
move $a $t # Move remainder into y
bnez $a gcdLoop # If y repeat loop
done:
move $v $a # Return GCD in v
jr $ra
main:
li $t # Loop counter
loop:
beqz $t exitloop # Exit loop if it's been executed times
# Print prompt
li $v
la $a prompt
syscall
# Read x
li $v
syscall
move $t $v # Store x in $t
# Read y
li $v
syscall
move $t $v # Store y in $t
# Ensure the larger number is passed first
bge $t $t calcGCD # If x y calculate GCD
move $t $t # Otherwise, swap x and y
move $t $t
move $t $t
calcGCD:
move $a $t # Pass x
move $a $t # Pass y
jal GCD # Call GCD function
move $t $v # Store GCD in $t
# Print result message
li $v
la $a resultmsg
syscall
# Print x
li $v
move $a $t
syscall
# Print and
li $v
la $a newline
syscall
# Print y
li $v
move $a $t
syscall
# Print is:
li $v
la $a newline
syscall
# Print GCD
li $v
move $a $t
syscall
# Print newline
li $v
la $a newline
syscall
sub $t $t # Decrement loop counter
j loop
exitloop:
li $v # Exit program
syscall
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started