Trouble with Co-processor commands (I'd like to draw f(x)=x*sin(1/x) curve in graphics mode)

StackOverflow https://stackoverflow.com/questions/14430012

  •  16-01-2022
  •  | 
  •  

Question

I'm a beginner in programming Assembly. I would like to draw f(x)=x*sin(1/x) curve in graphics mode. I can draw f(x)=sin(x) curve but I'm unable to draw f(x)=x*sin(1/x). How could I do this?

Below you can see my working f(x)=sin(x) code. It's working and drawing the sine curve perfectly:

    org 100h                   
    mov al,13h
    int 10h

    xor bx,bx

    loopage:
    inc word [angle]

    fld dword [pie]
    fimul word [angle]
    fsin
    fimul word [xradius]
    fistp word [x]
    fimul word [yradius]
    fistp word [y]

    mov al,15

    mov dx,[x]
    add dx,ax
    mov cx,[y]
    add cx,bx

    ;xor ax,ax
    ;int 16h

    inc bl
    mov ah,0Ch
    mov al,bl
    int 10h

    cmp word [angle],360
    je endage
    jb loopage

    mov ax,0a000h
    mov es,ax
    xor ax,ax
    mov cx,32000
    rep stosw

    endage:
    xor ax,ax
    int 16h
    ret

    y dw 0
    x dw 0
    angle dw 0
    a dw -1
    pie dd 0.01756
    yradius dw 50
    one dw 1
    xradius dw 50
    float dd 0

I'd like to change this code to draw the f(x)=x*sin(1/x) curve.

I've tried several things to change following section in the code above:

    ...
    fld dword [pie]
    fimul word [angle]
    fsin
    fimul word [xradius]
    fistp word [x]
    fimul word [yradius]
    fistp word [y]
    ...

However without any results so far.

Could you help me please?

=========== EDITED: ====================

I tried out your code. Now my code looks like this:

    ...
    loopage:
    ;inc word [angle]
    ; FP0 <- 1
    fld1
    ; FP0 = FP0 / [pie] / [angle], is equal to FP0 / ([pie] * [angle])
    fidiv dword [pie]
    fidiv word [angle]
    fsin
    fimul dword [pie]
    fimul word [angle]
    fimul word [yradius]
    fistp word [y]

    mov al,15

    mov dx,[angle]
    add dx,ax
    mov cx,[y]
    add cx,bx
    ...

However this code draws only a single line instead of the expected x*sin(1/x) curve. Could you help me please to figure out what could be the problem?

Was it helpful?

Solution

You have an error in "working" code:

....
; FP0 <- [pie] ~ Pi/180
fld dword [pie]
; FP0 = FP0 * [angle]
fimul word [angle]
; FP0 = sin(FP0)
fsin
; FP0 = FP0 * [xradius]
fimul word [xradius]
; [x] <- FP0
fistp word [x]
; **ERROR: no FP0!**
fimul word [yradius]
fistp word [y]
....

Do not ask why it works ;) actually your [x] must be equal to [angle] in this code, and formula is [y] = ([pie] * [angle]) * sin(1/[pie]/[angle]), [x] = [angle]:

; FP0 <- 1
fld1
; FP0 = FP0 / [pie] / [angle], is equal to FP0 / ([pie] * [angle])
fidiv dword [pie]
fidiv word [angle]
fsin
fimul dword [pie]
fimul word [angle]
fimul word [yradius]
fistp word [y]

So remove [x] and [xradius] variables and change this line:

mov dx,[x]

to this one:

mov dx,[angle]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top