NO

Author Topic: printf implementation  (Read 3247 times)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
printf implementation
« on: February 04, 2024, 11:32:25 AM »
Here is a printf implementation similar to the inlined version of the latest MS VC releases :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

ExitProcess PROTO :DWORD
__p__iob PROTO C
vfprintf PROTO C :DWORD,:DWORD,:VARARG
printf   PROTO C :DWORD,:VARARG

includelib  kernel32.lib
includelib  msvcrt.lib

_iobuf STRUCT

    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?

_iobuf ENDS

FILE TYPEDEF _iobuf

.data

pi      real8 3.141592
format  db '%s is nearly equal to %f',0
str1    db 'Pi',0

.code

start:

    invoke  printf,ADDR format,ADDR str1,pi
           
    invoke  ExitProcess,0

printf PROC C _format:DWORD,args:VARARG

    invoke  __p__iob

;   #define stdout (&__iob_func()[1])

    add     eax,SIZEOF(FILE)

    lea     ecx,args
    invoke  vfprintf,eax,_format,ecx
    ret

printf ENDP

END start

The Universal C run-time version :

Code: [Select]
int __cdecl __stdio_common_vfprintf(unsigned __int64 _Options, FILE* _Stream, char const* _Format , int _Locale, va_list);

int printf(const char * format, ...)
{
int retVal;
va_list vl;
va_start(vl, format);
retVal = __stdio_common_vfprintf(0, _stdout,format, 0, vl);
va_end(vl);
return retVal;
}


Code it... That's all...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: printf implementation
« Reply #1 on: February 04, 2024, 11:40:48 AM »
Thanks.
poasm programmers needs an easy way to test code and printf family is useful for them.
« Last Edit: February 04, 2024, 08:57:46 PM by TimoVJL »
May the source be with you

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: printf implementation
« Reply #2 on: March 03, 2024, 11:13:40 AM »
Converting real4 to real8 :

Code: [Select]
.486
.model flat,stdcall
option casemap:none

ExitProcess PROTO :DWORD
__p__iob PROTO C
vfprintf PROTO C :DWORD,:DWORD,:VARARG
printf   PROTO C :DWORD,:VARARG

includelib  kernel32.lib
includelib  msvcrt.lib

_iobuf STRUCT

    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?

_iobuf ENDS

FILE TYPEDEF _iobuf

.data

pi      real4 3.141592
format  db '%s is nearly equal to %f',0
str1    db 'Pi',0

.data?

pi8     real8 ?

.code

start:

    fld     pi
    fstp    pi8

    invoke  printf,ADDR format,ADDR str1,pi8
           
    invoke  ExitProcess,0

printf PROC C _format:DWORD,args:VARARG

    invoke  __p__iob

;   #define stdout (&__iob_func()[1])

    add     eax,SIZEOF(FILE)

    lea     ecx,args
    invoke  vfprintf,eax,_format,ecx
    ret

printf ENDP

END start
Code it... That's all...