flat assembler
Message board for the users of flat assembler.

 Index > Main > Floating Point examples
Author
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!
13 Oct 2011, 22:35
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]

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

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.
14 Oct 2011, 06:25
macgub

Joined: 11 Jan 2006
Posts: 345
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
```
18 Oct 2011, 10:05
Akujin

Joined: 11 Oct 2011
Posts: 26
Location: Exile
Akujin 18 Oct 2011, 19:25
Internals of FPU and MA\$M examples.

_________________
CLI
HLT
18 Oct 2011, 19:25
edfed

Joined: 20 Feb 2006
Posts: 4325
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
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
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.
18 Oct 2011, 21:08
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
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
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?
19 Oct 2011, 03:51
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.
22 Oct 2011, 06:49
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
22 Oct 2011, 23:16
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum