内联汇编与双precision号工作(GCC,IA-32) [英] Working with double-precision numbers in inline assembly (GCC, IA-32)

查看:141
本文介绍了内联汇编与双precision号工作(GCC,IA-32)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习汇编在我的计算机科学类,我有一个分配使用指定的舍入模式为圆一个浮点值。我试图实现这一点使用 FSTCW FLDCW FRNDINT 。我修改整控制位,轮数,然后还原previous控制位(分配的要求)。

I'm just starting to learn assembly in my computer science class, and I have an assignment to round a floating-point value using a specified rounding mode. I've tried to implement this using fstcw, fldcw, and frndint. I modify the rounding control bits, round the number, and then restore the previous control bits (a requirement of the assignment).

当前突出的问题是,指令 FLD%1 似乎加载错误的值到 ST(0)浮点寄存器(例如,如果我调用函数的2.6207值,数量-1.9427(...)E-29被加载到寄存器)。这可能是由于 GCC的误用的内联 ASM(),还是其他什么东西,但我不知道为什么会发生。

The current outstanding problem is that the instruction fld %1 seems to load the wrong value into the st(0) floating-point register (for example, if I call the function with a value of 2.6207, the number -1.9427(...)e-29 gets loaded into the register). This may be due to a misuse of gcc's inline asm(), or something else, but I'm not sure why it happens.

下面是我有:

double roundD (double n, RoundingMode roundingMode)
{
    // control word storage (2 bytes for previous, 2 for current)
    char *cw = malloc(4*sizeof(char));
    char *cw2 = cw + 2;

    asm("fstcw %3;" // store control word in cw
        "mov %3,%4;" // copy control word into cw2
        "and $0xF3FF,%4;" // zero out rounding control bits
        "or %2,%4;" // put new mode into rounding control bits
        "fldcw %5;" // load the modified control word
        "fld %1;" // load n into st(0)
        "frndint;" // round n
        "fstp %0;" // load st(0) back into n
        "fldcw %3;" // load the old control word from cw
        : "=m" (n)
        : "m" (n), "m" (roundingMode),
          "m" (cw), "r" (cw2), "m" (cw2) // mov requires one argument in a register
        );

    free(cw);

    return n;
}

我倒是AP preciate任何指针有什么不妥code,具体涉及 FLD%1 线和 ASM 输入/输出。 (当然,如果你能找到其他的问题,请随时让我知道他们。)我不想让任何人做功课对我来说,只是点我朝着正确的方向。谢谢!

I'd appreciate any pointers to what's wrong with that code, specifically relating to the fld %1 line and the asm inputs/outputs. (Of course, if you can find other problems, feel free to let me know about them as well.) I don't want anyone to do my homework for me, just point me in the right direction. Thanks!

推荐答案

至少有一个问题与当前的code是它使用的是单precision浮动FLD和FSTP点的版本。如果用fldl替换他们,fstpl它可能会工作。

At least one issue with your current code is it is using the single precision floating point versions of fld and fstp. If you replace them with fldl and fstpl it will probably work.

这篇关于内联汇编与双precision号工作(GCC,IA-32)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆