Projects > Poasm

printf function

(1/1)

Vortex:
Here is a printf function designed for Poasm and Masm :


--- Code: ---.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
--- End code ---

An example :


--- Code: ---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
--- End code ---

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

nidud:
Here's an extended one with line-feed:

http://masm32.com/board/index.php?topic=5247.msg56233#msg56233

Needs a unicode fix..

Vortex:
Here is the Unicode version :


--- Code: ---.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
--- End code ---

Navigation

[0] Message Index

Go to full version