title: Least common multiple and gcd in assembly
body: <p>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".</p>
  
  <p>Here is the code:</p>
  
  <pre><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 &gt; %rsi &lt;==&gt; x &gt; y
          sub %rsi, %rdi
  
          jmp .comp
  
  .found:
          div %rdi
  
          ret
  
          .size   lcm_gcd, . - lcm_gcd
  </code></pre>
  
  <p>And the test program in C:</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  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;
  }
  </code></pre>