The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on June 02, 2005, 01:20:10 PM

Title: Macros for a code generator
Post by: hutch-- on June 02, 2005, 01:20:10 PM
I have written code generators in basic for many years because of the string capacity and string concantenation but I have wanted for some time to be able to do the same stuff in MASM. Here is the first try.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    fileinit MACRO
      .data?
        @@__hFile_@@ dd ?                           ;; file handle
        @@_buffer_@@ db 1024 dup (?)                ;; line buffer
        @_@p_buf_@_@ dd ?                           ;; buffer pointer
      .code
    ENDM

    startfile MACRO txt
      mov @@__hFile_@@, fcreate(txt)
    ENDM

    text$ MACRO arg:VARARG
      mov @_@p_buf_@_@, ptr$(@@_buffer_@@)          ;; clear buffer
      fprint @@__hFile_@@,cat$(@_@p_buf_@_@,arg)    ;; print the text to file
    ENDM

    nl MACRO
      test fwrite(@@__hFile_@@,chr$(13,10),2), eax
    ENDM

    endfile MACRO
      fclose @@__hFile_@@
    ENDM

    qt equ <chr$(34)>

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    fileinit

    startfile "testfile.asm"

    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    text$ "    include \masm32\include\masm32rt.inc"
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "comment * -----------------------------------------------------"
    text$ "                        Build this  template with"
    text$ "                        CONSOLE ASSEMBLE AND LINK"
    text$ "        ----------------------------------------------------- *"
    nl
    text$ "    .data?"
    text$ "      value dd ?"
    nl
    text$ "    .data"
    text$ "      item dd 0"
    nl
    text$ "    .code"
    nl
    text$ "start:"
    text$ "   "
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "    call main"
    nl
    text$ "    exit"
    nl
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "main proc"
    nl
    text$ "    cls"
    text$ "    print ",qt,"Hello World",qt,",13,10"
    nl
    text$ "    ret"
    nl
    text$ "main endp"
    nl
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "end start"

    endfile

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Title: Re: Macros for a code generator
Post by: Vortex on June 02, 2005, 05:41:46 PM
Hi Hutch,

It works fine on my Win XP Home Sp2, nice macro designs :U
Title: Re: Macros for a code generator
Post by: hutch-- on June 03, 2005, 02:37:31 AM
Here is version 2. I am just testing the capacity before doing much more work with it. Command line added to get file name and it builds a "makeit.bat" file to build the example.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    fileinit MACRO
      .data?
        @@__hFile_@@ dd ?                           ;; file handle
        @@_buffer_@@ db 1024 dup (?)                ;; line buffer
        @_@p_buf_@_@ dd ?                           ;; buffer pointer
      .code
    ENDM

    startfile MACRO txt
      mov @@__hFile_@@, fcreate(txt)
    ENDM

    text$ MACRO arg:VARARG
      mov @_@p_buf_@_@, ptr$(@@_buffer_@@)          ;; clear buffer
      fprint @@__hFile_@@,cat$(@_@p_buf_@_@,arg)    ;; print the text to file
    ENDM

    nl MACRO
      test fwrite(@@__hFile_@@,chr$(13,10),2), eax
    ENDM

    endfile MACRO
      fclose @@__hFile_@@
    ENDM

    qt equ <chr$(34)>

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL pcmd  :DWORD
    LOCAL nbuf  :DWORD
    LOCAL buffer[128]:BYTE
    LOCAL namebuf[128]:BYTE

    mov pcmd, ptr$(buffer)

    invoke GetCL,1,pcmd
    cmp eax, 1
    je @F
    print "Please enter the project name MINUS the extension",13,10
    ret
  @@:

    fileinit

    mov nbuf, ptr$(namebuf)

    startfile cat$(nbuf,pcmd,".asm")

    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    text$ "    include \masm32\include\masm32rt.inc"
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "comment * -----------------------------------------------------"
    text$ "                        Build this  template with"
    text$ "                        CONSOLE ASSEMBLE AND LINK"
    text$ "        ----------------------------------------------------- *"
    nl
    text$ "    .data?"
    text$ "      value dd ?"
    nl
    text$ "    .data"
    text$ "      item dd 0"
    nl
    text$ "    .code"
    nl
    text$ "start:"
    text$ "   "
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "    call main"
    nl
    text$ "    exit"
    nl
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "main proc"
    nl
    text$ "    cls"
    text$ "    print ",qt,"Hello World",qt,",13,10"
    nl
    text$ "    ret"
    nl
    text$ "main endp"
    nl
    text$ "; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««"
    nl
    text$ "end start"

    endfile

  ; --------------------------
 
    startfile "makeit.bat"

    text$ "@echo off"
    nl
    text$ "if not exist rsrc.rc goto over1"
    text$ "\masm32\bin\rc /v rsrc.rc"
    text$ "\masm32\bin\cvtres /machine:ix86 rsrc.res"
    text$ " :over1"
    nl
    text$ "if exist ",qt,pcmd,".obj",qt," del ",qt,pcmd,".obj",qt
    text$ "if exist ",qt,pcmd,".exe",qt," del ",qt,pcmd,".exe",qt
    nl
    text$ "\masm32\bin\ml /c /coff ",qt,pcmd,".asm",qt
    text$ "if errorlevel 1 goto errasm"
    nl
    text$ "if not exist rsrc.obj goto nores"
    nl
    text$ "\masm32\bin\Link /SUBSYSTEM:WINDOWS ",qt,pcmd,".obj",qt," rsrc.res"
    text$ " if errorlevel 1 goto errlink"
    nl
    text$ "dir ",qt,pcmd,".*",qt
    text$ "goto TheEnd"
    nl
    text$ ":nores"
    text$ " \masm32\bin\Link /SUBSYSTEM:WINDOWS ",qt,pcmd,".obj",qt
    text$ " if errorlevel 1 goto errlink"
    text$ "dir ",qt,pcmd,".*",qt
    text$ "goto TheEnd"
    nl
    text$ ":errlink"
    text$ " echo _"
    text$ "echo Link error"
    text$ "goto TheEnd"
    nl
    text$ ":errasm"
    text$ " echo _"
    text$ "echo Assembly Error"
    text$ "goto TheEnd"
    nl
    text$ ":TheEnd"
    nl
    text$ "pause"

    endfile

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Title: Re: Macros for a code generator
Post by: Mark Jones on June 03, 2005, 06:41:16 AM
Oh no :dazzled: - Hutch is coding code which codes code... :clap:
(Does this mean he's micro-assembling?) :bg
Title: Re: Macros for a code generator
Post by: MichaelW on June 03, 2005, 08:08:07 AM
Would this be coding2 or coding4 ?



Title: Re: Macros for a code generator
Post by: hutch-- on June 03, 2005, 08:27:09 AM
This is sounding like a class inclusion paradox.  :green

Actually its based on laziness, I HATE rewriting code that I have done before so code generators do it for me.
Title: Re: Macros for a code generator
Post by: Mark_Larson on June 03, 2005, 08:25:37 PM

  I've done code generators in C before but never ASM.  Good job Hutch--! :)