REAL4 数字只是一堆 32 位,如 DWORD,但以不同的方式解释。如果您不需要特殊的 MASM 选项并检查 REAL4,您也可以使用 MASM 类型的 DWORD 。 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