Author Topic: Converting a Real10 to string  (Read 18868 times)

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Converting a Real10 to string
« on: April 28, 2013, 05:26:33 AM »
Hi
   
    ConvertFloat10DS is a procedure that converts a real10
   - integer digits (up to 19)- to a qword integer and
    then to a string. It doesnt use BCD and we may
    understand it in 4/5 simple steps;)

    We can get the complete file ConverterQQ.inc
    inside the folder ConverterQQ.

EDIT: please add this:
                       $TRUNCATE$     equ 027fh
 

Quote
; Input:
;           pReal10 = pointer                                       dd 0    <------- for lenght
;           pRcl   --------------> buffer shoud be  _buffer db x dup (?), 0
;
ConvertFloat10DS    proc     pReal10:DWORD, pRcl:DWORD
                    LOCAL    expscale     :DWORD
                    LOCAL    expscaleM    :DWORD
                    LOCAL    expscaleS    :DWORD
                    LOCAL    exponent     :DWORD
                    LOCAL    QWinteger    :QWORD
                    LOCAL    oldcw        :WORD
                    LOCAL    truncw       :WORD
                    LOCAL    string[20]   :BYTE
                    LOCAL    Real10Buf[10]:BYTE
                                       
                    push     ebx
                    push     esi
                    push     edi                   
                   
                    mov      edi, pRcl     
                    mov      ebx, pReal10

                    ; --------------------------
                    ;          examine
                    ; --------------------------
                    fld      tbyte ptr [ebx]
                    fxam
                    fstsw    ax               
                    fwait
                    sahf
                    jz       short @F
                    jpo      _erro1          ;C3=0 and C2=0 would be NAN or unsupported
                    jnc      short _start

                    ;-------------
                    ; is  INFINITY
                    ;-------------                   
                    mov      al,"+"
                    test     ah, 2             ;C1 field = sign
                    jz       _isinfinity
                    mov      al,"-"
                    jnz      _isinfinity
                                     
                    ;--------------------------
                    ; may be 0  or Denormalized
                    ;--------------------------
            @@:     jpe      short _isDenormal      ; comment this and
                    jmp      _iszero                           ; jpo     _iszero
                   
                    ; ---------------
                    ; Is demormalized
                    ; ---------------
    _isDenormal:    lea      ebx, Real10Buf
                    fstp     tbyte ptr [ebx]
                    or       word ptr [ebx+8], 1
                    fld      tbyte ptr [ebx]                   
                                                                                     
                    ;--------------------------------------
                    ;           get the exponent
                    ; exponent = characteristic of log10(X)

                    ;--------------------------------------
        _start:     fldlg2                  ; st(0)=log10(2) ; st(1)=X
                    fld      st(1)          ; copy st(1)=X to st(0)
                    fabs                    ; positive value
                    fyl2x                   ; log10(2)*log2(X) = log10(X)

                    ; ---------------------------------
                    ; X          in st(2) st(1)
                    ; log10(2)   in st(1) removed fyl2x
                    ; |X|        in st(0) removed fyl2x
                    ; log10(|X|) in st(0) st(0)   
                    ; ---------------------------------                                           

                    ; -----------------------------------------------------------
                    ;       Convert the logarithm to a truncated integer
                    ;  store the truncating value of the logarithm to exponent

                    ; -----------------------------------------------------------     
                    SETTRUNCATE oldcw, truncw
                    fistp    exponent
                    fldcw    oldcw          ; load back the former control word

                    ; -----------------------------
                    ; Get exponent scale factor
                    ; -----------------------------
                    mov     eax, $EXPONENT_REAL10_MAX
                    mov     edx, $DECIMALPLACES_REAL10
                    sub     edx, exponent
                    mov     expscale, edx
                    mov     expscaleM, edx
           
                    mov     ebx, offset _PowerTblQQP38
       
                    or      edx, edx
                    jns     short _cmpexpscale

                    neg     edx
                    mov     ebx, offset _PowerTblQQM38
           
                    cmp     edx, 46
                    jbe     short _load0

                    ; ------------------------------------------------
                    ; compute 10^expscale= 10^expscaleM * 10^expscaleS
                    ;    (where expscale= expscaleM + expscaleS)
                    ;     if expscale > $EXPONENT_REAL10_MAX 
                    ;   because 10^$EXPONENT_REAL10_MAX+2= INFINITY

                    ; ------------------------------------------------
    _power10:       cmp     edx, eax
                    jbe     short @F
                    ;
                    mov     expscaleM,  eax
                    sub     edx, eax
                    mov     expscaleS, edx                   

                    ; -----------------------------------
                    ;        compute 10^expscaleS
                    ; -----------------------------------                   
                    fild    expscaleS
                    fldl2t             ; log2(10)
                    fmul               ; log2(10)*expscale
                    fld     st
                    frndint            ; get the characteristic
                    fxch
                    fsub    st,st(1)   ; get only the mantissa but keep the characteristic
                    f2xm1              ; 2^(mantissa)-1
                    fld1
                    fadd               ; add 1 and pop
                    fscale
                    fstp    st(1)      ; remove the characteristic
                    ;
                    fmul               ; X * 10^expscaleS

                    ; -----------------------------------
                    ;        compute 10^expscaleM
                    ; -----------------------------------
            @@:     fild    expscaleM
                    fldl2t             ; log2(10)
                    fmul               ; log2(10)*expscale
                    fld     st
                    frndint            ; get the characteristic
                    fxch
                    fsub    st,st(1)   ; get only the mantissa but keep the characteristic
                    f2xm1              ; 2^(mantissa)-1
                    fld1
                    fadd               ; add 1 and pop
                    fscale
                    fstp    st(1)      ; remove the characteristic
                    jmp     _multiply

    _cmpexpscale:   cmp     edx, 46
                    ja      short _power10
           
        _load0:     shl     edx, 4
                    fld     tbyte ptr [ebx+edx]                   
                   
                    ; -------------------------                   
                    ; Multiply X by 10^expscale
                    ; -------------------------
    _multiply:      fmul

                    ; -----------------
                    ; store the integer
                    ; -----------------
                    fistp   QWinteger
                    fstsw   ax
                    fwait
                    shr     ax,1              ;test for invalid operation
                    jc      _erro2 

                    ; ---------------------------
                    ; convert QWinteger to string
                    ; ---------------------------
                    lea     esi, QWinteger
                    mov     eax, dword ptr [esi+0]
                    mov     edx, dword ptr [esi+4]

                    mov     bl, ' '
                    or      edx, edx
                    jns     short @F
                   
                    neg     eax
                    neg     edx
                    sub     edx, 1
                    ;
                    mov     bl, '-'
                    ; ------------------------
                    ;     Set the sign
                    ; ------------------------
            @@:     mov     byte ptr [edi], bl
                    add     edi, 1
                   
                    lea     esi, string+20

                    CONVERT1EDXEAX
                   
                    mov     edx, $DECIMALPLACES_REAL10
                   
                    ; -----------------------------------
                    ;     Recalculate exponent based
                    ; in the number of integer digits ebx

                    ; -----------------------------------
                    mov     eax, ebx
                    sub     eax, expscale
                    sub     eax, 1
                    mov     exponent, eax         ; we need only eax not exponent var
                   
                    ; --------------------------------------------------------
                    ; Regular notation or scientific notation ?
                    ; ebx = total number of digits in the buffer
                    ; edx = number of decimal places
                    ; eax = exponent
                    ; --------------------------------------------------------
               
                    or      eax, eax
                    jns     short _ispositive       ; exponent is positive

                    mov     ecx, eax
                    neg     ecx

                    ;cmp     ecx, 5 
                    cmp     ecx, edx
                    ja      _isscientific

                    ; ------------------------------
                    ; Regular notation - decimal
                    ; ------------------------------
                    mov     word ptr [edi], '.0'
                    add     edi, 2
                   
                    ; ------------------------------
                    ; If exponent = -1 it is 0.ddddd
                    ; If exponent = -2 it is 0.0dddd
                    ;         and so on
                    ; ------------------------------
                    sub     ecx, 1
                    jz      _movedecimal
                   
                    sub     edx, ecx
                   
                    ; -------------------------
                    ;        insert 0
                    ; -------------------------
            @@:     mov     byte ptr [edi], '0'
                    add     edi, 1
                    sub     ecx, 1
                    jnz     short @B

    _movedecimal:   movzx   eax, byte ptr [esi]
                    mov     byte ptr [edi], al
                    add     edi, 1
                    add     esi, 1

                    sub     edx, 1
                    jnz     short _movedecimal
                    jz      short _removeD
                    ; ------------------
                    ; remove last zeroes
                    ; ------------------
            @@:     sub     edi, 1
        _removeD:   movzx   eax, word ptr [edi-2]
                    cmp     al, '.'
                    je      _finish
                    ;
                    cmp     ah, '0'
                    je      short @B
                    jmp     _finish

            ;--------------------------------------------------
            ; ebx = total number of digits
            ; edx = number of decimal places
            ; eax = exponent
            ;--------------------------------------------------
                    ; -------------------
                    ;   exponent >= 0
                    ; -------------------
    _ispositive:    cmp     eax, edx
                    jae     _isscientific
                                   
                    ; ----------------------------------
                    ; Regular notation - integer
                    ; ----------------------------------
                    add     eax, 1              ; number of integer digits
                    sub     ebx, eax            ; number of decimal digits
                    ;
            @@:     movzx   edx, byte ptr [esi]
                    mov     byte ptr [edi], dl
                    add     edi, 1
                    add     esi, 1                   

                    sub     eax, 1
                    jnz     short @B
                    ;
                    or      ebx, ebx
                    jz      _finish
                   
                    mov     byte ptr [edi], '.'
                    add     edi, 1
                    ;
            @@:     movzx   eax, byte ptr [esi]
                    mov     byte ptr [edi], al
                    add     edi, 1
                    add     esi, 1                   

                    sub     ebx, 1
                    jnz     short @B
                    jmp     short _removeI
                    ; ------------------
                    ; remove last zeroes
                    ; ------------------
            @@:     sub     edi, 1
        _removeI:   movzx   eax, word ptr [edi-2]
                    cmp     al, '.'
                    je      _finish
                    ;
                    cmp     ah, '0'
                    je      short @B
                    jmp     _finish
                                       
            ;--------------------------------------------------
            ;                 is Scientific
            ;--------------------------------------------------
    _isscientific:  sub     ebx, 1
                   
                    movzx   edx, byte ptr [esi]
                    mov     dh, '.'                   
                    add     esi, 1
                    ;
                    mov     word ptr [edi], dx
                    add     edi, 2
                   
            @@:     movzx   edx, byte ptr [esi]
                    mov     byte ptr [edi], dl
                    ;
                    add     edi, 1
                    add     esi, 1

                    sub     ebx, 1
                    jnz     short @B
                    jz      short _removeE
                   
                    ; ------------------------
                    ; remove last zeroes
                    ; ------------------------
            @@:     sub     edi, 1
        _removeE:   movzx   edx, word ptr [edi-2]
                    cmp     dl, '.'
                    je      short @F
                    ;
                    cmp     dh, '0'
                    je      short @B

            @@:     EXPONENTREAL_10W
                           
        _finish:    mov      eax, edi
                    mov      edi, pRcl
                    sub      eax, edi

        _exit0:     mov      dword ptr [edi-4], eax
                    mov      byte ptr [edi+eax], 0
                                             
                    xor      eax, eax
                    clc
        _exit:      pop      edi
                    pop      esi
                    pop      ebx
                    ret
;------------------------------------------------------------
;                      fxam, ebp
;------------------------------------------------------------
    _iszero:        fstp     st(0)                                     
                    mov      word ptr [edi], '0 '   ;3020h
                    mov      eax, 2
                    jmp      _exit0
           
    _isinfinity:    fstp     st(0)
                    mov      byte ptr [edi], al
                    mov      dword ptr [edi+1], "IFNI"
                    mov      dword ptr [edi+5], "YTIN"
                    ;
                    mov      eax, 9
                    jmp      _exit0
                   
                    ; --------------------
                    ; is indefinite or NAN
                    ; --------------------
        _erro1:     fstp     st(0)
                    mov      eax, 1
                    ;
        _error:     mov      dword ptr [edi+0], "ORRE"
                    mov      byte ptr [edi+4], "R"
                    ;
                    mov      dword ptr [edi-4], 5
                    mov      byte ptr [edi+5], 0
                    stc
                    jmp      _exit
                                       
                    ; -----------------
                    ; invalid operation
                    ; -----------------
        _erro2:     fclex
                    mov      eax, 2
                    stc
                    jmp      _error
ConvertFloat10DS    endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Some results
Code: [Select]
0000_80000000_00000000 = 3.3621031431120935E-4932
0000_FFFFFFFF_FFFFFFFF = 6.724206286224187E-4932
0000_00000000_00000001 =ERROR
0000_FFFFFFFF_FFFFFFFF =ERROR
7FFF_80000000_00000000 =+INFINITY
FFFF_80000000_00000000 =-INFINITY
7FFE_FFFFFFFF_FFFFFFFF = 1.18973149535723177E+4932
FFFE_FFFFFFFF_FFFFFFFF =-1.18973149535723177E+4932
 *** STOP - ConvertFloat10DS  end test digits ***
 0
 1.0
 2.0
 3.0
 4.0
 5.0
 6.0
 7.0
 8.0
 9.0
 0.1
 0.2
 0.3
 0.4
 0.5
 0.6
 0.7
 0.8
 0.9
 *** STOP - ConvertFloat10DS ***
 0.8
 0.08
 0.008
 0.0008
 0.00008
 0.000008
 0.0000008
 0.00000008
 0.000000008
 0.0000000008
 0.00000000008
 0.000000000008
 0.0000000000008
 0.00000000000008
 0.000000000000008
 0.0000000000000008
 0.00000000000000008
 8.0E-18
 *** STOP - ConvertFloat10DS ***
 0.9
 0.09
 0.009
 0.0009
 0.00009
 0.000009
 0.0000009
 0.00000009
 0.000000009
 0.0000000009
 0.00000000009
 0.000000000009
 0.0000000000009
 0.00000000000009
 0.000000000000009
 0.0000000000000009
 0.00000000000000009
 9.0E-18
 *** STOP - ConvertFloat10DS ***
-1.23456789123456
-12.3456789123456
-123.456789123456
-1234.56789123456
-12345.6789123456
-123456.789123456
-1234567.89123456
-12345678.9123456
-12345678.9123456
-123456789.123456
-1234567891.123456
-12345678912.123456
-123456789123.123456
-1234567891234.12346
-12345678912345.1235
-123456789123456.123
-1234567891234567.12
-12345678912345678.1
-1.23456789123456789E+17
 *** STOP - ConvertFloat10DS --- E N D ---**
« Last Edit: June 25, 2014, 12:32:46 AM by RuiLoureiro »

jj2007

  • Member
  • *****
  • Posts: 7756
  • Assembler is fun ;-)
    • MasmBasic
Re: Converting a Real10 to string
« Reply #1 on: April 28, 2013, 11:14:44 AM »
ConverterQQ.inc(424) : error A2006: undefined symbol : $TRUNCATE$  ::)

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #2 on: April 28, 2013, 07:49:55 PM »
ConverterQQ.inc(424) : error A2006: undefined symbol : $TRUNCATE$  ::)
              Thanks Jochen,

$TRUNCATE$     equ 027fh

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #3 on: May 11, 2013, 09:18:24 AM »
My last tests about real10 to string (47 procedures)
conclusion: BCD are better

For 10 digits:
Quote
***** Time table *****

Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)

341  cycles, ConvertFloat10DX, direct, fxam, fxtract, esp - 10 digits
341  cycles, ConvertFloat10DY, direct, examine, fxtract, esp - 10 digits
347  cycles, ConvertReal10DX, direct, fxam, fxtract, esp - 10 digits
356  cycles, ConvertReal10DY, direct, examine, fxtract, esp - 10 digits
356  cycles, ConvertReal10DR, direct, examine, fxtract, ebp - 10 digits
356  cycles, ConvertFloat10DR, direct, examine, fxtract, ebp - 10 digits
365  cycles, ConvertFloat10DRD, direct, examine, fxtract, ebp - 10 digits
365  cycles, ConvertReal10DRD, direct, examine, fxtract, ebp - 10 digits
411  cycles, ConvertFloat10DYD, direct, examine, fxtract, esp - 10 digits
414  cycles, ConvertFloat10DXD, direct, fxam, fxtract, esp - 10 digits
418  cycles, ConvertReal10DYD, direct, examine, fxtract, esp - 10 digits
423  cycles, ConvertReal10DXD, direct, fxam, fxtract, esp - 10 digits
475  cycles, ConvertFloat10DF, direct, examine, fyl2x, ebp - 10 digits
478  cycles, ConvertFloat10DS, direct, fxam, fyl2x, ebp - 10 digits
479  cycles, ConvertReal10DF, direct, examine, fyl2x, ebp - 10 digits
486  cycles, ConvertReal10DSD, direct, fxam, fyl2x, ebp - 10 digits
487  cycles, ConvertFloat10DFD, direct, examine, fyl2x, ebp - 10 digits
487  cycles, ConvertFloat10DSD, direct, fxam, fyl2x, ebp - 10 digits
489  cycles, ConvertReal10DS, direct, fxam, fyl2x, ebp - 10 digits
492  cycles, ConvertReal10DFD, direct, examine, fyl2x, ebp - 10 digits
716  cycles, ConvertFloat10CT, BCD-CT, fxam, fxtract, esp - 10 digits
724  cycles, ConvertFloat10BX, BCD, fxam, fxtract, ebp - 10 digits
729  cycles, ConvertReal10CTD, BCD-CT, fxam, fxtract, esp - 10 digits
731  cycles, ConvertReal10BX, BCD, fxam, fxtract, ebp - 10 digits
739  cycles, ConvertFloat10BY, BCD, examine, fxtract, esp - 10 digits
739  cycles, ConvertFloat10CTD, BCD-CT, fxam, fxtract, esp - 10 digits
742  cycles, ConvertReal10BFD, BCD, fxam, fxtract, esp - 10 digits
744  cycles, ConvertFloat10BF, BCD, fxam, fxtract, esp - 10 digits
744  cycles, ConvertReal10BYD, BCD, examine, fxtract, esp - 10 digits
745  cycles, ConvertFloat10BFD, BCD, fxam, fxtract, esp - 10 digits
745  cycles, ConvertFloat10BYD, BCD, examine, fxtract, esp - 10 digits
746  cycles, ConvertReal10BY, BCD, examine, fxtract, esp - 10 digits
749  cycles, ConvertReal10BXD, BCD, fxam, fxtract, ebp - 10 digits
750  cycles, ConvertFloat10BXD, BCD, fxam, fxtract, ebp - 10 digits
752  cycles, ConvertReal10BF, BCD, fxam, fxtract, esp - 10 digits
775  cycles, ConvertReal10CT, BCD-CT, fxam, fxtract, esp - 10 digits
1147  cycles, ConvertFloat10ZX, BCD - old - 10 digits
1154  cycles, ConvertFloat10Z, BCD -old - 10 digits
2704  cycles, ConvertFloat10DWD, direct,Save FPU, fxam, fxtract, esp -10 digits
2709  cycles, ConvertReal10DWD, direct,Save FPU, fxam, fxtract, esp -10 digits
2798  cycles, ConvertFloat10DW, direct,Save FPU, fxam, fxtract, esp -10 digits
2816  cycles, ConvertReal10DW, direct,Save FPU, fxam, fxtract, esp -10 digits
2989  cycles, ConvertFloat10BW, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
2995  cycles, ConvertReal10BW, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
3042  cycles, ConvertFloat10, BCD, Save FPU -old - 10 digits
3104  cycles, ConvertFloat10BWD, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
3126  cycles, ConvertReal10BWD, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
 ********** END **********


For 15 digits:
Quote

 ***** Time table *****

Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)

756  cycles, ConvertFloat10BX, BCD, fxam, fxtract, ebp - 15 digits
769  cycles, ConvertFloat10BFD, BCD, fxam, fxtract, esp - 15 digits
769  cycles, ConvertReal10BYD, BCD, examine, fxtract, esp - 15 digits
770  cycles, ConvertReal10BFD, BCD, fxam, fxtract, esp - 15 digits
770  cycles, ConvertFloat10BYD, BCD, examine, fxtract, esp - 15 digits
771  cycles, ConvertReal10BY, BCD, examine, fxtract, esp - 15 digits
772  cycles, ConvertFloat10BF, BCD, fxam, fxtract, esp - 15 digits
776  cycles, ConvertFloat10CT, BCD-CT, fxam, fxtract, esp - 15 digits
778  cycles, ConvertFloat10BY, BCD, examine, fxtract, esp - 15 digits
779  cycles, ConvertReal10BXD, BCD, fxam, fxtract, ebp - 15 digits
779  cycles, ConvertFloat10BXD, BCD, fxam, fxtract, ebp - 15 digits
791  cycles, ConvertReal10CTD, BCD-CT, fxam, fxtract, esp - 15 digits
797  cycles, ConvertFloat10CTD, BCD-CT, fxam, fxtract, esp - 15 digits
800  cycles, ConvertReal10CT, BCD-CT, fxam, fxtract, esp - 15 digits
801  cycles, ConvertReal10BX, BCD, fxam, fxtract, ebp - 15 digits
802  cycles, ConvertReal10BF, BCD, fxam, fxtract, esp - 15 digits
1024  cycles, ConvertFloat10DR, direct, examine, fxtract, ebp - 15 digits
1029  cycles, ConvertFloat10DRD, direct, examine, fxtract, ebp - 15 digits
1032  cycles, ConvertReal10DR, direct, examine, fxtract, ebp - 15 digits
1035  cycles, ConvertReal10DRD, direct, examine, fxtract, ebp - 15 digits
1041  cycles, ConvertReal10DX, direct, fxam, fxtract, esp - 15 digits
1042  cycles, ConvertFloat10DX, direct, fxam, fxtract, esp - 15 digits
1046  cycles, ConvertFloat10DY, direct, examine, fxtract, esp - 15 digits
1051  cycles, ConvertReal10DY, direct, examine, fxtract, esp - 15 digits
1066  cycles, ConvertFloat10ZX, BCD - old - 15 digits
1068  cycles, ConvertFloat10Z, BCD -old - 15 digits
1094  cycles, ConvertReal10DYD, direct, examine, fxtract, esp - 15 digits
1097  cycles, ConvertFloat10DYD, direct, examine, fxtract, esp - 15 digits
1098  cycles, ConvertFloat10DXD, direct, fxam, fxtract, esp - 15 digits
1130  cycles, ConvertReal10DXD, direct, fxam, fxtract, esp - 15 digits
1141  cycles, ConvertFloat10DS, direct, fxam, fyl2x, ebp - 15 digits
1161  cycles, ConvertFloat10DF, direct, examine, fyl2x, ebp - 15 digits
1167  cycles, ConvertFloat10DFD, direct, examine, fyl2x, ebp - 15 digits
1170  cycles, ConvertFloat10DSD, direct, fxam, fyl2x, ebp - 15 digits
1173  cycles, ConvertReal10DS, direct, fxam, fyl2x, ebp - 15 digits
1176  cycles, ConvertReal10DFD, direct, examine, fyl2x, ebp - 15 digits
1181  cycles, ConvertReal10DSD, direct, fxam, fyl2x, ebp - 15 digits
1187  cycles, ConvertReal10DF, direct, examine, fyl2x, ebp - 15 digits
2938  cycles, ConvertFloat10, BCD, Save FPU -old - 15 digits
3024  cycles, ConvertReal10BW, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3027  cycles, ConvertFloat10BW, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3139  cycles, ConvertFloat10BWD, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3165  cycles, ConvertReal10BWD, BCD, Save FPU, fxam, fxtract, ebp - 15 digits
3344  cycles, ConvertReal10DWD, direct,Save FPU, fxam, fxtract, esp -15 digits
3356  cycles, ConvertFloat10DWD, direct,Save FPU, fxam, fxtract, esp -15 digits
3447  cycles, ConvertReal10DW, direct,Save FPU, fxam, fxtract, esp -15 digits
3463  cycles, ConvertFloat10DW, direct,Save FPU, fxam, fxtract, esp -15 digits
 ********** END **********

For 18 digits:
Quote
***** Time table *****

Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)

822  cycles, ConvertReal10BYD, BCD, examine, fxtract, esp - 18 digits
826  cycles, ConvertReal10BF, BCD, fxam, fxtract, esp - 18 digits
827  cycles, ConvertReal10BY, BCD, examine, fxtract, esp - 18 digits
827  cycles, ConvertFloat10BYD, BCD, examine, fxtract, esp - 18 digits
827  cycles, ConvertFloat10BF, BCD, fxam, fxtract, esp - 18 digits
830  cycles, ConvertFloat10BY, BCD, examine, fxtract, esp - 18 digits
831  cycles, ConvertReal10BFD, BCD, fxam, fxtract, esp - 18 digits
831  cycles, ConvertFloat10BFD, BCD, fxam, fxtract, esp - 18 digits
832  cycles, ConvertFloat10BXD, BCD, fxam, fxtract, ebp - 18 digits
833  cycles, ConvertReal10BXD, BCD, fxam, fxtract, ebp - 18 digits
836  cycles, ConvertFloat10BX, BCD, fxam, fxtract, ebp - 18 digits
856  cycles, ConvertReal10CT, BCD-CT, fxam, fxtract, esp - 18 digits
859  cycles, ConvertFloat10CTD, BCD-CT, fxam, fxtract, esp - 18 digits
860  cycles, ConvertReal10CTD, BCD-CT, fxam, fxtract, esp - 18 digits
870  cycles, ConvertReal10BX, BCD, fxam, fxtract, ebp - 18 digits
894  cycles, ConvertFloat10CT, BCD-CT, fxam, fxtract, esp - 18 digits
1080  cycles, ConvertFloat10Z, BCD -old - 18 digits
1085  cycles, ConvertFloat10ZX, BCD - old - 18 digits
1402  cycles, ConvertReal10DR, direct, examine, fxtract, ebp - 18 digits
1403  cycles, ConvertFloat10DR, direct, examine, fxtract, ebp - 18 digits
1409  cycles, ConvertReal10DRD, direct, examine, fxtract, ebp - 18 digits
1410  cycles, ConvertFloat10DX, direct, fxam, fxtract, esp - 18 digits
1412  cycles, ConvertReal10DX, direct, fxam, fxtract, esp - 18 digits
1429  cycles, ConvertReal10DY, direct, examine, fxtract, esp - 18 digits
1436  cycles, ConvertFloat10DY, direct, examine, fxtract, esp - 18 digits
1449  cycles, ConvertFloat10DRD, direct, examine, fxtract, ebp - 18 digits
1470  cycles, ConvertReal10DYD, direct, examine, fxtract, esp - 18 digits
1472  cycles, ConvertFloat10DYD, direct, examine, fxtract, esp - 18 digits
1472  cycles, ConvertReal10DXD, direct, fxam, fxtract, esp - 18 digits
1477  cycles, ConvertFloat10DXD, direct, fxam, fxtract, esp - 18 digits
1533  cycles, ConvertFloat10DFD, direct, examine, fyl2x, ebp - 18 digits
1538  cycles, ConvertReal10DFD, direct, examine, fyl2x, ebp - 18 digits
1550  cycles, ConvertFloat10DS, direct, fxam, fyl2x, ebp - 18 digits
1552  cycles, ConvertFloat10DF, direct, examine, fyl2x, ebp - 18 digits
1558  cycles, ConvertReal10DS, direct, fxam, fyl2x, ebp - 18 digits
1559  cycles, ConvertFloat10DSD, direct, fxam, fyl2x, ebp - 18 digits
1562  cycles, ConvertReal10DSD, direct, fxam, fyl2x, ebp - 18 digits
1566  cycles, ConvertReal10DF, direct, examine, fyl2x, ebp - 18 digits
3003  cycles, ConvertFloat10, BCD, Save FPU -old - 18 digits
3038  cycles, ConvertFloat10BW, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3039  cycles, ConvertReal10BW, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3157  cycles, ConvertFloat10BWD, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3165  cycles, ConvertReal10BWD, BCD, Save FPU, fxam, fxtract, ebp - 18 digits
3723  cycles, ConvertReal10DWD, direct,Save FPU, fxam, fxtract, esp -18 digits
3749  cycles, ConvertFloat10DWD, direct,Save FPU, fxam, fxtract, esp -18 digits
3843  cycles, ConvertFloat10DW, direct,Save FPU, fxam, fxtract, esp -18 digits
3898  cycles, ConvertReal10DW, direct,Save FPU, fxam, fxtract, esp -18 digits
 ********** END **********


For 19 digits- no BCD:
Quote
***** Time table *****

Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)

1546  cycles, ConvertFloat10DR, direct, examine, fxtract, ebp - 19 digits
1570  cycles, ConvertReal10DR, direct, examine, fxtract, ebp - 19 digits
1573  cycles, ConvertReal10DRD, direct, examine, fxtract, ebp - 19 digits
1584  cycles, ConvertFloat10DRD, direct, examine, fxtract, ebp - 19 digits
1592  cycles, ConvertFloat10DX, direct, fxam, fxtract, esp - 19 digits
1594  cycles, ConvertFloat10DY, direct, examine, fxtract, esp - 19 digits
1607  cycles, ConvertReal10DX, direct, fxam, fxtract, esp - 19 digits
1609  cycles, ConvertReal10DY, direct, examine, fxtract, esp - 19 digits
1635  cycles, ConvertReal10DYD, direct, examine, fxtract, esp - 19 digits
1663  cycles, ConvertFloat10DXD, direct, fxam, fxtract, esp - 19 digits
1675  cycles, ConvertFloat10DYD, direct, examine, fxtract, esp - 19 digits
1705  cycles, ConvertReal10DXD, direct, fxam, fxtract, esp - 19 digits
1727  cycles, ConvertFloat10DFD, direct, examine, fyl2x, ebp - 19 digits
1728  cycles, ConvertFloat10DS, direct, fxam, fyl2x, ebp - 19 digits
1728  cycles, ConvertFloat10DSD, direct, fxam, fyl2x, ebp - 19 digits
1729  cycles, ConvertFloat10DF, direct, examine, fyl2x, ebp - 19 digits
1731  cycles, ConvertReal10DF, direct, examine, fyl2x, ebp - 19 digits
1740  cycles, ConvertReal10DS, direct, fxam, fyl2x, ebp - 19 digits
1745  cycles, ConvertReal10DSD, direct, fxam, fyl2x, ebp - 19 digits
1765  cycles, ConvertReal10DFD, direct, examine, fyl2x, ebp - 19 digits
3893  cycles, ConvertFloat10DWD, direct,Save FPU, fxam, fxtract, esp -19 digits
3904  cycles, ConvertReal10DWD, direct,Save FPU, fxam, fxtract, esp -19 digits
3976  cycles, ConvertReal10DW, direct,Save FPU, fxam, fxtract, esp -19 digits
3982  cycles, ConvertFloat10DW, direct,Save FPU, fxam, fxtract, esp -19 digits
 ********** END **********

Mikl__

  • Member
  • ****
  • Posts: 555
Re: Converting a Real10 to string
« Reply #4 on: May 12, 2013, 09:16:39 PM »
Hi, RuiLoureiro!
This is my procedure for Converting a Real10 to string and it's faster your procedure
The accuracy of this procedure is 19 digits
Code: [Select]
; masm windows console #
.686
.model flat
include windows.inc
includelib user32.lib
includelib kernel32.lib
extern _imp__MessageBoxA@16:dword
extern _imp__ExitProcess@4:dword
extern _imp__GetStdHandle@4:dword;
extern _imp__WriteConsoleA@20:dword;
extern _imp__SetConsoleTitleA@4:dword;
extern _imp__SetConsoleScreenBufferSize@8:dword;
extern _imp__FreeConsole@0:dword;
extern _imp__AllocConsole@0:dword;
extern _imp__CharToOemA@8:dword
extern _imp__ReadConsoleA@20:dword
STD_OUTPUT_HANDLE    equ -11;для вывода
STD_INPUT_HANDLE     equ -10;для ввода
$max equ 60; <-- This number may be increased
magic1 equ 4D10h;lg(2)*65536=19728,3...
magic2  equ 35269h;log2(10)*65536=3,32192809488736234780*65536=217705,8796265381788254208..
.data
dt 1.0e$max
dw 0
irp k,<59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,\
       38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,\
       16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1>
dt 1.0e&k
dw 0
endm
tabs dt 1.0
dw 0
irp k,<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,\
       26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,\
       48,49,50,51,52,53,54,55,56,57,58,59,$max>
dt 1.0e-&k
dw 0
endm
table0 db "000102030405060708091011121314151617181920212223242526272829"
       db "303132333435363738394041424344454647484950515253545556575859"
       db "606162636465666768697071727374757677787980818283848586878889"
       db "90919293949596979899"
table1 dd shift0,shift1,shift2,shift3,shift4
string db 40 dup(0)
crd COORD <20,225>
HANDL  dd ?
HANDL1 dd ?
TITL db 0
STR1 db 'Press Enter to continue'
LENS db ?
xxx dt 000008000000000000000r;=3.3621031431120935E-4932
dt 00000FFFFFFFFFFFFFFFFr;6.724206286224187E-4932
;dt 000000000000000000001r; =ERROR
dt 07FFF8000000000000000r;  Infinity
dt 0FFFF8000000000000000r; -Infinity
dt 0FFFFC000000000000000r; -Uncertainty
dt 07FFFC000000000000000r;  Uncertainty
dt 07FFF9000000000000000r;  Signal Non a Number
dt 0FFFF9000000000000000r; -Signal Non a Number
dt 0FFFFD000000000000000r; -Quiet Non a Number
dt 07FFFD000000000000000r;  Quiet Non a Number
dt 07FFF7000000000000000r;  Error
dt 0FFFF7000000000000000r; -Error
dt 07FFEFFFFFFFFFFFFFFFFr; = 1.18973149535723177E+4932
dt 0FFFEFFFFFFFFFFFFFFFFr; =-1.18973149535723177E+4932
dt -0.0
dt 0
dt 1.0
dt 2.0
dt 3.0
dt 4.0
dt 5.0
dt 6.0
dt 7.0
dt 8.0
dt 9.0
dt 0.1
dt 0.2
dt 0.3
dt 0.4
dt 0.5
dt 0.6
dt 0.7
dt 0.8
dt 0.9
dt -1.1
dt -11.1
dt -111.1
dt -1111.1
dt -11111.1 
dt -111111.1
dt -1111111.1
dt -11111111.1
dt -111111111.1
dt -1111111111.1
dt -11111111111.1
dt -111111111111.1
dt -1111111111111.1
dt -11111111111111.1
dt -111111111111111.1
dt -1111111111111111.1
dt -11111111111111111.1
dt -111111111111111111.1
dt -1111111111111111111.1
dt -2.2
dt -22.2
dt -222.2
dt -2222.2
dt -22222.2
dt -222222.2
dt -2222222.2
dt -22222222.2
dt -222222222.2
dt -2222222222.2
dt -22222222222.2
dt -222222222222.2
dt -2222222222222.2
dt -22222222222222.2
dt -222222222222222.2
dt -2222222222222222.2
dt -22222222222222222.2
dt -222222222222222222.2
dt -2222222222222222222.2
dt -3.3
dt -33.3
dt -333.3
dt -3333.3
dt -33333.3
dt -333333.3
dt -3333333.3
dt -33333333.3
dt -333333333.3
dt -3333333333.3
dt -33333333333.3
dt -333333333333.3
dt -3333333333333.3
dt -33333333333333.3
dt -333333333333333.3
dt -3333333333333333.3
dt -33333333333333333.3
dt -333333333333333333.3
dt -3333333333333333333.3
dt -4.4
dt -44.4
dt -444.4
dt -4444.4
dt -44444.4
dt -444444.4
dt -4444444.4
dt -44444444.4
dt -444444444.4
dt -4444444444.4
dt -44444444444.4
dt -444444444444.4
dt -4444444444444.4
dt -44444444444444.4
dt -444444444444444.4
dt -4444444444444444.4
dt -44444444444444444.4
dt -444444444444444444.4
dt -4444444444444444444.4
dt -5.5
dt -55.5
dt -555.5
dt -5555.5
dt -55555.5
dt -555555.5
dt -5555555.5
dt -55555555.5
dt -555555555.5
dt -5555555555.5
dt -55555555555.5
dt -555555555555.5
dt -5555555555555.5
dt -55555555555555.5
dt -555555555555555.5
dt -5555555555555555.5
dt -55555555555555555.5
dt -555555555555555555.5
dt -5555555555555555555.5
dt -6.6
dt -66.6
dt -666.6
dt -6666.6
dt -66666.6
dt -666666.6
dt -6666666.6
dt -66666666.6
dt -666666666.6
dt -6666666666.6
dt -66666666666.6
dt -666666666666.6
dt -6666666666666.6
dt -66666666666666.6
dt -666666666666666.6
dt -6666666666666666.6
dt -66666666666666666.6
dt -666666666666666666.6
dt -6666666666666666666.6
dt -7.7
dt -77.7
dt -777.7
dt -7777.7
dt -77777.7
dt -777777.7
dt -7777777.7
dt -77777777.7
dt -777777777.7
dt -7777777777.7
dt -77777777777.7
dt -777777777777.7
dt -7777777777777.7
dt -77777777777777.7 ;ecx=3
dt -777777777777777.7
dt -7777777777777777.7
dt -77777777777777777.7
dt -777777777777777777.7
dt -7777777777777777777.7
dt -8.8
dt -88.8
dt -888.8
dt -8888.8
dt -88888.8
dt -888888.8
dt -8888888.8;ecx=4
dt -88888888.8
dt -888888888.8
dt -8888888888.8
dt -88888888888.8
dt -888888888888.8
dt -8888888888888.8
dt -88888888888888.8
dt -888888888888888.8
dt -8888888888888888.8
dt -88888888888888888.8
dt -888888888888888888.8
dt -8888888888888888888.8
dt -9.9
dt -99.9;the result of multiplying is -9.9900000000000000010
dt -999.9
dt -9999.9
dt -99999.9
dt -999999.9
dt -9999999.9
dt -99999999.9
dt -999999999.9;-9.9999999990000000010
dt -9999999999.9;ecx=4
dt -99999999999.9
dt -999999999999.9
dt -9999999999999.9;the result of multiplying is -9.9999999999998999990
dt -99999999999999.9
dt -999999999999999.9
dt -9999999999999999.9
dt -99999999999999999.9
dt -999999999999999999.9
dt -9999999999999999999.9
dt  0.1
dt  0.01
dt  0.001
dt  0.0001
dt  0.00001
dt  0.000001
dt  0.0000001
dt  0.00000001
dt  0.000000001
dt  0.0000000001
dt  0.00000000001
dt  0.000000000001
dt  0.0000000000001
dt  0.00000000000001
dt  0.000000000000001
dt  0.0000000000000001
dt  0.00000000000000001
dt  1.0E-18
dt  0.2
dt  0.02
dt  0.002
dt  0.0002
dt  0.00002
dt  0.000002
dt  0.0000002
dt  0.00000002
dt  0.000000002
dt  0.0000000002
dt  0.00000000002
dt  0.000000000002
dt  0.0000000000002
dt  0.00000000000002
dt  0.000000000000002
dt  0.0000000000000002
dt  0.00000000000000002
dt  2.0E-18
dt  0.3
dt  0.03
dt  0.003
dt  0.0003
dt  0.00003
dt  0.000003
dt  0.0000003
dt  0.00000003
dt  0.000000003
dt  0.0000000003
dt  0.00000000003
dt  0.000000000003
dt  0.0000000000003
dt  0.00000000000003
dt  0.000000000000003
dt  0.0000000000000003
dt  0.00000000000000003
dt  3.0E-18
dt  0.4
dt  0.04
dt  0.004
dt  0.0004
dt  0.00004
dt  0.000004
dt  0.0000004
dt  0.00000004
dt  0.000000004
dt  0.0000000004
dt  0.00000000004
dt  0.000000000004
dt  0.0000000000004
dt  0.00000000000004
dt  0.000000000000004
dt  0.0000000000000004
dt  0.00000000000000004
dt  4.0E-18
dt  0.5
dt  0.05
dt  0.005
dt  0.0005
dt  0.00005
dt  0.000005
dt  0.0000005
dt  0.00000005
dt  0.000000005
dt  0.0000000005
dt  0.00000000005
dt  0.000000000005
dt  0.0000000000005
dt  0.00000000000005
dt  0.000000000000005
dt  0.0000000000000005
dt  0.00000000000000005
dt  5.0E-18
dt  0.6
dt  0.06
dt  0.006
dt  0.0006
dt  0.00006
dt  0.000006
dt  0.0000006
dt  0.00000006
dt  0.000000006
dt  0.0000000006
dt  0.00000000006
dt  0.000000000006
dt  0.0000000000006
dt  0.00000000000006
dt  0.000000000000006
dt  0.0000000000000006
dt  0.00000000000000006
dt  6.0E-18
dt  0.7
dt  0.07
dt  0.007
dt  0.0007
dt  0.00007
dt  0.000007
dt  0.0000007
dt  0.00000007
dt  0.000000007
dt  0.0000000007
dt  0.00000000007
dt  0.000000000007
dt  0.0000000000007
dt  0.00000000000007
dt  0.000000000000007
dt  0.0000000000000007
dt  0.00000000000000007
dt  7.0E-18
dt  0.8
dt  0.08
dt  0.008
dt  0.0008
dt  0.00008
dt  0.000008
dt  0.0000008
dt  0.00000008
dt  0.000000008
dt  0.0000000008
dt  0.00000000008
dt  0.000000000008
dt  0.0000000000008
dt  0.00000000000008
dt  0.000000000000008
dt  0.0000000000000008
dt  0.00000000000000008
dt  8.0E-18
dt  0.9
dt  0.09
dt  0.009
dt  0.0009
dt  0.00009
dt  0.000009
dt  0.0000009
dt  0.00000009
dt  0.000000009
dt  0.0000000009
dt  0.00000000009
dt  0.000000000009
dt  0.0000000000009
dt  0.00000000000009
dt  0.000000000000009
dt  0.0000000000000009
dt  0.00000000000000009
dt  9.0E-18
dt -1.23456789123456
dt -12.3456789123456
dt -123.456789123456
dt -1234.56789123456
dt -12345.6789123456
dt -123456.789123456
dt -1234567.89123456
dt -12345678.9123456
dt -12345678.9123456
dt -123456789.123456
dt -1234567891.123456
dt -12345678912.123456
dt -123456789123.123456
dt -1234567891234.123456
dt -12345678912345.12345
dt -123456789123456.1234
dt -1234567891234567.123
dt -12345678912345678.12
dt -123456789123456789.1
dt -1234567891234567891.0
num = ($ - xxx)/10
.code
start: call _imp__FreeConsole@0;free the existing console
        call _imp__AllocConsole@0;we form a console
        push STD_INPUT_HANDLE
        call _imp__GetStdHandle@4;HANDL1 obtain input
        mov HANDL1,eax
        push STD_OUTPUT_HANDLE
        call _imp__GetStdHandle@4;HANDL get to output
        mov HANDL,eax
push crd
push eax
call _imp__SetConsoleScreenBufferSize@8
        push offset TITL
        call _imp__SetConsoleTitleA@4;definition window title
mov ebx,offset xxx
mov ecx,num/24
a11: push ecx
mov ecx,24
@@: push ecx
push offset string
push ebx
call Eprst
        push 0
        push offset LENS
        push dword ptr string
        push offset string+4
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
add ebx,10
pop ecx
loop @b
push 0
        push offset LENS
        push 23
        push offset STR1
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
push 0
        push offset LENS
        push 200
        push offset string
        push HANDL1
        call _imp__ReadConsoleA@20;waiting to enter a string of characters
pop ecx
loop a11
mov ecx,(num mod 24)
jecxz a12
@@: push ecx
push offset string
push ebx
call Eprst
        push 0
        push offset LENS
        push dword ptr string
        push offset string+4
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
add ebx,10
pop ecx
loop @b

a12:    push 0
        push offset LENS
        push 200
        push offset string
        push HANDL1
        call _imp__ReadConsoleA@20;waiting to enter a string of characters
push 0
call _imp__ExitProcess@4
;===================================================
Eprst proc uses edi esi ebx lpReal10:dword,lpDest:dword
local iExp:dword
local x[3]:dword
local temp1:dword
local temp2:dword
fninit
        mov ecx,lpReal10
fld tbyte ptr [ecx]
mov edi,lpDest
mov ebx,[ecx]
add edi,4
mov eax,[ecx+4]
mov [x],ebx
movzx edx,word ptr [ecx+8]
mov [x+4],eax
mov [x+8],edx
cmp edx,8000h
mov byte ptr [edi],'-'
sbb esi,esi
and esi,0Dh
sub [edi],esi
and edx,7FFFh
jnz a2
Zero_Or_Denormal: cmp edx,eax
jnz Denormal
        cmp ebx,eax
jnz Denormal
mov dword ptr [edi+1],'oreZ'
mov word ptr  [edi+5],0A0Dh
mov dword ptr [edi-4],7
        ret
a2: cmp edx,7FFFh
jz Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty
Denormal:mov esi,10
sub edx,16383
mov word ptr [edi+21],'+E'
imul edx,magic1
mov word ptr [edi+27],0A0Dh
and edx,0FFFF0000h
mov dword ptr [edi-4],29
sar edx,14
mov iExp,edx
jz @f
cmp edx,4*$max
jg a1
cmp edx,-4*$max
jge load
;------------------------------------------------
dec edx
a1: not edx
sar edx,2
mov temp1,edx
imul edx,magic2
sar edx,16
mov temp2,edx
fild temp2         ; get the characteristic
fild temp1         ; целая часть log2(10)*expscale
fldl2t    ; log2(10)
        fmul               ; log2(10)*expscale
        fsub st,st(1)      ; get only the mantissa but keep the characteristic
        f2xm1              ; 2^(mantissa)-1
        fld1
        fadd               ; add 1 and pop
        fscale
        fstp    st(1)      ; remove the characteristic
        jmp multiply
load: fld tabs[edx+edx*2]
multiply:fmul              ; X * 10^expscaleS
fld st
fstp tbyte ptr x
xor edx,edx
@@: mov ecx,x+8
mov ebx,x
        mov eax,x+4
and ecx,7FFFh
sub ecx,16382
jmp table1[ecx*4]
shift4::test eax,60000000h
jz a6
fld tabs[12]
fmul
add iExp,4
fstp tbyte ptr x
mov eax,x+4
        mov ebx,x
shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
jmp shift0
a6: shld edx,eax,4 ;ecx=1 ebx+4
shld eax,ebx,4 ;ecx=2 ebx+8
shl ebx,4      ;ecx=3 ebx+9
add ebx,12     ;ecx=4 ebx+12
jmp shift0
shift3::shld edx,eax,3
shld eax,ebx,3
shl ebx,3
add ebx,9;10
jmp shift0
shift2::shld edx,eax,2
shld eax,ebx,2
shl ebx,2
add ebx,8
jmp shift0
shift1::shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
shift0::adc eax,0
adc edx,0
mov [edi+1],dl
mov byte ptr [edi+2],','
or byte ptr [edi+1],'0'
k=3
rept 17
mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
mov ebx,eax
mov eax,ecx
k=k+1
endm

mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
;--------------------------------------
mov eax,iExp
mov edx,eax
sar eax,2
sar edx,31
mov ecx,esi
xor eax,edx
sub eax,edx
and edx,2
add byte ptr [edi+22],dl
mov esi,eax
mov edx,42949673;2^32/100
mul edx
mov ax,word ptr table0[edx*2] ;edx = quotient of the division by 100
mov word ptr [edi+23],ax
lea eax,[edx*4]
shl edx,2
lea edx,[edx+edx*2]
lea edx,[eax+edx*8] ;edx = quotient*100
sub esi,edx ;esi = remainder of the division by 100
mov ax,word ptr table0[esi*2]
        mov word ptr [edi+25],ax
ret
Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty:
cmp eax,80000000h
jnz SNAN_Or_QNAN_Or_Uncertainty
and ebx,ebx
jnz SNAN_Or_QNAN_Or_Uncertainty
        mov dword ptr [edi+1],'ifnI'
        mov dword ptr [edi+5],'ytin'
        mov word ptr  [edi+9],0A0Dh
mov dword ptr [edi-4],11
ret
SNAN_Or_QNAN_Or_Uncertainty:
test eax,eax
jns error
test eax,40000000h
jnz QNAN
        mov dword ptr [edi+1],'ngiS'
        mov dword ptr [edi+5],'N la'
        mov dword ptr [edi+9],'a no'
        mov dword ptr [edi+13],'muN '
        mov dword ptr [edi+17],0D726562h;'reb'
        mov dword ptr [edi+21],0Ah
mov dword ptr [edi-4],22
ret
QNAN: test eax,3FFFFFFFh
jnz @f
test ebx,ebx
jnz @f
        mov dword ptr [edi+1],'ecnU'
        mov dword ptr [edi+5],'iatr'
        mov dword ptr [edi+9],0D79746Eh;'ytn'
        mov byte ptr [edi+13],0Ah
mov dword ptr [edi-4],14
ret
@@: mov dword ptr [edi+1],'eiuQ'
        mov dword ptr [edi+5],'oN t'
        mov dword ptr [edi+9],' a n'
        mov dword ptr [edi+13],'bmuN'
        mov dword ptr [edi+17],0A0D7265h;'re'
mov dword ptr [edi-4],21
ret
error: mov dword ptr [edi+1],'orrE'
        mov dword ptr [edi+5],0A0D72h;'r'
mov dword ptr [edi-4],8
ret
Eprst endp
end start
« Last Edit: May 13, 2013, 12:24:07 AM by Mikl__ »

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #5 on: May 13, 2013, 03:49:03 AM »
Hi Mikl

This is what i get when i try to assemble your file
See the asm file Mikl1.zip
Code: [Select]
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: C:\MASM32\Converters_1\TestQQ1\Mikl1.asm
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(66) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(67) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(69) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(70) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(71) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(72) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(73) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(74) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(75) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(76) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(77) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(78) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(79) : error A2071: initializer magnitud
e too large for specified size
C:\MASM32\Converters_1\TestQQ1\Mikl1.asm(80) : error A2071: initializer magnitud
e too large for specified size
_
Assembly Error
Prima qualquer tecla para continuar . . . (this is: press any key to continue)

If i use this set of data

Code: [Select]
Eprst   proto   :DWORD,:DWORD
$max equ 60; <-- This number may be increased
magic1  equ 4D10h   ;lg(2)*65536=19728,3...
magic2  equ 35269h  ;log2(10)*65536=3,32192809488736234780*65536=217705,8796265381788254208..
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.data

dt 1.0e$max
dw 0
irp k,<59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,\
       38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,\
       16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1>
dt 1.0e&k
dw 0
endm
tabs dt 1.0
dw 0
irp k,<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,\
       26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,\
       48,49,50,51,52,53,54,55,56,57,58,59,$max>
dt 1.0e-&k
dw 0
endm

table0 db "000102030405060708091011121314151617181920212223242526272829"
       db "303132333435363738394041424344454647484950515253545556575859"
       db "606162636465666768697071727374757677787980818283848586878889"
       db "90919293949596979899"
table1 dd shift0,shift1,shift2,shift3,shift4

and this procedure

Code: [Select]
Eprst proc uses edi esi ebx lpReal10:dword,lpDest:dword
local iExp:dword
local x[3]:dword
local temp1:dword
local temp2:dword
fninit
        mov ecx,lpReal10
fld tbyte ptr [ecx]
mov edi,lpDest
mov ebx,[ecx]
add edi,4
mov eax,[ecx+4]
mov [x],ebx
movzx edx,word ptr [ecx+8]
mov [x+4],eax
mov [x+8],edx
cmp edx,8000h
mov byte ptr [edi],'-'
sbb esi,esi
and esi,0Dh
sub [edi],esi
and edx,7FFFh
jnz a2
Zero_Or_Denormal: cmp edx,eax
jnz Denormal
        cmp ebx,eax
jnz Denormal
mov dword ptr [edi+1],'oreZ'
mov word ptr  [edi+5],0A0Dh
mov dword ptr [edi-4],7
        ret
a2: cmp edx,7FFFh
jz Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty
Denormal:mov esi,10
sub edx,16383
mov word ptr [edi+21],'+E'
imul edx,magic1
mov word ptr [edi+27],0A0Dh
and edx,0FFFF0000h
mov dword ptr [edi-4],29
sar edx,14
mov iExp,edx
jz @f
cmp edx,4*$max
jg a1
cmp edx,-4*$max
jge load
;------------------------------------------------
dec edx
a1: not edx
sar edx,2
mov temp1,edx
imul edx,magic2
sar edx,16
mov temp2,edx
fild temp2         ; get the characteristic
fild temp1         ; ????? ????? log2(10)*expscale
fldl2t    ; log2(10)
        fmul               ; log2(10)*expscale
        fsub st,st(1)      ; get only the mantissa but keep the characteristic
        f2xm1              ; 2^(mantissa)-1
        fld1
        fadd               ; add 1 and pop
        fscale
        fstp    st(1)      ; remove the characteristic
        jmp multiply
load: fld tabs[edx+edx*2]
multiply:fmul              ; X * 10^expscaleS
fld st
fstp tbyte ptr x
xor edx,edx
@@: mov ecx,x+8
mov ebx,x
        mov eax,x+4
and ecx,7FFFh
sub ecx,16382
jmp table1[ecx*4]
shift4::test eax,60000000h
jz a6
fld tabs[12]
fmul
add iExp,4
fstp tbyte ptr x
mov eax,x+4
        mov ebx,x
shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
jmp shift0
a6: shld edx,eax,4 ;ecx=1 ebx+4
shld eax,ebx,4 ;ecx=2 ebx+8
shl ebx,4      ;ecx=3 ebx+9
add ebx,12     ;ecx=4 ebx+12
jmp shift0
shift3::shld edx,eax,3
shld eax,ebx,3
shl ebx,3
add ebx,9;10
jmp shift0
shift2::shld edx,eax,2
shld eax,ebx,2
shl ebx,2
add ebx,8
jmp shift0
shift1::shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
shift0::adc eax,0
adc edx,0
mov [edi+1],dl
mov byte ptr [edi+2],','
or byte ptr [edi+1],'0'
k=3
rept 17
mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
mov ebx,eax
mov eax,ecx
k=k+1
endm

mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
;--------------------------------------
mov eax,iExp
mov edx,eax
sar eax,2
sar edx,31
mov ecx,esi
xor eax,edx
sub eax,edx
and edx,2
add byte ptr [edi+22],dl
mov esi,eax
mov edx,42949673;2^32/100
mul edx
mov ax,word ptr table0[edx*2] ;edx = quotient of the division by 100
mov word ptr [edi+23],ax
lea eax,[edx*4]
shl edx,2
lea edx,[edx+edx*2]
lea edx,[eax+edx*8] ;edx = quotient*100
sub esi,edx ;esi = remainder of the division by 100
mov ax,word ptr table0[esi*2]
        mov word ptr [edi+25],ax
ret
Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty:
cmp eax,80000000h
jnz SNAN_Or_QNAN_Or_Uncertainty
and ebx,ebx
jnz SNAN_Or_QNAN_Or_Uncertainty
        mov dword ptr [edi+1],'ifnI'
        mov dword ptr [edi+5],'ytin'
        mov word ptr  [edi+9],0A0Dh
mov dword ptr [edi-4],11
ret
SNAN_Or_QNAN_Or_Uncertainty:
test eax,eax
jns error
test eax,40000000h
jnz QNAN
        mov dword ptr [edi+1],'ngiS'
        mov dword ptr [edi+5],'N la'
        mov dword ptr [edi+9],'a no'
        mov dword ptr [edi+13],'muN '
        mov dword ptr [edi+17],0D726562h;'reb'
        mov dword ptr [edi+21],0Ah
mov dword ptr [edi-4],22
ret
QNAN: test eax,3FFFFFFFh
jnz @f
test ebx,ebx
jnz @f
        mov dword ptr [edi+1],'ecnU'
        mov dword ptr [edi+5],'iatr'
        mov dword ptr [edi+9],0D79746Eh;'ytn'
        mov byte ptr [edi+13],0Ah
mov dword ptr [edi-4],14
ret
@@: mov dword ptr [edi+1],'eiuQ'
        mov dword ptr [edi+5],'oN t'
        mov dword ptr [edi+9],' a n'
        mov dword ptr [edi+13],'bmuN'
        mov dword ptr [edi+17],0A0D7265h;'re'
mov dword ptr [edi-4],21
ret
error: mov dword ptr [edi+1],'orrE'
        mov dword ptr [edi+5],0A0D72h;'r'
mov dword ptr [edi-4],8
ret
Eprst endp

        It doesnt convert anything

Mikl__

  • Member
  • ****
  • Posts: 555
Re: Converting a Real10 to string
« Reply #6 on: May 13, 2013, 11:17:25 AM »
repetição
Code: [Select]
; masm windows console #
      .686                      ; create 32 bit code
      .model flat, stdcall      ; 32 bit memory model
      option casemap :none      ; case sensitive

       include \masm32\include\windows.inc
       includelib \masm32\lib\user32.lib
       includelib \masm32\lib\kernel32.lib

extern _imp__ExitProcess@4:dword
extern _imp__GetStdHandle@4:dword;
extern _imp__WriteConsoleA@20:dword;
extern _imp__SetConsoleTitleA@4:dword;
extern _imp__SetConsoleScreenBufferSize@8:dword;
extern _imp__FreeConsole@0:dword;
extern _imp__AllocConsole@0:dword;
extern _imp__CharToOemA@8:dword
extern _imp__ReadConsoleA@20:dword
$max equ 60; <-- This number may be increased

magic1  equ 4D10h;lg(2)*65536=19728,3...
magic2  equ 35269h;log2(10)*65536=3,32192809488736234780*65536=217705,8796265381788254208..
.data
dt 1.0e$max
dw 0
irp k,<59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,\
       38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,\
       16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1>
dt 1.0e&k
dw 0
endm
tabs dt 1.0
dw 0
irp k,<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,\
       26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,\
       48,49,50,51,52,53,54,55,56,57,58,59,$max>
dt 1.0e-&k
dw 0
endm
table0 db "000102030405060708091011121314151617181920212223242526272829"
       db "303132333435363738394041424344454647484950515253545556575859"
       db "606162636465666768697071727374757677787980818283848586878889"
       db "90919293949596979899"
table1 dd shift0,shift1,shift2,shift3,shift4
string db 40 dup(0)
crd COORD <40,225>
HANDL  dd ?
HANDL1 dd ?
TITL db 0
STR1 db 'Prima qualquer tecla para continuar...';'Press Enter to continue'
LENS db ?
xxx db 0,0,0,0,0,0,0,80h,0,0;=3.3621031431120935E-4932
db 8 dup(0FFh),0,0;6.724206286224187E-4932
db 1,9 dup(0); =ERROR
dd 0,80000000h
dw 7FFFh      ;  Infinity
dd 0,80000000h
dw 0FFFFh     ; -Infinity
dd 0,0C0000000h
dw 0FFFFh; -Uncertainty
dd 0,0C0000000h
dw 7FFFh;  Uncertainty
dd 0,90000000h
dw 7FFFh;  Signal Non a Number
dd 0,90000000h
dw 0FFFFh; -Signal Non a Number
dd 0,0D0000000h
dw 0FFFFh; -Quiet Non a Number
dd 0,0D0000000h
dw 7FFFh;  Quiet Non a Number
dd 0,70000000h
dw 07FFFh;  Error
dd 0,70000000h
dw 0FFFFh; -Error
dd 0FFFFFFFFh,0FFFFFFFFh
dw 07FFEh; = 1.18973149535723177E+4932
dd 0FFFFFFFFh,0FFFFFFFFh
dw 0FFFEh; =-1.18973149535723177E+4932
dt -0.0
dt 0
dt 1.0
dt 2.0
dt 3.0
dt 4.0
dt 5.0
dt 6.0
dt 7.0
dt 8.0
dt 9.0
dt 0.1
dt 0.2
dt 0.3
dt 0.4
dt 0.5
dt 0.6
dt 0.7
dt 0.8
dt 0.9
dt -1.1
dt -11.1
dt -111.1
dt -1111.1
dt -11111.1 
dt -111111.1
dt -1111111.1
dt -11111111.1
dt -111111111.1
dt -1111111111.1
dt -11111111111.1
dt -111111111111.1
dt -1111111111111.1
dt -11111111111111.1
dt -111111111111111.1
dt -1111111111111111.1
dt -11111111111111111.1
dt -111111111111111111.1
dt -1111111111111111111.1
dt -2.2
dt -22.2
dt -222.2
dt -2222.2
dt -22222.2
dt -222222.2
dt -2222222.2
dt -22222222.2
dt -222222222.2
dt -2222222222.2
dt -22222222222.2
dt -222222222222.2
dt -2222222222222.2
dt -22222222222222.2
dt -222222222222222.2
dt -2222222222222222.2
dt -22222222222222222.2
dt -222222222222222222.2
dt -2222222222222222222.2
dt -3.3
dt -33.3
dt -333.3
dt -3333.3
dt -33333.3
dt -333333.3
dt -3333333.3
dt -33333333.3
dt -333333333.3
dt -3333333333.3
dt -33333333333.3
dt -333333333333.3
dt -3333333333333.3
dt -33333333333333.3
dt -333333333333333.3
dt -3333333333333333.3
dt -33333333333333333.3
dt -333333333333333333.3
dt -3333333333333333333.3
dt -4.4
dt -44.4
dt -444.4
dt -4444.4
dt -44444.4
dt -444444.4
dt -4444444.4
dt -44444444.4
dt -444444444.4
dt -4444444444.4
dt -44444444444.4
dt -444444444444.4
dt -4444444444444.4
dt -44444444444444.4
dt -444444444444444.4
dt -4444444444444444.4
dt -44444444444444444.4
dt -444444444444444444.4
dt -4444444444444444444.4
dt -5.5
dt -55.5
dt -555.5
dt -5555.5
dt -55555.5
dt -555555.5
dt -5555555.5
dt -55555555.5
dt -555555555.5
dt -5555555555.5
dt -55555555555.5
dt -555555555555.5
dt -5555555555555.5
dt -55555555555555.5
dt -555555555555555.5
dt -5555555555555555.5
dt -55555555555555555.5
dt -555555555555555555.5
dt -5555555555555555555.5
dt -6.6
dt -66.6
dt -666.6
dt -6666.6
dt -66666.6
dt -666666.6
dt -6666666.6
dt -66666666.6
dt -666666666.6
dt -6666666666.6
dt -66666666666.6
dt -666666666666.6
dt -6666666666666.6
dt -66666666666666.6
dt -666666666666666.6
dt -6666666666666666.6
dt -66666666666666666.6
dt -666666666666666666.6
dt -6666666666666666666.6
dt -7.7
dt -77.7
dt -777.7
dt -7777.7
dt -77777.7
dt -777777.7
dt -7777777.7
dt -77777777.7
dt -777777777.7
dt -7777777777.7
dt -77777777777.7
dt -777777777777.7
dt -7777777777777.7
dt -77777777777777.7 ;ecx=3
dt -777777777777777.7
dt -7777777777777777.7
dt -77777777777777777.7
dt -777777777777777777.7
dt -7777777777777777777.7
dt -8.8
dt -88.8
dt -888.8
dt -8888.8
dt -88888.8
dt -888888.8
dt -8888888.8;ecx=4
dt -88888888.8
dt -888888888.8
dt -8888888888.8
dt -88888888888.8
dt -888888888888.8
dt -8888888888888.8
dt -88888888888888.8
dt -888888888888888.8
dt -8888888888888888.8
dt -88888888888888888.8
dt -888888888888888888.8
dt -8888888888888888888.8
dt -9.9
dt -99.9;the result of multiplying is -9.9900000000000000010
dt -999.9
dt -9999.9
dt -99999.9
dt -999999.9
dt -9999999.9
dt -99999999.9
dt -999999999.9;-9.9999999990000000010
dt -9999999999.9;ecx=4
dt -99999999999.9
dt -999999999999.9
dt -9999999999999.9;the result of multiplying is -9.9999999999998999990
dt -99999999999999.9
dt -999999999999999.9
dt -9999999999999999.9
dt -99999999999999999.9
dt -999999999999999999.9
dt -9999999999999999999.9
dt  0.1
dt  0.01
dt  0.001
dt  0.0001
dt  0.00001
dt  0.000001
dt  0.0000001
dt  0.00000001
dt  0.000000001
dt  0.0000000001
dt  0.00000000001
dt  0.000000000001
dt  0.0000000000001
dt  0.00000000000001
dt  0.000000000000001
dt  0.0000000000000001
dt  0.00000000000000001
dt  1.0E-18
dt  0.2
dt  0.02
dt  0.002
dt  0.0002
dt  0.00002
dt  0.000002
dt  0.0000002
dt  0.00000002
dt  0.000000002
dt  0.0000000002
dt  0.00000000002
dt  0.000000000002
dt  0.0000000000002
dt  0.00000000000002
dt  0.000000000000002
dt  0.0000000000000002
dt  0.00000000000000002
dt  2.0E-18
dt  0.3
dt  0.03
dt  0.003
dt  0.0003
dt  0.00003
dt  0.000003
dt  0.0000003
dt  0.00000003
dt  0.000000003
dt  0.0000000003
dt  0.00000000003
dt  0.000000000003
dt  0.0000000000003
dt  0.00000000000003
dt  0.000000000000003
dt  0.0000000000000003
dt  0.00000000000000003
dt  3.0E-18
dt  0.4
dt  0.04
dt  0.004
dt  0.0004
dt  0.00004
dt  0.000004
dt  0.0000004
dt  0.00000004
dt  0.000000004
dt  0.0000000004
dt  0.00000000004
dt  0.000000000004
dt  0.0000000000004
dt  0.00000000000004
dt  0.000000000000004
dt  0.0000000000000004
dt  0.00000000000000004
dt  4.0E-18
dt  0.5
dt  0.05
dt  0.005
dt  0.0005
dt  0.00005
dt  0.000005
dt  0.0000005
dt  0.00000005
dt  0.000000005
dt  0.0000000005
dt  0.00000000005
dt  0.000000000005
dt  0.0000000000005
dt  0.00000000000005
dt  0.000000000000005
dt  0.0000000000000005
dt  0.00000000000000005
dt  5.0E-18
dt  0.6
dt  0.06
dt  0.006
dt  0.0006
dt  0.00006
dt  0.000006
dt  0.0000006
dt  0.00000006
dt  0.000000006
dt  0.0000000006
dt  0.00000000006
dt  0.000000000006
dt  0.0000000000006
dt  0.00000000000006
dt  0.000000000000006
dt  0.0000000000000006
dt  0.00000000000000006
dt  6.0E-18
dt  0.7
dt  0.07
dt  0.007
dt  0.0007
dt  0.00007
dt  0.000007
dt  0.0000007
dt  0.00000007
dt  0.000000007
dt  0.0000000007
dt  0.00000000007
dt  0.000000000007
dt  0.0000000000007
dt  0.00000000000007
dt  0.000000000000007
dt  0.0000000000000007
dt  0.00000000000000007
dt  7.0E-18
dt  0.8
dt  0.08
dt  0.008
dt  0.0008
dt  0.00008
dt  0.000008
dt  0.0000008
dt  0.00000008
dt  0.000000008
dt  0.0000000008
dt  0.00000000008
dt  0.000000000008
dt  0.0000000000008
dt  0.00000000000008
dt  0.000000000000008
dt  0.0000000000000008
dt  0.00000000000000008
dt  8.0E-18
dt  0.9
dt  0.09
dt  0.009
dt  0.0009
dt  0.00009
dt  0.000009
dt  0.0000009
dt  0.00000009
dt  0.000000009
dt  0.0000000009
dt  0.00000000009
dt  0.000000000009
dt  0.0000000000009
dt  0.00000000000009
dt  0.000000000000009
dt  0.0000000000000009
dt  0.00000000000000009
dt  9.0E-18
dt -1.23456789123456
dt -12.3456789123456
dt -123.456789123456
dt -1234.56789123456
dt -12345.6789123456
dt -123456.789123456
dt -1234567.89123456
dt -12345678.9123456
dt -12345678.9123456
dt -123456789.123456
dt -1234567891.123456
dt -12345678912.123456
dt -123456789123.123456
dt -1234567891234.123456
dt -12345678912345.12345
dt -123456789123456.1234
dt -1234567891234567.123
dt -12345678912345678.12
dt -123456789123456789.1
dt -1234567891234567891.0
num = ($ - xxx)/10
.code
start: call _imp__FreeConsole@0;free the existing console
        call _imp__AllocConsole@0;we form a console
        push STD_INPUT_HANDLE
        call _imp__GetStdHandle@4;HANDL1 obtain input
        mov HANDL1,eax
        push STD_OUTPUT_HANDLE
        call _imp__GetStdHandle@4;HANDL get to output
        mov HANDL,eax
push crd
push eax
call _imp__SetConsoleScreenBufferSize@8
        push offset TITL
        call _imp__SetConsoleTitleA@4;definition window title
mov ebx,offset xxx
mov ecx,num/24
a11: push ecx
mov ecx,24
@@: push ecx
push offset string
push ebx
call Eprst
        push 0
        push offset LENS
        push dword ptr string
        push offset string+4
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
add ebx,10
pop ecx
loop @b
push 0
        push offset LENS
        push 38
        push offset STR1
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
push 0
        push offset LENS
        push 200
        push offset string
        push HANDL1
        call _imp__ReadConsoleA@20;waiting to enter a string of characters
pop ecx
loop a11
mov ecx,(num mod 24)
jecxz a12
@@: push ecx
push offset string
push ebx
call Eprst
        push 0
        push offset LENS
        push dword ptr string
        push offset string+4
        push HANDL
        call _imp__WriteConsoleA@20;we derive a string of characters
add ebx,10
pop ecx
loop @b

a12:    push 0
        push offset LENS
        push 200
        push offset string
        push HANDL1
        call _imp__ReadConsoleA@20;waiting to enter a string of characters
push 0
call _imp__ExitProcess@4
;===================================================
Eprst proc uses edi esi ebx lpReal10:dword,lpDest:dword
local iExp:dword
local x[3]:dword
local temp1:dword
local temp2:dword
fninit
        mov ecx,lpReal10
fld tbyte ptr [ecx]
mov edi,lpDest
mov ebx,[ecx]
add edi,4
mov eax,[ecx+4]
mov [x],ebx
movzx edx,word ptr [ecx+8]
mov [x+4],eax
mov [x+8],edx
cmp edx,8000h
mov byte ptr [edi],'-'
sbb esi,esi
and esi,0Dh
sub [edi],esi
and edx,7FFFh
jnz a2
Zero_Or_Denormal: cmp edx,eax
jnz Denormal
        cmp ebx,eax
jnz Denormal
mov dword ptr [edi+1],'oreZ'
mov word ptr  [edi+5],0A0Dh
mov dword ptr [edi-4],7
        ret
a2: cmp edx,7FFFh
jz Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty
Denormal:mov esi,10
sub edx,16383
mov word ptr [edi+21],'+E'
imul edx,magic1
mov word ptr [edi+27],0A0Dh
and edx,0FFFF0000h
mov dword ptr [edi-4],29
sar edx,14
mov iExp,edx
jz @f
cmp edx,4*$max
jg a1
cmp edx,-4*$max
jge load
;------------------------------------------------
dec edx
a1: not edx
sar edx,2
mov temp1,edx
imul edx,magic2
sar edx,16
mov temp2,edx
fild temp2         ; get the characteristic
fild temp1         ; ????? ????? log2(10)*expscale
fldl2t    ; log2(10)
        fmul               ; log2(10)*expscale
        fsub st,st(1)      ; get only the mantissa but keep the characteristic
        f2xm1              ; 2^(mantissa)-1
        fld1
        fadd               ; add 1 and pop
        fscale
        fstp    st(1)      ; remove the characteristic
        jmp multiply
load: fld tabs[edx+edx*2]
multiply:fmul              ; X * 10^expscaleS
fld st
fstp tbyte ptr x
xor edx,edx
@@: mov ecx,x+8
mov ebx,x
        mov eax,x+4
and ecx,7FFFh
sub ecx,16382
cmp ecx,4
ja shift0
jmp table1[ecx*4]
shift4::test eax,60000000h
jz a6
fld tabs[12]
fmul
add iExp,4
fstp tbyte ptr x
mov eax,x+4
        mov ebx,x
shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
jmp shift0
a6: shld edx,eax,4 ;ecx=1 ebx+4
shld eax,ebx,4 ;ecx=2 ebx+8
shl ebx,4      ;ecx=3 ebx+9
add ebx,12     ;ecx=4 ebx+12
jmp shift0
shift3::shld edx,eax,3
shld eax,ebx,3
shl ebx,3
add ebx,9;10
jmp shift0
shift2::shld edx,eax,2
shld eax,ebx,2
shl ebx,2
add ebx,8
jmp shift0
shift1::shld edx,eax,1
shld eax,ebx,1
shl ebx,1
add ebx,4
shift0::adc eax,0
adc edx,0
mov [edi+1],dl
mov byte ptr [edi+2],','
or byte ptr [edi+1],'0'
k=3
rept 17
mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
mov ebx,eax
mov eax,ecx
k=k+1
endm

mul esi
mov [edi+k],dl
mov ecx,eax
mov eax,ebx
mul esi
add ecx,edx
adc byte ptr [edi+k],'0'
;--------------------------------------
mov eax,iExp
mov edx,eax
sar eax,2
sar edx,31
xor eax,edx
sub eax,edx
and edx,2
add byte ptr [edi+22],dl
mov esi,eax
mov edx,42949673;2^32/100
mul edx
mov ax,word ptr table0[edx*2] ;edx = quotient of the division by 100
mov word ptr [edi+23],ax
lea eax,[edx*4]
shl edx,2
lea edx,[edx+edx*2]
lea edx,[eax+edx*8] ;edx = quotient*100
sub esi,edx ;esi = remainder of the division by 100
mov ax,word ptr table0[esi*2]
        mov word ptr [edi+25],ax
ret
Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty:
cmp eax,80000000h
jnz SNAN_Or_QNAN_Or_Uncertainty
and ebx,ebx
jnz SNAN_Or_QNAN_Or_Uncertainty
        mov dword ptr [edi+1],'ifnI'
        mov dword ptr [edi+5],'ytin'
        mov word ptr  [edi+9],0A0Dh
mov dword ptr [edi-4],11
ret
SNAN_Or_QNAN_Or_Uncertainty:
test eax,eax
jns error
test eax,40000000h
jnz QNAN
        mov dword ptr [edi+1],'ngiS'
        mov dword ptr [edi+5],'N la'
        mov dword ptr [edi+9],'a no'
        mov dword ptr [edi+13],'muN '
        mov dword ptr [edi+17],0D726562h;'reb'
        mov dword ptr [edi+21],0Ah
mov dword ptr [edi-4],22
ret
QNAN: test eax,3FFFFFFFh
jnz @f
test ebx,ebx
jnz @f
        mov dword ptr [edi+1],'ecnU'
        mov dword ptr [edi+5],'iatr'
        mov dword ptr [edi+9],0D79746Eh;'ytn'
        mov byte ptr [edi+13],0Ah
mov dword ptr [edi-4],14
ret
@@: mov dword ptr [edi+1],'eiuQ'
        mov dword ptr [edi+5],'oN t'
        mov dword ptr [edi+9],' a n'
        mov dword ptr [edi+13],'bmuN'
        mov dword ptr [edi+17],0A0D7265h;'re'
mov dword ptr [edi-4],21
ret
error: mov dword ptr [edi+1],'orrE'
        mov dword ptr [edi+5],0A0D72h;'r'
mov dword ptr [edi-4],8
ret
Eprst endp
end start
« Last Edit: May 13, 2013, 12:19:05 PM by Mikl__ »

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #7 on: May 14, 2013, 12:20:54 AM »
Mikl___,
             Now it assembles and it works but it doesnt end
             i dont know why, but no problems.
             I read your code. Now the question is this: if i try to use
             your Eprst to convert only one real10 number it doesnt do
             it correctly, it shows only rubbish. So, please write your
             code in such a way that we may do this, for instance
              (and post it) :

            fldpi
            fstp    tbyte ptr _Real10_R
            invoke  Eprst, addr _Real10_R, addr _RclBuf
            print   addr _RclBuf, 13, 10           
            inkey " *** STOP - Eprst --- E N D ---*** "

             
         

Mikl__

  • Member
  • ****
  • Posts: 555
Re: Converting a Real10 to string
« Reply #8 on: May 14, 2013, 12:41:46 AM »
Olá, RuiLoureiro!
Claro, eu vou fazer isso amanhã...

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #9 on: May 14, 2013, 01:30:09 AM »
Olá, RuiLoureiro!
Claro, eu vou fazer isso amanhã...
:biggrin:
              Olá Mikl__ !
               See what i wrote about magic numbers.
 :icon14:
Rui

qWord

  • Member
  • *****
  • Posts: 1471
  • The base type of a type is the type itself
    • SmplMath macros
Re: Converting a Real10 to string
« Reply #10 on: May 14, 2013, 01:53:33 AM »
if i try to use
             your Eprst to convert only one real10 number it doesnt do
             it correctly, it shows only rubbish
It somehow work for some numbers after I've cleaned up the code a bit:
Code: [Select]
include \masm32\include\masm32rt.inc
.686
.mmx
.xmm

    EPRST struct
        cc DWORD ?
        string BYTE 32 dup (?) ; ends with CR,LF
    EPRST ends

    Eprst proto lpReal10:ptr REAL10,lpDest:ptr EPRST

.const
    foo REAL10 3.14159265358979323846264
    ;foo REAL10 3.14159265358979323846264E61 ; crash --> requires: $max >= 61
.code
main proc
LOCAL result:EPRST
   
    invoke Eprst,OFFSET foo,ADDR result
    mov eax,result.cc
    mov result.string[eax],0
    print ADDR result.string
   
    inkey
    exit
   
main endp



    $max equ 60; <-- This number may be increased
    magic1 equ 4D10h;lg(2)*65536=19728,3...
    magic2  equ 35269h;log2(10)*65536=3,32192809488736234780*65536=217705,8796265381788254208..
.const
    cntr = -$max
    REPEAT 2*$max+1
        IF cntr NE 0
            REAL10 @CatStr(<1.0e>,%-cntr)
        ELSE
            tabs REAL10 1.0
        ENDIF
        WORD 0
        cntr = cntr + 1
    ENDM

    table0 db "000102030405060708091011121314151617181920212223242526272829"
            db "303132333435363738394041424344454647484950515253545556575859"
            db "606162636465666768697071727374757677787980818283848586878889"
            db "90919293949596979899"
   
    table1 dd shift0,shift1,shift2,shift3,shift4
.code
Eprst proc uses edi esi ebx lpReal10:ptr REAL10,lpDest:ptr EPRST
local iExp:dword
local x[3]:dword
local temp1:dword
local temp2:dword
    fninit
        mov ecx,lpReal10
    fld tbyte ptr [ecx]
    mov edi,lpDest
    mov ebx,[ecx]
    add edi,4
    mov eax,[ecx+4]
    mov [x],ebx
    movzx edx,word ptr [ecx+8]
    mov [x+4],eax
    mov [x+8],edx
    cmp edx,8000h
    mov byte ptr [edi],'-'
    sbb esi,esi
    and esi,0Dh
    sub [edi],esi
    and edx,7FFFh
    jnz a2
Zero_Or_Denormal: cmp edx,eax
    jnz Denormal
        cmp ebx,eax
    jnz Denormal
    mov dword ptr [edi+1],'oreZ'
    mov word ptr  [edi+5],0A0Dh
    mov dword ptr [edi-4],7
        ret
a2: cmp edx,7FFFh
    jz Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty
Denormal:mov esi,10
    sub edx,16383
    mov word ptr [edi+21],'+E'
    imul edx,magic1
    mov word ptr [edi+27],0A0Dh
    and edx,0FFFF0000h
    mov dword ptr [edi-4],29
    sar edx,14
    mov iExp,edx
    jz @f
    cmp edx,4*$max
    jg a1
    cmp edx,-4*$max
    jge load
;------------------------------------------------
    dec edx
a1: not edx
    sar edx,2
    mov temp1,edx
    imul edx,magic2
    sar edx,16
    mov temp2,edx
    fild temp2         ; get the characteristic
    fild temp1         ; ????? ????? log2(10)*expscale
    fldl2t    ; log2(10)
        fmul               ; log2(10)*expscale
        fsub st,st(1)      ; get only the mantissa but keep the characteristic
        f2xm1              ; 2^(mantissa)-1
        fld1
        fadd               ; add 1 and pop
        fscale
        fstp    st(1)      ; remove the characteristic
        jmp multiply
load: fld tabs[edx+edx*2]
multiply:fmul              ; X * 10^expscaleS
    fld st
    fstp tbyte ptr x
    xor edx,edx
@@: mov ecx,x+8
    mov ebx,x
        mov eax,x+4
    and ecx,7FFFh
    sub ecx,16382
    jmp table1[ecx*4]
shift4::test eax,60000000h
    jz a6
    fld tabs[12]
    fmul
    add iExp,4
    fstp tbyte ptr x
    mov eax,x+4
        mov ebx,x
    shld edx,eax,1
    shld eax,ebx,1
    shl ebx,1
    add ebx,4
    jmp shift0
a6: shld edx,eax,4 ;ecx=1 ebx+4
    shld eax,ebx,4 ;ecx=2 ebx+8
    shl ebx,4      ;ecx=3 ebx+9
    add ebx,12     ;ecx=4 ebx+12
    jmp shift0
shift3::shld edx,eax,3
    shld eax,ebx,3
    shl ebx,3
    add ebx,9;10
    jmp shift0
shift2::shld edx,eax,2
    shld eax,ebx,2
    shl ebx,2
    add ebx,8
    jmp shift0
shift1::shld edx,eax,1
    shld eax,ebx,1
    shl ebx,1
    add ebx,4
shift0::adc eax,0
    adc edx,0
    mov [edi+1],dl
    mov byte ptr [edi+2],','
    or byte ptr [edi+1],'0'
   
    k=3
    REPEAT 17
        mul esi
        mov [edi+k],dl
        mov ecx,eax
        mov eax,ebx
        mul esi
        add ecx,edx
        adc byte ptr [edi+k],'0'
        mov ebx,eax
        mov eax,ecx
    k=k+1
    ENDM

    mul esi
    mov [edi+k],dl
    mov ecx,eax
    mov eax,ebx
    mul esi
    add ecx,edx
    adc byte ptr [edi+k],'0'
;--------------------------------------
    mov eax,iExp
    mov edx,eax
    sar eax,2
    sar edx,31
    mov ecx,esi
    xor eax,edx
    sub eax,edx
    and edx,2
    add byte ptr [edi+22],dl
    mov esi,eax
    mov edx,42949673;2^32/100
    mul edx
    mov ax,word ptr table0[edx*2] ;edx = quotient of the division by 100
    mov word ptr [edi+23],ax
    lea eax,[edx*4]
    shl edx,2
    lea edx,[edx+edx*2]
    lea edx,[eax+edx*8] ;edx = quotient*100
    sub esi,edx ;esi = remainder of the division by 100
    mov ax,word ptr table0[esi*2]
        mov word ptr [edi+25],ax
    ret
Infinity_Or_SNAN_Or_QNAN_Or_Uncertainty:
    cmp eax,80000000h
    jnz SNAN_Or_QNAN_Or_Uncertainty
    and ebx,ebx
    jnz SNAN_Or_QNAN_Or_Uncertainty
        mov dword ptr [edi+1],'ifnI'
        mov dword ptr [edi+5],'ytin'
        mov word ptr  [edi+9],0A0Dh
    mov dword ptr [edi-4],11
    ret
SNAN_Or_QNAN_Or_Uncertainty:
    test eax,eax
    jns error
    test eax,40000000h
    jnz QNAN
        mov dword ptr [edi+1],'ngiS'
        mov dword ptr [edi+5],'N la'
        mov dword ptr [edi+9],'a no'
        mov dword ptr [edi+13],'muN '
        mov dword ptr [edi+17],0D726562h;'reb'
        mov dword ptr [edi+21],0Ah
    mov dword ptr [edi-4],22
    ret
QNAN: test eax,3FFFFFFFh
    jnz @f
    test ebx,ebx
    jnz @f
        mov dword ptr [edi+1],'ecnU'
        mov dword ptr [edi+5],'iatr'
        mov dword ptr [edi+9],0D79746Eh;'ytn'
        mov byte ptr [edi+13],0Ah
    mov dword ptr [edi-4],14
    ret
@@: mov dword ptr [edi+1],'eiuQ'
        mov dword ptr [edi+5],'oN t'
        mov dword ptr [edi+9],' a n'
        mov dword ptr [edi+13],'bmuN'
        mov dword ptr [edi+17],0A0D7265h;'re'
    mov dword ptr [edi-4],21
    ret
error: mov dword ptr [edi+1],'orrE'
        mov dword ptr [edi+5],0A0D72h;'r'
    mov dword ptr [edi-4],8
    ret
Eprst endp
end main

BTW: the errors for the hexadecimal FP values comes up through an bug in MASM - it is fixed for newer versions (or jWasm)
MREAL macros - when you need floating point arithmetic while assembling!

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #11 on: May 14, 2013, 03:31:57 AM »
qWord,
       now i understood what he did

Quote
       
.data
                dd 0
_RclBuf      db 40 dup (0)
_real10_R  dt 1.2345678901234567E+500

.code
        mov      esi, offset _Rclbuffer           
        invoke   Eprst, OFFSET _real10_R, esi
        mov      eax, [esi]       
        mov      byte ptr [esi+4+eax], 0
        add      esi, 4
        print    esi

        In this case the program crashes
        and it doesnt give the correct results
        in other cases

Mikl__,
       we need the user's manual  ;)
       you need to say something about how to
       use it.

        . all my converters works with null
          terminated strings;
         
        . the address of the the first byte is
          the address of the buffer.

      So if we do
.data
                dd 0
_StringBuffer   db 40 dup (0)

      we only need to do

            invoke  ConvertXXX, addr _real10_R, addr _StringBuffer
            print   addr _StringBuffer

       If we want to know the length we do this:

            mov     ecx, [_StringBuffer-4]   

qWord

  • Member
  • *****
  • Posts: 1471
  • The base type of a type is the type itself
    • SmplMath macros
Re: Converting a Real10 to string
« Reply #12 on: May 14, 2013, 04:30:53 AM »
       If we want to know the length we do this:

            mov     ecx, [_StringBuffer-4]
... which is a very unusual solution  :eusa_snooty:
MREAL macros - when you need floating point arithmetic while assembling!

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: Converting a Real10 to string
« Reply #13 on: May 14, 2013, 04:38:30 AM »
       If we want to know the length we do this:

            mov     ecx, [_StringBuffer-4]
... which is a very unusual solution  :eusa_snooty:
               I know this since i am here, in this forum,
               ask MichaelW (and Paul i think) what i said to him in those days !
               It´s a very old story.

               In TASM we have the values of each member,
               in MASM not.

               My solution is the address is the address not
               1 byte forward like PASCAL, for instance.
               more: if the length is 20 we have 0 in
                         [_StringBuffer+20]
              Unusual is also variables _x, _exit, _all...kind ...
              but i have not problems with names _eax, _esp
              _print, etc. all names. The assembler doesnt stop
              because we are using a reserved word.

dedndave

  • Member
  • *****
  • Posts: 8751
  • Still using Abacus 2.0
    • DednDave
Re: Converting a Real10 to string
« Reply #14 on: May 14, 2013, 05:14:21 AM »
if you look at my EvalReal function, i use a buffer that has 4 extra bytes at the beginning
i process numbers at Buffer+4
then, when i want to add a sign and a decimal point, i have room to work   :P