FP Mem80 to Ascii. 
Author Message
 FP Mem80 to Ascii.

Does someone have a Routine for translating a Memory FP80 Value
(Data) directely to Ascii? I mean without any use of the FPU.

Betov.



Tue, 28 Jun 2005 00:52:48 GMT  
 FP Mem80 to Ascii.

alt.lang.asm:

Quote:

> Does someone have a Routine for translating a Memory FP80 Value
> (Data) directely to Ascii? I mean without any use of the FPU.

> Betov.

Here's part of a program I wrote a long, long time ago.  "How long,
long ago?" you ask.  The last modification date on the file is March
22, 1986.  It was written in old MASM (2.something or 3.something) to
run on 8088 with an 8087.

You didn't specify which type of FP80 value, there are two.  This one
used the FP80 BCD format dumped to memory, starting at the fbstp
instruction, to convert the BCD to ASCII.

========
scale_up label dword
        dd      1000000.0

little label dword
        dd      5.0

outfloat proc far
        push    di
        push    si
        push    bp
        mov     bp, sp
        sub     sp, 14                  ;local tbyte pointer for dump

;bp - 14 = local storage
;bp - 12 = original status word
;bp - 10 = decimal dump
;bp +  0 = old bp
;bp +  2 = old si
;bp +  4 = old di
;bp +  6 = return offset
;bp +  8 = return segment
;bp + 10 = destination_pointer
;bp + 12 = float_pointer

        mov     bx, [bp + 12]           ;-> double
        fld     qword ptr [bx]          ;onto 8087 tos
        mov     di, [bp + 10]           ;-> destination string
        fmul    cs:[scale_up]           ;move decimal 6 left
        ftst                            ;test for zero
        cld                             ;direction = forward
        fstsw   [bp - 12]               ;store status word
        fwait                           ;be sure it's stored
        mov     al, [bp - 11]           ;get status word's hi byte
        test    al, 40h                 ;tos == 0 ?
        jz      not_zero                ;if not completely zero

;the number is completely zero, store a zero and get out...
;...but first have to rid 8087 tos of it

dump_zero:
        fcomp   cs:[little]             ;clear 8087's stack
        mov     al, '0'                 ;just print a zero
        stosb                           ;store it

clean_up:
        mov     byte ptr [di], 0        ;terminate the string
        mov     ax, di                  ;return -> '\0'
        mov     sp, bp                  ;discard locals
        pop     bp                      ;restore registers
        pop     si
        pop     di
        ret

not_zero:
        fabs                            ;make absolute value
        mov     [bp - 14], al           ;remember if negative
        fcom    cs:[little]             ;test for abs val < .000005
        fstsw   [bp - 12]               ;dump result of compare
        fwait                           ;make sure it's there
        test    byte ptr [bp - 11], 1   ;test for carry
        jnz     dump_zero               ;will round to zero
        fadd    cs:[little]             ;add 5 to round up
        frndint
        fbstp   tbyte ptr [bp - 10]     ;dump in bcd
        mov     al, '-'                 ;in case negative
        test    byte ptr [bp - 14], 1   ;nz if is negative
        jz      not_negative            ;if not, go
        stosb                           ;*di++ = '-'

not_negative:
        fwait
        mov     si, bp                  ;address buffer
        sub     si, 2                   ;skip sign flag
        mov     cx, 6                   ;# bytes with integer values
        mov     dx, '0'                 ;to make ascii
        mov     bx, 0f00fh              ;nibble test masks

no_int_loop:
        mov     ah, [si]                ;get next ms byte
        dec     si                      ;-> next
        test    ah, bh                  ;got a hi nibble ?
        jnz     do_int_hi               ;yes, go
        test    ah, bl                  ;got a lo nibble ?
        jnz     do_int_lo               ;yes, go
        loop    no_int_loop             ;until all 6
        jmp     short integer_done

int_loop:
        mov     ah, [si]                ;get next ms byte
        dec     si                      ;-> next

do_int_hi:
        mov     al, ah                  ;get both nibbles
        shr     al, 1                   ;shift hi to low
        shr     al, 1
        shr     al, 1
        shr     al, 1
        add     al, dl                  ;make it ascii
        stosb                           ;dump it

do_int_lo:
        mov     al, ah                  ;get both nibbles
        and     al, bl                  ;discard hi nibble
        or      al, dl                  ;make ascii
        stosb                           ;store it
        loop    int_loop

integer_done:
        mov     bx, [bp - 10]           ;get first 4 fractional digits
        mov     ah, [bp - 8]            ;and last 2
        mov     al, '.'                 ;output decimal point
        stosb                           ;first fractional pair
        call    unpack_two
        mov     ah, bh
        call    unpack_two
        mov     ah, bl
        call    unpack_two
        dec     di
        mov     [di], dh

un_zero_loop:
        dec     di
        mov     al, [di]
        cmp     al, dl
        jz      un_zero_loop
        cmp     al, '.'
        jz      zap_point
        inc     di

zap_point:
        jmp     clean_up

outfloat endp

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Tue, 28 Jun 2005 12:35:13 GMT  
 FP Mem80 to Ascii.
The UCR Standard Library v1.x provides a software-based conversion.
It also provides a basic software floating point package (which it uses to
do the conversion).
http://webster.cs.ucr.edu
Randy Hyde
Quote:

> Does someone have a Routine for translating a Memory FP80 Value
> (Data) directely to Ascii? I mean without any use of the FPU.

> Betov.



Tue, 28 Jun 2005 12:42:32 GMT  
 FP Mem80 to Ascii.


Thanks, Jack, but i have several of these Routines already and this
is not what i am searching for.

Quote:
> "You didn't specify which type of FP80 value"

The Real10 (or real80, if you like better). More details in my answer
to Randy.

Thanks, Betov.



Tue, 28 Jun 2005 17:33:18 GMT  
 FP Mem80 to Ascii.


Quote:
> The UCR Standard Library v1.x provides a software-based conversion.
> It also provides a basic software floating point package (which it uses
> to do the conversion).
> http://webster.cs.ucr.edu

Yes, Randy, i have read them. If you are thinking of "fpdigits.hla",
by the way, i am unsure about what it really outputs (does not seems
to me outputing a Real, neither in scientific nor in decimal notation,
but, i did not dig really much into the functionality details, as the
translation work, from HLA to Assembly (teasing you ;) would be bigger
for me than with other releases.

Anyway, this is not at all what i am searching for with this:

Let us say that we store the FPU Top into a Memory Real Ten Variable:

Quote:
>[MyFP10: R ?]

>fstp TMyFP10

Now, we read the real10 in Memory:

Quote:
> movzx ebx WMyFP10+8
> test ebx 08000 | jz L0>
>   ; Sign found:
>     mov Bedi '-' | inc edi
>     xor ebx 08000

> mov edx DMyFP10+4 | mov eax DMyFP10
> ; Now edx:eax hold the Mantissa // edx holds the exponent:
> ; ... and so on....

I stopped at the "and so on", thinking that someone else already
did it this way... Nobody???...

I need to do it all *without* using *at all* the FPU, because
1) it is to be inserted into an FPU expressions Pre-Parser that
already make havy use of FPU (and i want to enable "partial sub-
results" on the fly),
2) because i hope it could be a bit faster this way, and
3) because it would be good of having such a "hard coding" style
analyse for educational purpose.

Thanks, anyway, Betov.



Tue, 28 Jun 2005 17:34:40 GMT  
 FP Mem80 to Ascii.

Quote:

> I stopped at the "and so on", thinking that someone else already
> did it this way... Nobody???...

Hi Betov!

I think I recall seeing a "total bit-bashing" routine, but
unfortunately, I didn't save it (or can't find it if I did). I forget
who posted it - Terje, maybe? "My" routines use the FB* instructions
(similar to what Jack posted), and aren't what you're looking for. I
think I've got another example using FPU instructions to isolate
exponent and mantissa - still no use to you. I'll keep you in mind if I
come across that other method - I think you're right that it would be
faster...

Best,
Frank



Tue, 28 Jun 2005 18:55:25 GMT  
 FP Mem80 to Ascii.

"Betov" answered "Randall Hyde" :
|
| > The UCR Standard Library v1.x provides a software-based conversion.
| > It also provides a basic software floating point package (which it uses
| > to do the conversion).
| > http://webster.cs.ucr.edu
|
| Yes, Randy, i have read them. If you are thinking of "fpdigits.hla",
| by the way, i am unsure about what it really outputs (does not seems
| to me outputing a Real, neither in scientific nor in decimal notation,
| but, i did not dig really much into the functionality details, as the
| translation work, from HLA to Assembly (teasing you ;) would be bigger
| for me than with other releases.
|
|
| Anyway, this is not at all what i am searching for with this:
|
| Let us say that we store the FPU Top into a Memory Real Ten Variable:
|
| >[MyFP10: R ?]
| >
| >fstp TMyFP10
|
|
| Now, we read the real10 in Memory:
|
| > movzx ebx WMyFP10+8
| > test ebx 08000 | jz L0>
| >   ; Sign found:
| >     mov Bedi '-' | inc edi
| >     xor ebx 08000
| >
| > mov edx DMyFP10+4 | mov eax DMyFP10
| > ; Now edx:eax hold the Mantissa // edx holds the exponent:
| > ; ... and so on....
|
| I stopped at the "and so on", thinking that someone else already
| did it this way... Nobody???...
|
| I need to do it all *without* using *at all* the FPU, because
| 1) it is to be inserted into an FPU expressions Pre-Parser that
| already make havy use of FPU (and i want to enable "partial sub-
| results" on the fly),
| 2) because i hope it could be a bit faster this way, and
| 3) because it would be good of having such a "hard coding" style
| analyse for educational purpose.
|
|
| Thanks, anyway, Betov.

Yes I did, the standard FPU-formats weren't supported anymore by my OS.
Just for data-import-reasons I kept the conversion routines.

All you need to do is:
the most significant bit is always the mantissa sign, store it away.
mask out the exponent-bits [b14..b0 of the top word, for extended precision]
adjust the exponent with the bias

 and exp(word),7fff
 sub exp,3fff         ;bias for 80-bit format

now you already got a signed exponent but it's still a power of 2 value.
if the result is zero then the mantissa is invalid [zero or tiny]
if it is "FFC0" then it's a "NaN" (not a number or infinitive).

the remaining 8 bytes are the unsigned mantissa in the form "1.xxxxxxxx"
So the mantissa-value is always between 1<=2 (except if tiny format)
and the MSbit must be set to indicate a "normal"

Note: the "1" is not implicit in 80-bit, as it is in 64 and 32 bit formats.
So a 64-bit unsigned integer cannot be represented with this format.

The designers had chosen this format for fast and easy comparison,
but they didn't think of easy calculation at all !  :)

For more details look at Intel's library.
I got these books years ago....

--
KESYS development
wolfgang



Wed, 29 Jun 2005 11:30:20 GMT  
 FP Mem80 to Ascii.

Quote:

> Does someone have a Routine for translating a Memory FP80 Value
> (Data) directely to Ascii? I mean without any use of the FPU.

Yes, Randy, i have read them. If you are thinking of "fpdigits.hla",
by the way, i am unsure about what it really outputs (does not seems
to me outputing a Real, neither in scientific nor in decimal notation,
but, i did not dig really much into the functionality details, as the
translation work, from HLA to Assembly (teasing you ;) would be bigger
for me than with other releases.
<<<<<

You're looking at the wrong routines if you're reading HLA code.
The UCR Standard Library v1.x (and 2.x for that matter) was written
with MASM in 16-bit mode for DOS.  It, too, has an fpdigits function
(which converts the FP number to the string of digits without regard to
placement of the decimal point or the exponent, that work is done later),
but it uses a software division by ten to achieve it's results.  The HLA
Standard Library version uses the FPU BCD load and store instructions
to do the conversion;  I had assumed, based on your post, that you did not
want to do this.

As for a "bit twiddling" way to do the conversion, try doing that for
binary integers and then apply the same scheme to FP (i.e., if you manage
to make it work, it's not going to buy you much).  BTW, the algorithms
in the UCR Stdlib v1.x came out of a book written by Neil Graham called
"Microprocessor Programming for Computer Hobbyists" which was written
back in the days of the 8080 microprocessor (most of the coding examples
were given in PL/M-80).  Because of the cost of software floating point
arithmetic, the conversion is far from efficient.  Then again, it's going to be
pretty hard to beat FBLD and FBSTP, despite the fact that they are among
the slowest executing instructions on the CPU.

If you're using the FPU for floating point arithmetic and you simply want
to prevent going crazy with the stack, you *can* simply do an FBSTP
instruction and then manipulate the result in memory from that point.
No need to use the FPU any further (this is the way the UCR Stdlib v2.x
and the HLA Standard Libraries both work).  Much, much faster,
and it doesn't involve the use of the FPU once you store the data into memory.
Of course, if the number you want to print is not in the FPU in the first place,
then you can't use this without using one stack location in the FPU.
Randy Hyde



Thu, 30 Jun 2005 15:57:49 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. FP to ASCII

2. More than two FP-TB-3 with FP-TC-120

3. OO and FP (was subtyping in FP)

4. FP Component Libraries? (FP vs OOP)

5. FP to FP Binary/Hex

6. Ascii to Binary to Ascii

7. Ascii to Hex, Hex to Ascii convertion

8. ASCII to numbers and numbers to ASCII

9. Need converter program to translate ascii to ebcdic in mixes ascii/binary file

10. Ascii-records (with some comp fields) to plain Ascii-records

11. OO and FP [Fwd: comparison OOP and FP (and how to put them together) was: Re: need help with haskell]

12. Using FP registers as additional GP registers

 

 
Powered by phpBB® Forum Software