MASM 汇编 - REAL4 浮点指令

MASM assembly - REAL4 float instructions

我正在尝试使用 REAL4 数据类型将浮点数存储在数组中。以下的正确说明是什么?

  1. 获取用户输入并存储在数组中?例如这样做,但使用 REAL4 数字。

    mov array[ebx], sval(input())
    
  2. 打印浮点值。另一个例子,

    mov eax, array[ebx]
    print str$(eax)
    

此外,如果有任何指向有用的 MASM real4 指令文档的链接,我将不胜感激,谢谢!

REAL4 数字就像 DWORD 一样只是一堆 32 位,但以不同的方式解释。如果您不需要特殊的 MASM 选项和 REAL4 检查,您也可以使用 MASM 类型 DWORD resp。 SDWORD 或常见的汇编器类型 DD.

对于大多数外部函数,您必须将 REAL4 数(单精度浮点格式)转换为 REAL8 数(双精度浮点格式)。最简单的方法是将单数加载到 FPU 中并将其存储为双数。

来自控制台的输入将存储为字符串。您必须将此字符串转换为所需的格式。

让我们先输出一个 REAL4 数字数组:

INCLUDE \masm32\include\masm32rt.inc

.DATA
    result  REAL8 0.0
    array   REAL4 -1.0, 1.2, 2.3, 3.4, 4.567, 0.0
            SDWORD -1               ; End of array -> NaN

.CODE
main PROC
    xor ebx, ebx

    @@:
    mov eax, DWORD PTR array[ebx]   ; "DWORD PTR" = "REAL4 PTR"
    cmp eax, -1                     ; NaN = end of array?
    je @F                           ; Yes -> Jump to the next @@

    fld DWORD PTR array[ebx]        ; Load a single into FPU ...
    fstp QWORD PTR result           ; ... and store it as double
    printf("%f\n",result)           ; MASM32 macro that acts like the C function

    add ebx, 4                      ; REAL4 has 4 bytes
    jmp @B                          ; Jump to the previous @@

    @@:
    exit 0

main ENDP

END main

现在让我们输入几个数字并打印出来。有 16 个变量的位置。该程序不检查该限制。您只需键入 ENTER(不带数字)即可结束输入:

INCLUDE \masm32\include\masm32rt.inc
INCLUDE \masm32\macros\macros.asm

.DATA
    result  REAL8 0.0
    lpstring DWORD 0
    array   REAL4 16 DUP (0.0)
            SDWORD -1                           ; End of array -> NaN

.CODE
main PROC

    xor ebx, ebx

    @@:
    mov esi, input("Enter number here ",62," ") ; Input string ... STRING!
    cmp BYTE PTR [esi], 0;                      ; Nothing inputted?
    je @F                                       ; Yes -> jump forward to the next @@

    push ebx                                    ; StrToFloat changes EBX! So it is to save
    INVOKE StrToFloat, esi, ADDR result         ; Convert string to double
    pop ebx                                     ; Restore the saved EBX

    fld REAL8 PTR result                        ; Load a double ...
    fstp REAL4 PTR array[ebx]                   ; ... and save it as single
    mov eax, -1                                 ; NaN = end of array
    mov DWORD PTR array[ebx+4], eax             ; Store the NaN as the next element

    add ebx, 4                                  ; Pointer to the next REAL4 in array
    jmp @B                                      ; Jump back to the previous @@

    @@:
    xor ebx, ebx

    @@:
    mov eax, DWORD PTR array[ebx]               ; "DWORD PTR" = "REAL4 PTR"
    cmp eax, -1                                 ; NaN = end of array?
    je @F                                       ; Yes -> Jump to the next @@

    fld DWORD PTR array[ebx]                    ; Load a single into FPU ...
    fstp QWORD PTR result                       ; ... and store it as double
    printf("%f\n",result)                       ; MASM32 macro that acts like the C function

    add ebx, 4                                  ; REAL4 has 4 bytes
    jmp @B                                      ; Jump to the previous @@

    @@:

    exit 0

main ENDP

END main