Charles Pegge
10-03-2008, 19:45
This shows the conversion of an angle in degrees to radians to Sine and Cosine to radians to degrees.
The final answer is expressed by the FPU within +/- 180 degree range.
By retaining sine and cosine data - the quadrant info is never lost, as happens with a tangent.
Also, tangent infinity problems are avoided.
Note that the FPU can produce the Sine and Cosine with a single instruction. FSINCOS
' 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)
' 360 degree sine cosine and arctangent
'----------------------------------------------
dim vv(8) as double
dim sMC as string = MC_Eval$ "
b8 #vv ' ' vv base address
dd 40 08 ' fld qword [eax+08] ' angle degrees in vv2
c7 00 NL180 ' mov dword [eax],180 ' temp store vv0 integer 180
da 30 ' fidiv dword [eax] ' angle in degrees
d9 eb ' fldpi ' pi
de c9 ' fmulp st(1) ' multiply by pi
d9 fb ' fsincos ' produce sine st(0) and cosine st(1)
dd 50 38 ' fst qword ptr [eax+56] ' sin in vv8
d9 c9 ' fxch st(1) ' swap to save cos
dd 50 30 ' fst qword ptr [eax+48] ' cos in vv7
d9 c9 ' fxch st(1) ' swap back again
d9 f3 ' fpatan ' arctangent pops 1 then result in st(0)
dd 50 28 ' fst qword ptr [eax+40] ' store arctan radians in vv6
d9 eb ' fldpi ' load pi
de f9 ' fdivp st(1) ' divide angle by pi
da 08 ' fimul dword ptr [eax] ' multiply by 180
dd 58 20 ' fstp qword ptr [eax+32] 'store angle degrees in vv5
c3 ' ret '
"
'-------------------------------'
vv(2)=300 ' test angle
MC_Exec sMC
msgbox 0, "Initial angle: "+vv(2)+$crlf_
+"Cosine: "+vv(8)+$crlf_
+"Sine: "+vv(7)+$crlf_
+"Radians: "+vv(6)+$crlf_
+"Degrees: "+vv(5)
The final answer is expressed by the FPU within +/- 180 degree range.
By retaining sine and cosine data - the quadrant info is never lost, as happens with a tangent.
Also, tangent infinity problems are avoided.
Note that the FPU can produce the Sine and Cosine with a single instruction. FSINCOS
' 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)
' 360 degree sine cosine and arctangent
'----------------------------------------------
dim vv(8) as double
dim sMC as string = MC_Eval$ "
b8 #vv ' ' vv base address
dd 40 08 ' fld qword [eax+08] ' angle degrees in vv2
c7 00 NL180 ' mov dword [eax],180 ' temp store vv0 integer 180
da 30 ' fidiv dword [eax] ' angle in degrees
d9 eb ' fldpi ' pi
de c9 ' fmulp st(1) ' multiply by pi
d9 fb ' fsincos ' produce sine st(0) and cosine st(1)
dd 50 38 ' fst qword ptr [eax+56] ' sin in vv8
d9 c9 ' fxch st(1) ' swap to save cos
dd 50 30 ' fst qword ptr [eax+48] ' cos in vv7
d9 c9 ' fxch st(1) ' swap back again
d9 f3 ' fpatan ' arctangent pops 1 then result in st(0)
dd 50 28 ' fst qword ptr [eax+40] ' store arctan radians in vv6
d9 eb ' fldpi ' load pi
de f9 ' fdivp st(1) ' divide angle by pi
da 08 ' fimul dword ptr [eax] ' multiply by 180
dd 58 20 ' fstp qword ptr [eax+32] 'store angle degrees in vv5
c3 ' ret '
"
'-------------------------------'
vv(2)=300 ' test angle
MC_Exec sMC
msgbox 0, "Initial angle: "+vv(2)+$crlf_
+"Cosine: "+vv(8)+$crlf_
+"Sine: "+vv(7)+$crlf_
+"Radians: "+vv(6)+$crlf_
+"Degrees: "+vv(5)