Here is a FTOA routine -- converting a Floating-Point number to ASCII.

It is called like this:

            ; number on FPU stack

            push segment buffer
            push offset buffer
            push decimal_places
            call ftoa

            ; FPU stack has been popped

For example, the following code places the string '3.141592653589793'
in the buffer at ES:DI:

            fldpi
            push es
            push di
            push 15
            call ftoa

Notes:

    The string that this function outputs is null-terminated.

    Up to 3 values are pushed onto the FPU stack during the
    calculations; make sure that there are no more than 5
    values on the stack on entry to this function.

    It requires at least an 80186 processor and 8087 coprocessor.






;****************** ftoa() -- Convert floating point number to ASCII
;void ftoa(real x, char *strp, int prec);

strp        equ bp+6
prec        equ bp+4

ten         equ bp-2
exponent    equ bp-4
buffer      equ bp-6
save_buf    equ bp-20

ftoa        proc

            enter 20,0              ;Set up stack frame
            pusha                   ;Save all registers
            push es
            fstenv [save_buf]       ;Save FPU state
            mov word ptr [ten],10   ;Store the value 10 in memory

            fstcw [buffer]          ;Set rounding to round-down
            or byte ptr [buffer+1],004h
            and byte ptr [buffer+1],0F7h
            fldcw [buffer]

            les di,[strp]           ;ES:DI = string

            ftst                    ;Check the number against 0
            fstsw [buffer]
            mov ax,[buffer]
            sahf
            jae p1_negok            ;If it is negative, then

            mov al,'-'              ;Store a '-' sign
            stosb
            fchs                    ;Make it positive

p1_negok:   fld1                    ;Load Log10(2)
            fldl2t
            fdiv

            fld st(1)               ; st(0) = x
                                    ; st(1) = Log10(2)
                                    ; st(2) = x

            fyl2x                   ; st(0) = Log10(x)
                                    ; st(1) = x

            fistp word ptr [exponent]

            fxtract                 ; st(0) = Log2(x)
            fxch                    ; st(2) = Mant2(x)

            fldl2t                  ; st(0) = Log2(x)
            fxch                    ; st(1) = Log2(10)
                                    ; st(2) = Mant2(x)

            fprem                   ; st(0) = Log2(x) mod Log2(10)
            fstp st(1)              ;       = Scale
                                    ; st(1) = Mant2(x)

            fld1                    ; st(0) = Scale
            fld st(1)               ; st(1) = 1
                                    ; st(2) = Scale
                                    ; st(3) = Mant2(x)

            fprem                   ; st(0) = Frac(Scale)
            fstp st(1)              ; st(1) = Scale
                                    ; st(2) = Mant2(x)

            f2xm1                   ; st(0) = 2^Frac(Scale)-1
                                    ; st(1) = Scale
                                    ; st(2) = Mant2(x)

            fld1                    ; st(0) = 2^Frac(Scale)
            fadd                    ; st(1) = Scale
                                    ; st(2) = Mant2(x)

            fscale                  ; st(0) = 2^Scale
            fstp st(1)              ; st(1) = Mant2(x)

            fmul                    ; st(0) = Mant10(x)

            fist word ptr [buffer]  ;Store as integer
            fisub word ptr [buffer] ;Reduce to fractional part
            mov ax,2E30h            ; '0.'
            add al,[buffer]         ;Convert digit to ASCII
            stosw                   ;Store 1st digit & decimal point

            mov cx,[prec]           ;CX = precision

p1_mloop:   fimul word ptr [ten]    ;Multiply by 10
            fist word ptr [buffer]  ;Store as integer
            fisub word ptr [buffer] ;Reduce to fractional part
            mov al,[buffer]         ;AL = digit
            add al,'0'              ;Convert to ASCII
            stosb                   ;Store digit
            loop p1_mloop           ;Loop back

            mov bx,[exponent]       ;Exponent 0 or -8000h, quit
            test bx,7FFFh
            jz p1_done

            mov al,'e'              ;Store an 'e'
            stosb

            test bx,bx              ;Negative?
            jge p1_expok
            mov al,'-'              ;Store minus sign
            stosb
            neg bx                  ;Make it positive

p1_expok:   xchg bx,ax              ;BX = exponent
            xor cx,cx               ;Zero CX
            mov si,10               ;SI = 10

p1_dloop:   xor dx,dx               ;Divide by 10
            div si
            add dl,'0'              ;Convert to digit
            push dx                 ;Push digit
            inc cx
            test ax,ax              ;Loop back
            jnz p1_dloop

p1_ploop:   pop ax                  ;Pop digit
            stosb                   ;Store digit
            loop p1_ploop           ;Loop back

            xor al,al               ;Add the null byte
            stosb

p1_done:    fldenv [save_buf]       ;Restore FPU state
            fstp st(0)              ;Pop data off stack
            pop es                  ;Restore registers
            popa
            leave                   ;Delete stack frame
            ret 6                   ;Return

ftoa        endp
