I'm trying to write a program, which calculates lcm and gcd of two unsigned longs in assembly x86-64, but I get some floating point error, when I'm launching my program. The error is in polish, but I can translate it into "Floating point operations error".
Here is the code:
.global lcm_gcd
.type lcm_gcd,@function
.section .text
lcm_gcd:
mult = %rdx
mov %rdi, %rax
mul %rsi
mov %rax, mult
.comp:
cmp %rdi, %rsi # gcd
je .found
jb .secPos
sub %rdi, %rsi
jmp .comp
.secPos: # %rdi > %rsi <==> x > y
sub %rsi, %rdi
jmp .comp
.found:
div %rdi
ret
.size lcm_gcd, . - lcm_gcd
And the test program in C:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned long lcm, gcd;
} result_t;
result_t lcm_gcd(unsigned long x, unsigned long y);
int main(int argc, char **argv) {
result_t r = lcm_gcd(atol(argv[1]), atol(argv[2]));
printf("%lu, %lu\n", r.lcm, r.gcd);
return 0;
}