The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cman on October 02, 2009, 05:04:36 PM

Title: Output Macros
Post by: cman on October 02, 2009, 05:04:36 PM
How would one write a macro mimicking the behavior of the stdio.h library function printf() ? Are macros that could implement the behavior of iostream.h's "cout" function possible? I'm curious as to what can be created with Masm's macro processor. Thanks for any tips....
Title: Re: Output Macros
Post by: nathanpc on October 02, 2009, 05:27:56 PM
You can create include files(*.inc). :U

Hope i'm  helping!
Title: Re: Output Macros
Post by: PBrennick on October 02, 2009, 11:18:42 PM
nathanpc,

You are correct, all '.h' files are for C or C++ what '.inc' files are for assembly. .h files can and constantly are converted to .inc files but sometimes it can involve a bit of effort. Merely renaming the file will not work. Japheth has a tool to do this which works most of the time. Take a look at his excellent site.

Paul
Title: Re: Output Macros
Post by: GregL on October 03, 2009, 02:58:48 AM
cman,

A macro mimicking printf could be done, but it would be a lot of work.  cout would be even more difficult.  Why not just use the original printf function?

Here is a printf macro I wrote (it uses MSVCRT printf).


; ====================================
printf MACRO fmt, args:VARARG
    LOCAL first
    IFB <args>
        IFB <fmt>                              ;; args is blank and fmt is blank
            INVOKE crt_printf, TEXT(CR,LF)     ;; just print a newline.
        ELSE                                   ;; args is blank and fmt is not blank
            first SUBSTR <fmt>,1,1             ;; is fmt quoted text or a pointer
            IFIDN first,<">                    ;; is the first char of fmt a double quote?
                INVOKE crt_printf, TEXT(fmt)   ;; it's quoted text
            ELSE                               ;; otherwise
                INVOKE crt_printf, fmt         ;; it's a pointer
            ENDIF
        ENDIF
    ELSE                                       ;; args is not blank and fmt is not blank
        first SUBSTR <fmt>,1,1                 ;; is fmt quoted text or a pointer
        IFIDN first,<">                        ;; is the first char of fmt a double quote?
            INVOKE crt_printf, TEXT(fmt), args ;; it's quoted text
        ELSE                                   ;; otherwise
            INVOKE crt_printf, fmt, args       ;; it's a pointer to a format string
        ENDIF
    ENDIF
ENDM
; ====================================


It requires INCLUDE msvcrt.inc and INCLUDELIB msvcrt.lib.