Author Topic: printf function  (Read 9477 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2787
printf function
« 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

cman

  • Member
  • **
  • Posts: 223
Re: printf function
« Reply #1 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: ).

Gunther

  • Member
  • *****
  • Posts: 4196
  • Forgive your enemies, but never forget their names
Re: printf function
« Reply #2 on: December 03, 2016, 04:13:06 AM »
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

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: printf function
« Reply #3 on: December 03, 2016, 07:47:20 AM »
deleted
« Last Edit: February 25, 2022, 10:37:59 PM by nidud »

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2787
Re: printf function
« Reply #4 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