Monday, May 4, 2009
Using the FSIN instruction to compute SINE of an angle
Use the fsin instruction to compute the sine of an angle.
The fsin instruction has an implied operand (top of the floating point stack).
The top of the floating point stack is denoted by ST0 register.
The fld instruction loads the floating point value from the location 'theta' into ST0:
fld dword [theta]
Now the operand is in st0. The fsin instruction now computes the sine of the angle in st0 and overwrites st0 with the result. Note: The operand to fsin is in radians(not degrees). In the example below thetha contains 1.57 ( ~PI/2 radians). So at the end of the program we expect a value of Sin(PI/2) in ST0.
-----------------------------------------------------
section .data
theta dd 1.57
section .text
global _start
_start:
nop
finit
fld dword [theta]
fsin
mov eax,1
mov ebx, 0
int 0x80
-------------------------------------------------
It is easy to see the program-flow with gdb:
9 finit
1: $st0 = 0 -> ST0 = 0
10 fld dword [theta] <--- next instruction to be executed.
1: $st0 = 0
11 fsin <--- next instruction to be executed
1: $st0 = 1.57000005245208740234375 (Result of FLD is in ST0)
13 mov eax,1
1: $st0 = 0.99999968297360224280629845128309796 <--- (Result of FSIN is in ST0)
--------------------------------------------------------------
The value in ST0 is 0.9999 which is close to the expected value of 1. Note that the inaccuracy comes from the fact that our input is 1.57 which is only an approximation of PI/2.
The fsin instruction has an implied operand (top of the floating point stack).
The top of the floating point stack is denoted by ST0 register.
The fld instruction loads the floating point value from the location 'theta' into ST0:
fld dword [theta]
Now the operand is in st0. The fsin instruction now computes the sine of the angle in st0 and overwrites st0 with the result. Note: The operand to fsin is in radians(not degrees). In the example below thetha contains 1.57 ( ~PI/2 radians). So at the end of the program we expect a value of Sin(PI/2) in ST0.
-----------------------------------------------------
section .data
theta dd 1.57
section .text
global _start
_start:
nop
finit
fld dword [theta]
fsin
mov eax,1
mov ebx, 0
int 0x80
-------------------------------------------------
It is easy to see the program-flow with gdb:
9 finit
1: $st0 = 0 -> ST0 = 0
10 fld dword [theta] <--- next instruction to be executed.
1: $st0 = 0
11 fsin <--- next instruction to be executed
1: $st0 = 1.57000005245208740234375 (Result of FLD is in ST0)
13 mov eax,1
1: $st0 = 0.99999968297360224280629845128309796 <--- (Result of FSIN is in ST0)
--------------------------------------------------------------
The value in ST0 is 0.9999 which is close to the expected value of 1. Note that the inaccuracy comes from the fact that our input is 1.57 which is only an approximation of PI/2.
Subscribe to Posts [Atom]
Post a Comment