I need just the code ready to run Assembly code Greatest Common Divisor (GCD) Th
ID: 3823831 • Letter: I
Question
I need just the code ready to run
Assembly code
Greatest Common Divisor (GCD) The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division in a loop, described by the following pseudocode: int GCD(int x, int y) {x = abs(x)//absolute value y = abs(y) do {int n = x % y x = y y = n} while (y > 0) return x} Implement this function in assembly language and write a test program that calls the function several times, passing it different values. Display all results on the screen.Explanation / Answer
CODE:
.text
.globl GCD
.type GCD, @function
GCD:
push %rbp
mov %rbp,%rsp
mov -20(%rbp),%rdi
mov -24(%rbp),%rsi
mov %rax,-20(%rbp)
cmp %rax,0
jns L2
mov %rax,-20(%rbp)
xor %rax,%rax
mov -20(%rbp),%rax
L2:
mov %rax,-24(%rbp)
cmp %rax,0
jns L3
mov %rax,-24(%rbp)
xor %rax,%rax
mov -24(%rbp),%rax
L3:
mov %rax,-20(%rbp)
cdq
idivq -24(%rbp)
mov -4(%rbp),%rdx
mov %rax,-24(%rbp)
mov -20(%rbp),%rax
mov %rax,-4(%rbp)
mov -24(%rbp),%rax
mov %rax,-24(%rbp)
cmp %rax,0
jle L4
jmp L3
L4:
mov %rax,-20(%rbp)
pop %rbp
ret
NOTE: If you get errors change all rax..etc to eax and idivq to idiv