|
help with FPU in ASM |
| category: code |
|
|
Hi people, I'm trying to learn how to use the FPU but stuff isn't working out here.
This is my code, it's supposed to draw a circumference on the screen:
Code: ; Initialize video mode mov al, 13h int 10h push word 0xA000 pop es
; Initialize FPU finit
draw: ;;;;;;;;;CIRCLE ;;;;;;;;; ; x = 160 + R*cos(t) y = 100 + R*sin(t) ; di = 160 + R*cos(t) + 320 * (100 + R*sin(t)) ;;;;;;;;;;;;;;;;;;;;;;;;; ;LOAD T TO FPU fld dword [_t] ;ST(0) = t
;ADD TINC TO T AND SAVE IT FOR THE NEXT POINT fld st0 ;ST(0) = t, ST(1) = t fadd dword [_tinc] ;ST(0) = t+tinc , ST(1) = t fstp dword [_t] ;ST(0) = t
;GET SIN AND COS OF T fsincos ;ST(0) = cos(t), ST(1) = sin(t) ;MULTIPLY COS BY R fmul dword [_R] ;ST(0) = R*cos(t), ST(1) = sin(t)
;GET COS INTO DI fistp dword [_pos] ;ST(0) = sin(t) mov di, [_pos] ;di = R*cos(t)
;ADD 160 TO DI add di,160 ;di = 160 + R*cos(t)
;MULTIPLY SIN BY R fmul dword [_R] ;ST(0) = R*sin(t)
;GET SIN INTO BX fistp dword [_pos] ;FPU Stack is empty mov bx,[_pos] ;BX = R*sin(t)
;ADD 100 TO BX add bx,100 ;BX = 100 + R*sin(t)
;MULTIPLY BX BY 320 imul bx,320 ;BX = 320 * (100 + R*sin(t))
;ADD BX TO DI add di,bx ;DI = 160 + R*cos(t) + 320*(100 + R*sin(t)) ;Draw the pixel mov al,20h ;color stosb ;Draw the pixel
; Check for ESC keypress and exit in al, 60h dec al jnz draw
mov al, 03h ;Select text mode and int 10h ;do the interruption ret
; Values _t: dd 0.0 _tinc: dd 0.01 _R: dd 50.0 _pos dd 0.0
Everytime I get a value from the FPU with fstp or fistp I get a 0 (zero), so the program only draws a pixel in the center of the screen.
Any help is appreciated |
|
|
Instead of making us read all that code, maybe you can narrow down the problem to the smallest portion that doesn't behave as you expect. (In the process, maybe you'll see the solution yourself!)
|
|
|
|
Are you sure everything is treated as the proper data types that they are supposed to? I.e, you are not mixing up floats and doubles I hope? |
|
|
The code itself is correct.
Your forgot only this:
org 100h
Without it fpu load/save addresses are wrong. |
|
|
what assembler do you use?
Code: . . . fistp dword [_pos] ;ST(0) = sin(t) mov di, [_pos] ;di = R*cos(t) . . . fistp dword [_pos] ;FPU Stack is empty mov bx,[_pos] ;BX = R*sin(t) . . .
as you see you try to drop dwords into words. that won't bring the correct result.
Code: . . . fistp word [tester] ;ST(0) = sin(t) mov di, [tester] ;di = R*cos(t) . . . fistp word [tester] ;FPU Stack is empty mov bx,[tester] ;BX = R*sin(t) . . . ; Values
tester dw ?
here you have your blue circle (=
@frag: really? i did not get it running under fasm. sizes won't match on the parts above in the original code. hm. |
|
|
@sensenstahl really.
>as you see you try to drop dwords into words. that won't bring the correct result.
It's only address, dosent't matter here, if you load store the same 4-byte value it should work. |
|
|
|
fix: ... the same 2-byte value... |
|
|
|
indeed (= |
|
|
frag, for god sake, if you have an ability to speak asm. Go work for Kaspersky. They need dedicated coder analysts like you are.
And frag, most advanced virus ever made, technically pointi of view, is 1990 made WHALE virus. Even novadays, how it works, what it does, is unknown.
I'll give you 200 euros via paypal, if you can disasseble it and make a paper what it does. This virus was like a stuxnet or flame. Far ahead of its time. It was piece of an art.
|
|
|
oh wow, it was the org 100h
I thought that was done automatically when assembling with nasm and it never gave me a problem with other stuff
thanks a lot frag!
|
|
|
You made a circle!
Anyway, just wanted to say the FISTP instruction should have been censored... it's so dirty...
|
|
|
|
as kb_ once put it eloquently "fist dword ptr [me]". :) |
|
|
|
oops.. sorry kb_ .. it was gaffer.. |
|
|
|
|