汇编中最少的常见倍数和gcd

时间:2017-04-15 17:00:51

标签: assembly x86-64 greatest-common-divisor lcm

我试图编写一个程序,它在程序集x86-64中计算两个无符号长整数的lcm和gcd,但是当我启动程序时,我得到一些浮点错误。错误很简单,但我可以将其翻译成"浮点运算错误"。

以下是代码:

    .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

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;
}

0 个答案:

没有答案