flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Examples and Tutorials > Double-Precision to String & Friends. FPU-based conversi

Author
Thread Post new topic Reply to topic
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
Double-Precision to String & Friends. FPU-based conversi
This is straight out from the oven Very Happy

These routines deal with FPU-based conversions for double and single precision: Float/Double to String (prtflt,prtdbl) and String to Float/Double (getflt,getdbl). Plus other useful routines for newbies dealing with precision data.

The routines objectives:
1. To open up new opportunities for newbies and learners to deal with precision data while learning assembly. No more integer-only assembly programming. Now you can see what's in the FPU stack as you program them.
2. To demonstrate the basic steps in FPU conversion both ways by using only FPU instructions and two basic C/OS library - putchar and getchar.

Main routines:
1. prtdbl - display REAL8 precision data (dq). Send the address
2. prtflt - display REAL4 precision data (dd). Send the value
3. getflt - get a float data from keyboard
4. getdbl - get a double-precision data from keyboard
5. fpbin - Display binary format of a real4 data
6. fpbinq - Display binary format of a real8 data. Send the address
7. fpu_stack - Display FPU stack
8. fpu_sflag - Display FPU status flag
9. fpu_cflag - Display FPU control flag

Support routines:
10. prthex
11. prtstr
12. prtchr
13. line

Notes:
- The file is ready to compile and run.
- These routines are far from perfect. I coded them as it goes, as a hobby, based on my limited knowledge on ASM
- Routines are slow. I put no emphasis on optimization or efficiency, plus, there could be some hidden bugs somehere. You are welcome to improve it and post it back here.
- The import table at the end of code must be left alone.
- Excuse the lack of comments. I don't intent to put comments until all bugs are figured out. Deleting side-comments for 1000-line codes is simply a PITA!
- Macro version is available. But I don't think you are interested.


Description: FPU-based conversion routines plus other useful routines for newbies
Download
Filename: test5.asm
Filesize: 18.5 KB
Downloaded: 72 Time(s)



Last edited by fasmnewbie on 24 Sep 2014, 14:29; edited 1 time in total
Post 24 Sep 2014, 04:21
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
Test program and output (it's included in the attachment).

Code:
format pe console      ;Flat PE
include 'win32axp.inc'

stdcall fpbinq,z,1     ;Display normalized binary (double)
stdcall fpbin,[x],1    ;Display normalized binary (single)
call line
fst [y]                ;Attempt to store from empty ST0. Check flags
call fpu_sflag         ;Display FPU status flag
fld [x]                ;Load a single precision into ST0
call fpu_stack         ;View FPU stack
call line
stdcall prtdbl,z,5,1   ;pass-by-reference. Print with 5 decimal places
stdcall prtflt,[x],0,1 ;pass-by-value. Display in full. New line
fld [z]                ;Load another variable to ST0
fldpi                  ;Load PI
fld [n]                ;load a NAN value
call line
call fpu_stack         ;See the stack
call fpu_cflag         ;Display FPU control flag

invoke system,"pause>0"
invoke exit,0
x dd -0.000000000000000000045332
y dq 34.06565
z dq -67.004355
n dq 9221120237041090561 ;a NAN value



Output:


Code:
1.10000000101.00001100000001000111-01011010001100011010010010111110
1.00111110.10101100001001100000110

F  E  D  C  B  A  9  8  7  6  5  4  3  2  1  0
----------------------------------------------
B C3 TP TP TP C2 C1 C0 IR SF  P  U  O  Z  D  I  ;FPU status flag
----------------------------------------------
0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  1 41

ST0: -4.5331998496789180e-20  ;FPU stack content
ST1...
ST2...
ST3...
ST4...
ST5...
ST6...
ST7...

-67.00436
-4.5331993e-20

ST0: <NaN>
ST13.1415926535897931
ST2: -67.0043550000000038
ST3: -4.5331998496789180e-20
ST4...
ST5...
ST6...
ST7...
F  E  D  C  B  A  9  8  7  6  5  4  3  2  1  0   ;FPU control flags
----------------------------------------------
        IC RC RC PC PC IEM   PM UM OM ZM DM IM
----------------------------------------------
0  0  0  0  0  0  1  0  0  1  1  1  1  1  1  1 27F



Last edited by fasmnewbie on 24 Sep 2014, 14:08; edited 3 times in total
Post 24 Sep 2014, 04:26
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1104
Location: Russian Federation
Good examples, good job! Thank you for sharing.
Post 24 Sep 2014, 10:27
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399

comrade wrote:
Good examples, good job! Thank you for sharing.



any time.
Post 24 Sep 2014, 14:42
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
Notes to newbies on usage:

prtdbl, fpbinq deal with quadword (dq,64-bit,8bytes) data. So to pass such data to the routines, you need to pass the address instead.

Example;

Code:
stdcall fpbinq,mydouble,1    ;pass the address of mydouble. See its binary format
stdcall prtdbl,mydouble,0,1  ;pass the address of mydouble
mov eax,mydouble   ;another way of passing the address
stdcall prtdbl,eax,0,1  ;pass the address of mydouble in EAX
...
mydouble dq 43.34



The same goes to getflt and getdbl.

For double word (float, 32-bit) prtflt and fpbin, you can pass the value via variables or using constants directly.

Code:
stdcall fpbin,34.42,1  ;send constant. Print 1 newline
stdcall prtflt,[myFloat],0,1  ;send the value of a [dd] data. No decimal places. display newline
stdcall prtflt,34.42,0,1  ;or use constant. No decimal places. Display newline
...
myFloat dd 34.42



For other routines, I think its quite straightforward.
Post 24 Sep 2014, 15:01
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
For routine fpu_stack, it shows the current content of the FPU registers. If you wished to preserve your data, make sure you use instruction FINIT carefully because FINIT will certainly erase all the current content of the stack.


Code:
fldpi           ;load a PI from FPU
call fpu_stack  ;Display stack
call line
fldz            ;load a 0.0 from FPU
call fpu_stack  ;Show persistency
call line
finit           ;This one CLEARS the stack
call fpu_stack  ;Nothing in the stack



Output:

Code:
ST03.1415926535897931
ST1...
ST2...
ST3...
ST4...
ST5...
ST6...
ST7...

ST00.0
ST13.1415926535897931
ST2...
ST3...
ST4...
ST5...
ST6...
ST7...

ST0...
ST1...
ST2...
ST3...
ST4...
ST5...
ST6...
ST7...



Maybe we need another routines to save and restore the FPU?
Post 24 Sep 2014, 15:27
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
ok, for the moment, you can test this 2 new procedures for saving and restoring the FPU. But I'd like to remind you that FSAVE instruction will also reset the FPU because it eventually calls for another FINIT. So you decide when you should use these two routines fpu_save and fpu_restore


Code:
format pe console
include 'win32axp.inc'

fldpi                     ;load a PI from FPU
call fpu_stack            ;Display stack
call line
fldz                      ;load a 0.0 from FPU
call fpu_stack            ;Show persistency
stdcall fpu_save,fpu      ;Save fpu state
call line
finit                     ;This one CLEARS the stack
call fpu_stack            ;Nothing in the stack
stdcall fpu_restore,fpu   ;restore fpu state
call line
call fpu_stack  ;FPU stack restored

invoke system,'pause>0'
invoke exit,0
fpu rb 108   ;use this data to save FPU state

;Save this 2 routines in the file you downloaded and use the test program above.
proc fpu_save,info
     mov eax,[info]
     fsave [eax]
     fwait
     ret
endp
proc fpu_restore,info
     mov eax,[info]
     fwait
     frstor [eax]
     ret
endp
;The rest of the attached routines should follow


Post 24 Sep 2014, 15:53
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 399
Convert float and double to string using FPU
A major overhaul to prtflt. A few visual improvements on output. Plus a few additional features;

1. fpu_roundto -rounding
2. fpu_precision - set precision mode
3. fpu_env - display stack,control flag and status flag
4. fpu_scopy - copy entire stack to an array of dq
5. fpu_copy - copy a single FPU register
6. fpu_reg -view a single FPU register

Test code:


Code:
format pe console
include 'win32axp.inc'

;stdcall getflt,myfloat
;stdcall getdbl,mydouble
fld [myfloat]
fld [mydouble]
call fpu_env
stdcall prtflt,[myfloat],0,1
stdcall prtflt,[myfloat],3,1
stdcall prtdbl,mydouble,0,1
stdcall prtdbl,mydouble,3,1

invoke system,"pause>0"
invoke exit,0
myfloat dd 45.666768
mydouble dq 6787.342632



Test run output:

Code:
-----------STACK----------
ST06787.3426319999998668
ST145.6667671203613281
ST2...
ST3...
ST4...
ST5...
ST6...
ST7...
--------------------------

----------------------------------------STATUS
B C3 TP TP TP C2 C1 C0 IR SF  P  U  O  Z  D  I
----------------------------------------------
0  1  1  1  0  0  0  1  0  0  0  0  0  0  0  0=7100

---------------------------------------CONTROL
        IC RC RC PC PC IEM   PM UM OM ZM DM IM
----------------------------------------------
0  0  0  0  0  0  1  0  0  1  1  1  1  1  1  1=27F
45.6667671
45.667
6787.3426319999998668
6787.343



Description: Almost everything u need for FPU-based coding and learning is here. Ready to compile and run.
Download
Filename: fpu.asm
Filesize: 22.12 KB
Downloaded: 54 Time(s)

Post 07 Oct 2014, 22:52
View user's profile Send private message Visit poster's website 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.