看老罗的书中有如下一段,
......
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 101DLG_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 :noneinclude 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.libDLG_MAIN equ 1
IDC_COUNT equ 101.data?
hInstance dd ? .const
dwRadius dw 75
dwPara180 dw 180.code_CalcY proc _dwDegree,_dwRadiuslocal @dwReturnfild dwRadiusfild _dwDegreefldpifmulfild dwPara180fdivp st(1),stfcosfild _dwRadiusfmulfsubp st(1),stfistp @dwReturnmov eax,@dwReturnret_CalcY endp_ProcDlgMain proc uses ebx edi esi hWnd,wMsg,wParam,lParammov eax,wMsg.if eax == WM_CLOSEinvoke EndDialog,hWnd,NULL.elseif eax == WM_INITDIALOGinvoke _CalcY,45,20invoke SetDlgItemInt,hWnd,IDC_COUNT,eax,FALSE.elseif eax == WM_COMMAND.elsemov eax,FALSEret.endifmov eax,TRUEret_ProcDlgMain endpstart:invoke GetModuleHandle,NULLmov hInstance,eaxinvoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULLinvoke ExitProcess,NULLend start
构建运行如下;
invoke _CalcY,45,20,改为 invoke _CalcY,90,20,再构建一次,运行如下;