MASM汇编 - REAL4浮点指令

问题描述 投票:1回答:1

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

  1. 从用户获取输入并存储在数组中?例如,使用REAL4号码执行此操作。 mov array[ebx], sval(input())
  2. 打印浮动值。另一个例子, mov eax, array[ebx] print str$(eax)

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

assembly floating-point x86 masm32
1个回答
1
投票

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

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

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

让我们首先输出一个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
© www.soinside.com 2019 - 2024. All rights reserved.