flat assembler
Message board for the users of flat assembler.

Index > Main > FPU help

Author
Thread Post new topic Reply to topic
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.

Thank you in advance.
Post 24 May 2007, 16:56
View user's profile Send private message Reply with quote
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)
Post 24 May 2007, 17:13
View user's profile Send private message Visit poster's website Reply with quote
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?
Post 25 May 2007, 06:19
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 25 May 2007, 11:10
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.
Post 25 May 2007, 11:10
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 25 May 2007, 11:29
thanks madmatt for the optimization

mattst88, shouldn't the operation be rather:
amplitude * sin (pi * ((audio_frequency / channel_phaze) * frequency))
or am I wrong?
Post 25 May 2007, 11:29
View user's profile Send private message Reply with quote
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.
Post 25 May 2007, 14:15
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.