# Re: Help producing sine wave

From: Ed Beroset (beroset_at_mindspring.com)
Date: 11/06/03

```Date: Thu, 06 Nov 2003 05:03:02 GMT

```

Tony Jester wrote:
> "sinewave" <none@nowhere.com> wrote in message
> news:oprx6s0gi61pb73s@10.0.0.1...
>
>>hard-code it Tony. write a 3-line HLL program to get the integers and
>
> store
>
>>them inside your program in an array... the fastest calculation is one you
>>don't have to make.
>
>
> After two days of wasted time trying, I'm giving up for now and will do just
> what you suggest. I'm really interested in the FPU, however. I've always
> known it was there, but I've never done anything with it.

If your sine wave is 512 points per cycle and has a range of 0 to 255,
then mathematically your function would be

y = 255/2 * ( 1 + sin(x * pi/256) )

One way to do that calculation would be to do things in this order. If
you think about the order in which a human might do these calculations,
one might evaluate the expression inside the parentheses first, then
calculate the sin, then add 1 and then multiply by 255 and finally
divide by two. In code, that might look like this:

; simple demo code to show how to use FPU
; written by Ed Beroset on 5 November 2003
; donated to the public domain by the author
;
; assembled using TASM 4.0
; might work with MASM 6.11
.model small
.586
.stack 200h
.data
halfcycle dw 256
amplitude dd 127.5
value dw ?
answer dw ?
.code
start proc
.startup
mov cx,512 ; one full cycle
top:
mov [value],cx
call sine
loop top
.exit 0
start endp

sine proc
fldpi ; st0 = pi
fild [halfcycle] ; st1 = pi, st0 = halfcycle
fdivp ; st0 = pi/halfcycle
fild [value] ; st0 = x, st1 = pi/halfcycle
fmulp ; st0 = x * pi/halfcycle
fsin ; st0 = sin (x * pi/halfcycle)
fld1 ; st0 = 1, st1 = sin(...)
faddp ; st0 = 1 + sin(...)
fld [amplitude] ; st0 = amplitude, st1 = 1+sin(...)
fmulp ; st0 = amp * (1+sin(...))
fistp [answer] ; save the answer and empty FPU
ret
sine endp
END

Ed