flat assembler
Message board for the users of flat assembler.

Index > Main > Floating Point examples

Author
Thread Post new topic Reply to topic
me239



Joined: 06 Jan 2011
Posts: 200
me239 13 Oct 2011, 22:35
Hey guys, I've been looking all over for some SIMPLE examples of floating point math done in assembly and printing it. Preferably 16 bit, thanks!
Post 13 Oct 2011, 22:35
View user's profile Send private message Reply with quote
Fred



Joined: 22 Oct 2010
Posts: 39
Fred 14 Oct 2011, 06:25
Floating point math in SSE is very easy to do.

Code:
movd  xmm0,[esp+4] ;move some float from the stack to xmm0[0-31]
movd  xmm1,[esp+8] ;^ but to xmm1[0-31]
addss xmm0,xmm1    ;add scalar single (xmm[0-31] + xmm[0-31])    


That's about as simple of an example you'll get Very Happy

I don't think there's any instructions for 16 bit floats... yet. I'm pretty sure some new processor was going to support 32 <-> 16 bit float conversion though.

Printing floats would require some windows calls(if you're on windows anyway) and that's unfortunately not my area of expertise. Razz
Post 14 Oct 2011, 06:25
View user's profile Send private message Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 350
Location: Poland
macgub 18 Oct 2011, 10:05
The same code done on FPU:
Code:
fninit
fld     dword[esp+4]   ; single precision, 32 bits.
fadd  dword[esp+8]   ; st0 - result
    
Post 18 Oct 2011, 10:05
View user's profile Send private message Visit poster's website Reply with quote
Akujin



Joined: 11 Oct 2011
Posts: 26
Location: Exile
Akujin 18 Oct 2011, 19:25
Internals of FPU and MA$M examples.
http://www.ray.masmcode.com/downloads/SimplyFPU1-6.zip

_________________
CLI
HLT
Post 18 Oct 2011, 19:25
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 18 Oct 2011, 21:08
simple example:

compute ebx=eax*1.32456
Code:
example1:
push dword 1.32456 eax ;preload in stack
finit     ;init the fpu
fld [ss:esp+4] ;load float 1.32456
fimul [ss:esp] ;mul by eax value
fistp [ss:esp+4] ;store in second pop
pop eax eax ;pop two times, free the stack
ret ;quit
    


compute eax=eax*sin(eax*pi)
Code:
example2:
push eax
finit
fldpi ;load pi value
fimul [ss:esp] ;mul by eax
fsin ;sinus of st0 in st0
fimul [ss:esp]
fistp [ss:esp]
pop eax
ret
    


the fpu is simple in fact.
Post 18 Oct 2011, 21:08
View user's profile Send private message Visit poster's website Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 19 Oct 2011, 03:51
edfed wrote:
simple example:

compute ebx=eax*1.32456
Code:
example1:
push dword 1.32456 eax ;preload in stack
finit     ;init the fpu
fld [ss:esp+4] ;load float 1.32456
fimul [ss:esp] ;mul by eax value
fistp [ss:esp+4] ;store in second pop
pop eax eax ;pop two times, free the stack
ret ;quit
    


compute eax=eax*sin(eax*pi)
Code:
example2:
push eax
finit
fldpi ;load pi value
fimul [ss:esp] ;mul by eax
fsin ;sinus of st0 in st0
fimul [ss:esp]
fistp [ss:esp]
pop eax
ret
    


the fpu is simple in fact.

now how would I print one of those values to the prompt?
Post 19 Oct 2011, 03:51
View user's profile Send private message Reply with quote
emc



Joined: 20 Aug 2011
Posts: 90
Location: France
emc 22 Oct 2011, 06:49
You can use printf with a formatted string (with %f) to print floating values.
Post 22 Oct 2011, 06:49
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 22 Oct 2011, 23:16
emc wrote:
You can use printf with a formatted string (with %f) to print floating values.
I'm using DOS
Post 22 Oct 2011, 23:16
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.