x86架构下,一般通过专用协处理器计算浮点数。浮点数运算通过FPU(Float Point Unit,浮点运算单元计算)计算,提供了浮点栈寄存器st0~st7。
fld mem ;浮点数入栈
fmul mem ;st(0)=st(0)*mem
fst mem ;st(0)转换为指定精度的浮点数
fstp mem;同fst,并弹出st0
另还 fdiv ,fsqrt,fist,fistp等等常用浮点数指令。
例:
fmul_proc proc
local x:DWORD
local y:DWORD
mov x,3FC00000h;1.5
fld x
mov y,4019999Ah;2.4
fmul y; st(0)=st(0)*y
call print_esp
sub esp,8
fst QWORD PTR [esp] ;默认double型
push offset szfmt_003
call crt_printf
add esp,12
call print_esp
sub esp,8
sub esp,8
fst QWORD PTR [esp]
push offset szfmt_003
call crt_printf
add esp,4
call print_esp
fstp x;弹出 st(0)
mov eax,x
ret
fmul_proc endp
注:
c库函数printf函数,格式化%f仅支持double型数据,所以入栈参数为8字节( sub esp,8 )