【问题标题】:Compare float sum比较浮点数
【发布时间】:2017-11-02 04:01:00
【问题描述】:

我正在开发一个执行算术运算的 MASM32 项目。我们必须使用协处理器(8087 指令集)才能使用浮点单元。所以,假设我的浮点限制是100.0,那么每个数字都必须小于限制。我试图将两个数字相加,然后比较结果,但它不起作用。

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
include c:\masm32\include\masm32rt.inc
.data
    ;Variables de comparación.
    FP_max DD 100
    ;Variables volcadas de la tabla de símbolos.
    _a DD 25
    _b DD 80.0
.code
    start:
         fild _a
         fild _b
         fadd
         fcom FP_max       ;compare ST(0) with the value of the real8_var variable
         fstsw ax          ;copy the Status Word containing the result to AX
         sahf              ;transfer the condition codes to the CPU's flag register
         ja overflow       ;when all flags are 0

        jmp end_program

        overflow:
            print "ERROR: overflow.", 13, 10, 0
            jmp end_program
        end_program:
            invoke ExitProcess, 0
    end start

我的问题是:我做错了什么?它怎么能解决它?谢谢!

【问题讨论】:

  • 您可以直接使用fcomi 设置标志,就像您使用fstsw ax / sahf 一样。 (我认为需要 ppro,即过去 20 年的任何 x86。)

标签: assembly masm masm32 x87


【解决方案1】:

fild 是一个整数->FP 加载。 fld 是一个浮动负载。 (您的汇编程序通过查看标签后的第一个指令神奇地推断出您想要基于 32 位加载。同一指令可以加载 64 位整数或 FP。)

dd 根据表达式中是否有小数点为数字选择整数或浮点编码。 (MASM 可能还有其他强制 FP 解释的方法,例如 REAL4。(Float data example using Masm)


您使用fild _b,但_b 拥有一个32 位二进制浮点值。将该位模式视为 32 位整数可能不是您的本意。

使用_b DD 80 代替_b DD 80.0,或使用fld _b 代替fild _b

FP_max DD 100 存在相反的问题,因为您使用 fcom 从内存中加载它。设为REAL4

(或使用ficom,但请注意,没有ficomi 指令可以与整数进行比较并直接设置标志。有fcomi(您应该使用)比较浮点数,还有ficomfild + fcomp 但不需要额外的 FP 寄存器。与 fiadd 的想法相同。

您可能应该使用 faddpfcomip 来弹出 x87 堆栈,这样您就不会让它不平衡。

标签 wiki 中查看一些 x87 链接。

【讨论】:

  • 但在这种情况下,我该如何定义像 1,32 这样的数字?
  • @TomiSebastiánJuárez。哦,我假设您想要整数,因为您使用的是fild 而不是普通的fld。你应该在问题中准确地说出你想要的。更新了我的答案。
猜你喜欢
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 2011-10-21
相关资源
最近更新 更多