News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Test piece for arg count checking.

Started by hutch--, September 28, 2016, 10:10:47 PM

Previous topic - Next topic

hutch--

This is a variation on an idea that Erol suggested some years ago, using macros as prototypes to get the arg count correct and not using "invoke" or similar to call an API but call it directly by its name. I don't personally care as I like the format of ML64 without prototypes but it is technically possible if the arg count is known when automating the production of the macros.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    OPTION DOTNAME                          ; required for macro files
    option casemap:none                     ; case sensitive

    includelib \masm32\lib64\kernel32.lib
    includelib \masm32\lib64\user32.lib

    PPROC TYPEDEF PTR PROC

    externdef __imp_MessageBoxA:PPROC
    externdef __imp_ExitProcess:PPROC

    include \masm32\macros64\macros64.inc

    MessageBox MACRO hndl:REQ, tmsg:REQ, dttl:REQ, dstyle:REQ
      IFNDEF __UNICODE__
        invoke __imp_MessageBoxA,hndl,tmsg,dttl,dstyle
      ELSE
        invoke __imp_MessageBoxW,hndl,tmsg,dttl,dstyle
      ENDIF
    ENDM

    ExitProcess MACRO ecode:REQ
      invoke __imp_ExitProcess,0
    ENDM

    rt MACRO args:VARARG
      args
      EXITM <rax>
    ENDM

    .data
      tmsg db "Text Message",0
      tttl db "Title",0
      pmsg dq tmsg
      pttl dq tttl

    .code

    function_format equ <0>

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

STACKFRAME

entry_point proc

    IF function_format
    ; ---------------
    ; function format
    ; ---------------
      LOCAL mbrv :QWORD
      LOCAL eprv :QWORD
      mov mbrv, rt(MessageBox 0,pmsg,pttl,0)
      mov eprv, rt(ExitProcess 0)
    ELSE
    ; ----------------
    ; statement format
    ; ----------------
      MessageBox 0,pmsg,pttl,0
      ExitProcess 0
    ENDIF

    ret

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end

jj2007

Quote from: hutch-- on September 28, 2016, 10:10:47 PM
This is a variation on an idea that Erol suggested some years ago

In fact, following the Alternative invoke macro thread, I had remembered that; however, I couldn't find it in the old forum. Part of GeneSys??

The idea is very good for people who come from HLL. It is more tricky when porting 32-bit sources to 64-bit, because of the brackets to add.

hutch--

 :biggrin:

In typical ML64 style, if any extra args are added, the macro just ignores them.

jj2007

Quote from: hutch-- on September 28, 2016, 10:36:22 PMIn typical ML64 style, if any extra args are added, the macro just ignores them.

That is in contradiction to the thread's title :eusa_naughty:

However, it's easy to fix:
MessageBox MACRO hndl:REQ, tmsg:REQ, dttl:REQ, dstyle:REQ, notallowed
  ifnb <notallowed>
      echo ** count your fingers and tell me if there are four of them **
      .err
  endif
  ...

hutch--

Yep, this works OK, God know why but I got an error last time I tried that technique.

    MessageBox MACRO hndl:REQ, tmsg:REQ, dttl:REQ, dstyle:REQ, extra
      IFNB <extra>
        .err <<< too many arguments >>>
      ENDIF
      IFNDEF __UNICODE__
        invoke __imp_MessageBoxA,hndl,tmsg,dttl,dstyle
      ELSE
        invoke __imp_MessageBoxW,hndl,tmsg,dttl,dstyle
      ENDIF
    ENDM

Humerously enough, it worked before on less than the required arg count but did not pick up the extra argument.