The MASM Forum

Projects => Poasm => Topic started by: Vortex on December 01, 2016, 05:56:33 AM

Title: printf function
Post by: Vortex on December 01, 2016, 05:56:33 AM
Here is a printf function designed for Poasm and Masm :

Code: [Select]
.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 :

Code: [Select]
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
Title: Re: printf function
Post by: cman on December 02, 2016, 11:39:56 AM
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: ).
Title: Re: printf function
Post by: Gunther on December 03, 2016, 04:13:06 AM
Hi Vortex,

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

Gunther
Title: Re: printf function
Post by: nidud on December 03, 2016, 07:47:20 AM
deleted
Title: Re: printf function
Post by: Vortex on May 07, 2017, 08:36:22 PM
Here is the Unicode version :

Code: [Select]
.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