News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

crt_sprintf

Started by Mikl__, October 23, 2013, 07:30:15 PM

Previous topic - Next topic

Mikl__

; masm windows gui #
.686
.model flat
.XMM
include \masm32\include\windows.inc
include \masm32\include\user32.inc       
includelib \masm32\lib\user32.lib
include \masm32\include\msvcrt.inc    ; for crt_printf\crt_scanf
includelib \masm32\lib\msvcrt.lib
.data
x dd 2.1
y dq 1.1
z dt 6.3
format db "%.2g ",0
buffer db 90 dup(0)
.code
_Start: invoke crt_sprintf, ADDR buffer, addr format, [y],[y+4]
invoke MessageBox,0, ADDR buffer, 0,0
ret
end _Start
how to use a function of crt_sprintf display REAL4 and REAL10, maybe something artful specifiers?

jj2007

Check real4$() in \Masm32\macros\macros.asm.

Mikl__

And "Check real10$() in \Masm32\macros\macros.asm"?

jj2007

Yes indeed, but when studying that macro you will realise that real10$() gives you only REAL8 precision. For true REAL10 precision, you need a library that supports this format:

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        fldpi        ; put PI on the FPU
        Inkey Str$("PI=%Ji", ST(0))        ; arg can be ST(x) or a REAL10 variable
        Exit
end start


Output: PI=3.141592653589793238

Mikl__

jj2007,    real4$ MACRO r4value:req
        LOCAL buffer, r8value, r4tmp
        .data?
            r4tmp   REAL4 ?
            r8value REAL8 ?
            buffer  BYTE  48 dup(?)
        IFNDEF r8fmt   
        .data
            r8fmt   BYTE "%lf", 0
        ENDIF   
        .code
            IFE issize(r4value, 4)
                echo ------------------------
                echo real4$ - requires REAL4
                echo ------------------------
                .ERR
            ENDIF           
            IF isregister(r4value)
                push   r4value
                pop    r4tmp
                finit
                fld    r4tmp
            ELSE
                finit
                fld    r4value <--
            ENDIF   
            fstp   r8value <--
            fwait
            mov    buffer[0], 0
            invoke crt_sprintf, ADDR buffer, ADDR r8fmt, r8value <--
            EXITM <OFFSET buffer>
    ENDM
This is a scam and not a solution!
It seems that crt_sprintf is ONLY for REAL8

japheth

Quote from: Mikl__ on October 24, 2013, 04:30:37 PM
It seems that crt_sprintf is ONLY for REAL8

Yes - since all floats will be automatically transformed to double in C if the parameter is "vararg".