The MASM Forum

General => The Campus => Topic started by: Mikl__ on October 23, 2013, 07:30:15 PM

Title: crt_sprintf
Post by: Mikl__ on October 23, 2013, 07:30:15 PM
; 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?
Title: Re: crt_sprintf
Post by: jj2007 on October 23, 2013, 08:45:20 PM
Check real4$() in \Masm32\macros\macros.asm.
Title: Re: crt_sprintf
Post by: Mikl__ on October 23, 2013, 08:47:25 PM
And "Check real10$() in \Masm32\macros\macros.asm"?
Title: Re: crt_sprintf
Post by: jj2007 on October 23, 2013, 11:10:11 PM
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 (http://masm32.com/board/index.php?topic=94.0)
        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
Title: Re: crt_sprintf
Post by: Mikl__ on October 24, 2013, 04:30:37 PM
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
Title: Re: crt_sprintf
Post by: japheth on October 24, 2013, 10:18:19 PM
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".