Assembly language > Assembly discussions

printf implementation

(1/1)

Vortex:
Here is a printf implementation similar to the inlined version of the latest MS VC releases :


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

The Universal C run-time version :


--- Code: ---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;
}

--- End code ---


TimoVJL:
Thanks.
poasm programmers needs an easy way to test code and printf family is useful for them.

Vortex:
Converting real4 to real8 :


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

--- End code ---

Navigation

[0] Message Index

Go to full version