flat assembler
Message board for the users of flat assembler.

 Index > Main > FPU help
Author
ManOfSteel

Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 24 May 2007, 16:56
Hello,

I have some trouble understanding exactly how the FPU works.

I have the following code and what would help me is to have it explained:
Code:
```fldpi
fild  dword [audio_frequency]
fidiv word [channel_phaze]
fimul word [frequency]
fmul  st0,st1
fsin
fimul word [amplitude]
fistp word [di]
```

Could you tell me:
1. what are the mathematical operations that are being performed, i.e. on the instruction "n", the value "x" is multiplied by the the value "y", and that sort of things.
2. what FPU registers are being used on each line since none is explicitly used other than on the fifth line.

24 May 2007, 16:56
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88 24 May 2007, 17:13
fildpi ; loads Pi onto the top of the stack (st0)
fild dword [audio_frequency] ; loads the audio frequency onto the top of the stack (st0), Pi is moved down to st1
fidiv word [channel_phaze] ; converts channel_phaze to a double precision FP value; divide st0 (audio_frequency) by channel_phaze and store results on top of stack (st0)
fimul word [frequency] ; converts frequency to a double precision FP value and multiplies st0 by freqency and stores the result in st0
fmul st0,st1 ; multiplies st0 by st1 (pi) and stores the result in st0
fsin ; calculates the sine of st0 in radians and stores the result in st0
fimul word [amplitude] ; converts to double precision and multiplies by st0 storing the result in st0
fistp word [di] ; store the contents of st0 in the memory location [di]; remove st0 from the stack

mathematically this performs the following operation

di = Amplitude * sin((π * frequency)/channel_phaze)
24 May 2007, 17:13
ManOfSteel

Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 25 May 2007, 06:19
It's exactly what I wanted. Thanks very much. Just one more thing, why has pi been moved down to st1? Is it because the "fild" instruction decrements the TOP register pointer?
25 May 2007, 06:19

Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
Instead of "fmul st0, st1" you could use the fmulp (with no arguments) this muliplies st0 by st1 and pops the stack then stores the result in st0. Doing this would leave your FPU stack clean for the next operation.
25 May 2007, 11:10
ManOfSteel

Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 25 May 2007, 11:29

mattst88, shouldn't the operation be rather:
amplitude * sin (pi * ((audio_frequency / channel_phaze) * frequency))
or am I wrong?
25 May 2007, 11:29
mattst88

Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88 25 May 2007, 14:15
Yes, it should. My mistake.

Quote:
It's exactly what I wanted. Thanks very much. Just one more thing, why has pi been moved down to st1? Is it because the "fild" instruction decrements the TOP register pointer?

Yes, this is exactly what is happening. Since Pi is stored in st0 (the top of the stack) and something else is being pushed onto the stack, Pi is no longer the top. It is now referenced as st1 and the new item is now the top, st0.
25 May 2007, 14:15
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum