看老罗的书中有如下一段,
......
fild dwRadius
fild _dwDegree
fldpi
fmul ;角度*Pi
fild dwPara180
fdivp st(1),st ;角度*Pi/180
fsin ;Sin(角度*Pi/180)
fild _dwRadius
fmul ;半径*Sin(角度*Pi/180)
......
这些指令看上去很陌生;初步看了一下,这些是Intel汇编的浮点运算指令,其中的st是,St0 到 St7 是 8个浮点栈寄存器;
它有一个过程,传入2个数值进行计算,计算后把结果存入eax,然后返回;
先单独做程序看一下它的浮点运算的这个子过程;
先做ftest.rc,
#include <resource.h>
#define DLG_MAIN 1
#define IDC_COUNT 101
DLG_MAIN DIALOG 50, 50, 113, 40
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "例子"
FONT 9, "宋体"
{
LTEXT "", IDC_COUNT, 62, 16, 40, 10
}
定义一个对话框,其中只有一个静态文本控件,控件ID为IDC_COUNT,用来显示计算结果;
asm代码,先把它的,
_CalcY proc _dwDegree,_dwRadius
......
_CalcY endp
这个子过程加入;
此过程需要在.const段定义2个常量,
dwRadius dw 75
dwPara180 dw 180
然后在对话框过程的WM_INITDIALOG消息处理分支中,调用_CalcY过程,
invoke _CalcY,45,20
调用了之后计算结果在eax中,然后调用SetDlgItemInt函数显示eax中的内容;
invoke SetDlgItemInt,hWnd,IDC_COUNT,eax,FALSE
最终的asm代码如下;
.386
.model flat, stdcall
option casemap :none
include D:\masm32\include\windows.inc
include D:\masm32\include\user32.inc
include D:\masm32\include\kernel32.inc
include D:\masm32\include\gdi32.inc
includelib D:\masm32\lib\user32.lib
includelib D:\masm32\lib\kernel32.lib
includelib D:\masm32\lib\gdi32.lib
DLG_MAIN equ 1
IDC_COUNT equ 101
.data?
hInstance dd ?
.const
dwRadius dw 75
dwPara180 dw 180
.code
_CalcY proc _dwDegree,_dwRadius
local @dwReturn
fild dwRadius
fild _dwDegree
fldpi
fmul
fild dwPara180
fdivp st(1),st
fcos
fild _dwRadius
fmul
fsubp st(1),st
fistp @dwReturn
mov eax,@dwReturn
ret
_CalcY endp
_ProcDlgMain proc uses ebx edi esi hWnd,wMsg,wParam,lParam
mov eax,wMsg
.if eax == WM_CLOSE
invoke EndDialog,hWnd,NULL
.elseif eax == WM_INITDIALOG
invoke _CalcY,45,20
invoke SetDlgItemInt,hWnd,IDC_COUNT,eax,FALSE
.elseif eax == WM_COMMAND
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
end start
构建运行如下;
invoke _CalcY,45,20,改为 invoke _CalcY,90,20,再构建一次,运行如下;