(fldcw [sp]) 汇编代码中的控制字问题

(fldcw [sp]) Control Word issue in assembly code

我对汇编还很陌生,它是我的第一门编程语言。

我对这行有疑问:fldcw [sp]。它会导致构建错误:error A2031: must be index or base register

我知道:

sp是16位栈指针

esp是32位栈指针

-

我正在尝试学习如何使用 FPU 控制字。 我使用 Skylake 处理器。

我的所有信息都在:http://www.website.masmforum.com/tutorials/fptute/fpuchap3.htm#fstcw

当我用 sp 替换 esp 时,它构建得很好。

我是不是对指南有误解? 什么可能导致此错误?

.386
.model flat, stdcall
option casemap :none  

includelib \masm32\lib\msvcrt.lib
sprintf proto C :vararg
includelib \masm32\lib\user32.lib 
MessageBoxA proto :ptr,:ptr,:ptr,:DWORD
includelib \masm32\lib\kernel32.lib
ExitProcess proto :dword 

.data
   _title db "Result",13,10,0
   $interm db "%0.4f","+","%0.5f",13,10,0
   oldcw   dw   ?



.code
main PROC
LOCAL szBuf[9]:byte




  fstcw oldcw     ;get the current Control Word to retain all setting bits
                  ;not related to the rounding control (RC) bits
  fwait       ;to insure the storage instruction is completed
  mov   ax,oldcw
; and   ax,0F3FFh ;clears only the RC bits, leaving all other bits unchanged
                  ;not necessary here because both bits will be set
  or    ax,0C00h  ;this will set both bits of the RC field to the truncating mode
                  ;without affecting any of the other field's bits
  push  eax       ;use the stack to store the modified Control Word in memory
  fldcw [sp]      ;load the modified Control Word



  fldcw oldcw     ;restore the previous Control Word
  pop   eax       ;clean-up the stack
                  ;this could also retrieve a 16-bit or 32-bit integer
                  ;possibly returned by the "other FPU instruction(s)"

Finished:  


   invoke sprintf, addr szBuf, offset $interm, eax, edx
   invoke MessageBoxA, 0, addr szBuf, offset _title, 0
   invoke ExitProcess, 0



main ENDP
END main

在 16 位模式下,[sp] 不是有效的内存操作数。只有以下内存操作数是有效的,每个操作数都有一个可选的位移:

[bx]
[bx+si]
[bx+di]
[bp]
[bp+si]
[bp+di]
[si]
[di]
[addr16]

要修复您的代码,我建议您设置堆栈框架并使用 bp-相对寻址:

push bp       ; establish stack frame
mov bp,sp     ; dito
fstcw oldcw
fwait
mov ax,oldcw
or ax,0C00h
push ax
fldcw [bp-2]  ; load control word from stack
leave         ; tear down stack frame

然而,如果您处于 32 位模式,您应该只参考 esp,而不是 32 位堆栈指针。永远不要在内存操作数中使用 16 位寄存器,除非你处于 16 位模式或确切地知道你在做什么:

fstcw oldcw
fwait
mov ax,oldcw
or ax,0C00h
push eax
fldcw [esp]   ; load control word from stack
pop eax       ; restore stack