Author Topic: Technique for argument count checking in MASM.  (Read 492 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4886
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Technique for argument count checking in MASM.
« on: April 15, 2017, 01:21:37 AM »
The attached ZIP file is a working example of how to use MACROS to perform argument count checking. Whether or not I could be bothered is another matter but it does work correctly. The macros are easy to replicate from a simple list of API function names with the arg count after them.

For normal statement calls the macro style is as follows.

    CreateWindowEx? MACRO args:VARARG
      test_arg_count 12,"CreateWindowEx?",args
      invoke CreateWindowEx,args
    ENDM


For function format calls the macro style is as follows.

    SendMessage@ MACRO args:VARARG
      .mi test_arg_count 4,"SendMessage@",args : invoke SendMessage,args
      EXITM <rax>
    ENDM


Cute what you can do with a genuine MACRO assembler that is not trying to be a compiler.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7666
  • Assembler is fun ;-)
    • MasmBasic
Re: Technique for argument count checking in MASM.
« Reply #1 on: April 15, 2017, 02:54:50 AM »
New SDK version?

demo.asm(91) : error A2006:undefined symbol : GdiplusStartup

Vortex

  • Member
  • *****
  • Posts: 1714
Re: Technique for argument count checking in MASM.
« Reply #2 on: April 15, 2017, 06:18:32 AM »
Hi Hutch,

Another method is to modify the invoke macro to check the argument count :

Code: [Select]
ExitProcess@ = 1
GetCommandLine@ = 0
GetModuleHandle@ = 1
CreateWindowEx@ = 12
DefWindowProc@ = 4
DispatchMessage@ = 1
GetMessage@ = 4
LoadCursor@ = 2
LoadIcon@ = 2
PostQuitMessage@ = 1
RegisterClassEx@ = 1
ShowWindow@ = 2
TranslateMessage@ = 1
UpdateWindow@ = 1
WndProc@ = 4
WinMain@ = 4

Code: [Select]
@ArgCount MACRO arglist:VARARG

LOCAL count
count = 0

    FOR arg, <arglist>
        count = count + 1
    ENDM
   
    EXITM %count

ENDM
.
.
invoke MACRO funcname:REQ,args:VARARG

LOCAL paramcount,paramcnt,p,disp,optaddr,stackbytes
.
.
p=@ArgCount(args)

temp TEXTEQU %funcname&@

IF p NE funcname&@

    % echo funcname should take temp parameters.

    .ERR

ENDIF

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4886
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Technique for argument count checking in MASM.
« Reply #3 on: April 15, 2017, 11:28:39 AM »
Thanks Erol, have it saved where I can find it for when I have enough brain left to digest it.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7666
  • Assembler is fun ;-)
    • MasmBasic
Re: Technique for argument count checking in MASM.
« Reply #4 on: April 15, 2017, 06:25:10 PM »
Code: [Select]
ExitProcess@ = 1
GetCommandLine@ = 0
GetModuleHandle@ = 1
CreateWindowEx@ = 12

One could even add the type of the argument:
Code: [Select]
j@ExitProcess equ 3849/58:s1
j@GetCommandLineA equ 3924/58:s
j@GetModuleHandleA equ 4002/58:s1
j@CreateWindowExA equ 12490/130:s111111111111
j@__toascii equ 5638/76:c6
j@__wgetmainargs equ 5643/76:c6
j@VariantTimeToSystemTime equ 8777/89:s39

(from \Masm32\MasmBasic\Res\pt.inc, October 2016, 585,746 bytes, included here)