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

时间:2017-08-21 01:15:56

标签: assembly x86 masm masm32

我对装配而言相当新这是我的第一个编程语言。

我对此行有疑问:fldcw [sp]。它会导致构建错误:错误A2031:必须是索引或基址寄存器

我知道:

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

1 个答案:

答案 0 :(得分:1)

在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