Author Topic: Macro failures - An attempt at SWITCH/CASE  (Read 17065 times)

mark allyn

  • Guest
Macro failures - An attempt at SWITCH/CASE
« on: August 21, 2009, 12:51:19 AM »
Hi everyone -  the following Macro produces error messages when invoked.    The SWITCH macro generates an error message that says:  comma, colon, or end of line expected, and the CASE macro produces an error message that says: label or instruction expected at start of line.

%macro SWITCH 1
%push switch
%assign __curr 1
   mov eax, %1
   jmp %$loc(__curr)         ;%$loc(__curr)
%endmacro

%macro CASE 1
%ifctx switch
   %$__curr:            ;loc(__curr):
   %assign __curr __curr+1
   mov ebx, %1
   cmp eax, ebx
   jne __curr               ;%$loc(__curr)
%endif
%endmacro

I have been thru this code a whole bunch of times and can't see what ails it.  

If anyone has a clue or suggestion, I would be grateful.

Ciao,
Mark Allyn

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Macro failures - An attempt at SWITCH/CASE
« Reply #1 on: August 21, 2009, 07:00:58 AM »
Well, the first error is because Nasm doesn't like the parentheses. The second error is because an identifier (your "__curr:" label is "1:", etc.) may not begin with a number (took me a while to figure that one out!). If you use "%$loc%$__curr", Nasm will make labels like "..@1.loc1:". "..@1.loc2:", etc. You'll need an "ENDSWITCH" macro to generate the final label, or Nasm will complain that it's undefined. I imagine you'll want a "DEFAULT" macro, too - and some error checking in case some "user" tries to use CASE before SWITCH or after DEFAULT, etc... Trashing eax and ebx in a macro isn't too great...

Good luck, Mark!

Best,
Frank

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Macro failures - An attempt at SWITCH/CASE
« Reply #2 on: August 21, 2009, 07:25:39 AM »
I should mention, in "case" you're not aware of it, the "-e" switch will cause Nasm to "preprocess only" (to stdout). This can be useful in debugging macros - shows what's getting passed to the "assemble" stage. It doesn't do well on forward references, but is still some help. Macros are a PITA to debug, in any "case"!

Best,
Frank

mark allyn

  • Guest
Re: Macro failures - An attempt at SWITCH/CASE
« Reply #3 on: August 21, 2009, 12:30:42 PM »
Hi Frank -

Man oh man, I would never have come up with that solution.  I can't dig it out of the NAsM manual.  

I was sure too that it was a problem with the %$loc(___curr) and tried all kinds of ways to rewrite it.  Even looked for the thing on GOOGLE.  Nothing I tried could fix it.  

Thanks a bunch,
Mark