News:

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

Main Menu

printf function

Started by Vortex, December 01, 2016, 05:56:33 AM

Previous topic - Next topic

Vortex

Here is a printf function designed for Poasm and Masm :

.386
.model flat,stdcall
option casemap:none

include     printf.inc

.data?

buffer      db 512 dup(?)
_esi        dd ?

.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

printf PROC C _format:DWORD,args:VARARG

    mov     _esi,esi
    mov     esi,DWORD PTR [esp]
    mov     DWORD PTR [esp],OFFSET buffer
    call    wsprintf

    invoke  lstrlen,ADDR buffer

    push    NULL
    mov     ecx,esp
    push    NULL
    push    ecx     ; ecx -> lpNumberOfBytesWritten
    push    eax   
    push    OFFSET buffer
   
    invoke  GetStdHandle,STD_OUTPUT_HANDLE
    push    eax
    call    WriteFile
    mov     eax,DWORD PTR [esp]
    pop     ecx

    mov     DWORD PTR [esp],esi
    mov     esi,_esi

    IFDEF   __POASM__

        retn

    ELSE

        ret

    ENDIF

printf ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END


An example :

include     Test.inc

printf      PROTO C _format:DWORD,args:VARARG

.data

f1          db '%s %s',0
str1        db 'This is',0
str2        db 'a test.',0

.code

start:

    invoke  printf,ADDR f1,ADDR str1,ADDR str2
    invoke  ExitProcess,0

END start


cman

Nice work , Vortex :t I'll have to use this  (  for a minute I thought you rewrote the whole thing in assembly language - I was looking for many lines of code when I opened the archive  :biggrin: ).

Gunther

Hi Vortex,

good solution and a nice work. Thank you for that.  :t

Gunther
You have to know the facts before you can distort them.

nidud

#3
deleted

Vortex

Here is the Unicode version :

.386
.model flat,stdcall
option casemap:none

include     wprintf.inc

.data?

buffer      db 512 dup(?)
_esi        dd ?

.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

wprintf PROC C _format:DWORD,args:VARARG

    mov     _esi,esi
    mov     esi,DWORD PTR [esp]
    mov     DWORD PTR [esp],OFFSET buffer
    call    wsprintf

    invoke  lstrlen,ADDR buffer

    push    NULL
    mov     ecx,esp
    push    NULL
    push    ecx     ; ecx -> lpNumberOfBytesWritten
    push    eax   
    push    OFFSET buffer
   
    invoke  GetStdHandle,STD_OUTPUT_HANDLE
    push    eax
    call    WriteConsole
    mov     eax,DWORD PTR [esp]
    pop     ecx

    mov     DWORD PTR [esp],esi
    mov     esi,_esi

    IFDEF   __POASM__

        retn

    ELSE

        ret

    ENDIF

wprintf ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END