flat assembler
Message board for the users of flat assembler.
Index
> Tutorials and Examples > Double-Precision to String & Friends. FPU-based conversi |
Author |
|
fasmnewbie 24 Sep 2014, 04:21
This is straight out from the oven
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.
Last edited by fasmnewbie on 24 Sep 2014, 14:29; edited 1 time in total |
|||||||||||
24 Sep 2014, 04:21 |
|
comrade 24 Sep 2014, 10:27
Good examples, good job! Thank you for sharing.
|
|||
24 Sep 2014, 10:27 |
|
fasmnewbie 24 Sep 2014, 14:42
comrade wrote: Good examples, good job! Thank you for sharing. any time. |
|||
24 Sep 2014, 14:42 |
|
fasmnewbie 24 Sep 2014, 15:01
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. |
|||
24 Sep 2014, 15:01 |
|
fasmnewbie 24 Sep 2014, 15:27
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: ST0: 3.1415926535897931 ST1: ... ST2: ... ST3: ... ST4: ... ST5: ... ST6: ... ST7: ... ST0: 0.0 ST1: 3.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? |
|||
24 Sep 2014, 15:27 |
|
fasmnewbie 24 Sep 2014, 15:53
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 |
|||
24 Sep 2014, 15:53 |
|
fasmnewbie 07 Oct 2014, 22:52
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---------- ST0: 6787.3426319999998668 ST1: 45.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
|
|||||||||||
07 Oct 2014, 22:52 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.