Charles Pegge
10-03-2008, 14:18
The FPU is a stack machine with a depth of 8 registers. So when you use it think Reverse Polish Notation (RPN). Variables are always loaded from memory onto st(0), pushing the other registers down, so the previous st(0) becomes st(1) and so forth. Arithmetic is executed on the st(0) register using st(0) and a memory operand or an st(0..7) register. Operands are popped from the FPU stack once they have served their purpose, leaving the FPU in its original state.
' Using Machine Code with ThinBasic
'---------------------------------------------------------------------------
'---Reference:
' Intel x86 Architecture Ref Manual Vol 2
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
' Syntax rules:
' #Variable patches in long address of Variable (4 bytes)
' NLn patches in long decimal value n (4 bytes)
' comments are indicated by a quote mark '
' all other words are read as hexadecimal bytes.
' An error during MC_Eval$ will produce a string containing &hc3 (ret) only.
'----------------------------------------------
' arithmentic on the Floating Point Processor (FPU)
'
' (vv1+vv2) / (vv3+vv4)
'
' 23 bytes
'----------------------------------------------
dim vv(8) as double
dim sMC as string = MC_Eval$ "
b8 #vv ' vv base address
dd 40 00 ' fld qword ptr [eax+00] ' ( vv1
dc 40 08 ' fadd qword ptr [eax+08] ' + vv2
dd 40 10 ' fld qword ptr [eax+16] ' ) ( vv3
dc 40 18 ' fadd qword ptr [eax+24] ' +vv4
'de c9 ' fmulp st(1) '
de f9 ' fdivp st(1) ' ) /
dd 58 38 ' fstp qword ptr [eax+56] ' = vv8
c3 ' ret '
"
'-------------------------------'
vv(1)=1.5
vv(2)=2
vv(3)=2.5
vv(4)=3
' (vv1+vv2) / (vv3+vv4)
' = 0.636363...
MC_Exec sMC
msgbox 0, vv(8) ' result
' Using Machine Code with ThinBasic
'---------------------------------------------------------------------------
'---Reference:
' Intel x86 Architecture Ref Manual Vol 2
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
' Syntax rules:
' #Variable patches in long address of Variable (4 bytes)
' NLn patches in long decimal value n (4 bytes)
' comments are indicated by a quote mark '
' all other words are read as hexadecimal bytes.
' An error during MC_Eval$ will produce a string containing &hc3 (ret) only.
'----------------------------------------------
' arithmentic on the Floating Point Processor (FPU)
'
' (vv1+vv2) / (vv3+vv4)
'
' 23 bytes
'----------------------------------------------
dim vv(8) as double
dim sMC as string = MC_Eval$ "
b8 #vv ' vv base address
dd 40 00 ' fld qword ptr [eax+00] ' ( vv1
dc 40 08 ' fadd qword ptr [eax+08] ' + vv2
dd 40 10 ' fld qword ptr [eax+16] ' ) ( vv3
dc 40 18 ' fadd qword ptr [eax+24] ' +vv4
'de c9 ' fmulp st(1) '
de f9 ' fdivp st(1) ' ) /
dd 58 38 ' fstp qword ptr [eax+56] ' = vv8
c3 ' ret '
"
'-------------------------------'
vv(1)=1.5
vv(2)=2
vv(3)=2.5
vv(4)=3
' (vv1+vv2) / (vv3+vv4)
' = 0.636363...
MC_Exec sMC
msgbox 0, vv(8) ' result