Sine procedure in MASM

Discussion in 'Homework Help' started by agentofdarkness, Nov 21, 2007.

  1. agentofdarkness

    Thread Starter Active Member

    Oct 9, 2007
    42
    0
    For an assignment I have to write a procedure to calculate the sine of a number (located in EAX). I'm suppose to use a taylor expansion to approximate the sin(eax). I have most of the code working. If I have a number in EAX, I can calculate the 7th order Taylor expansion of the number. I have a macro called mTaylor_Order integer. The macro will calculate ST(0)^integer / integer!. The Taylor approximation works fine. However, a 7th order Taylor approximation of Sin x is only accurate between -pi and -pi. I have to be able to calculate the sine of any number. The way I tried to shift any number (I will call it Y) into a number between -pi and pi (call it X) is that I use the following equation:
    X = pi * (modulo(Y/pi)).

    The program does not work though. Any ideas on how I should shift Y so it will be a number between -pi and pi?



    Sine PROC

    fldpi
    fld Z
    fprem
    fldpi
    fmul
    fstp X
    finit
    fld X
    mTaylor_Order 1
    fld X
    mTaylor_Order 5
    fadd
    fld X
    mTaylor_Order 3
    fsub
    fld X
    mTaylor_Order 7
    fsub
    fstp X
    mov eax, X
    call WriteHex
    call Crlf
    ret
    Sine ENDP
     
  2. mogadeet

    Member

    May 1, 2007
    19
    0
    hey agent

    if i understand right (which may well fail to be the case) you're asking how, given an arbitrary real number y, you can produce a number x with abs(x)<pi that is congruent to y modulo 2*pi. i gather the language you're using lets you calculate the residue of a given number modulo a given modulus. if i'm right about both those points then you need something like this:

    def f(x):
    x = y modulo 2*pi
    if 0<=x<=pi: return x
    else: return 2*pi-x

    hope it helps

    peace
    stm
     
  3. agentofdarkness

    Thread Starter Active Member

    Oct 9, 2007
    42
    0
    How can I do modulo division in MASM using the FPU? There is an instruction FPREM but it does not work, keeps returning a value >1. I tried to write a modulo macro using the instruction FRNDINT but that can round up or down.
     
  4. mogadeet

    Member

    May 1, 2007
    19
    0
    how about something along these lines?:

    def f(x):
    if abs(x)<=pi: return x
    sgn = (-1,+1)[x>0]
    return f(x)-2*pi*sgn

    the problem is that i don't know assembly language. i'm willing to try to help you with this but it's very possible that my help isn't worth anything.
     
  5. agentofdarkness

    Thread Starter Active Member

    Oct 9, 2007
    42
    0
    Thanks for the help. I ended up writing my own Modulo code because I couldn't get MASM's modulo command to work for me. To write the Modulo code, you have to remember to set rounding to negative infinity so the FPU always rounds down. Then use FRNDINT. This will take a number and round it to the nearest integer. If you have the FPU rounding down, you will basically chop the remainder off. Store the original value, use FRNDINT. Then subtract the two and you have the remainder. Pretty simple but it took me quite a while to figure out. Hopefully that can help someone in the future.
     
Loading...