Get first whitespace-separated macro argument

Viewed 82

I'd like to get the first whitespace-separated macro argument in NASM. It's easy to do it if arguments are comma-separated, for example this emits 5 nops, ignoring the blah:

%macro foo 1+
times 5 %1
%endmacro
foo nop, blah

How do I define my foow macro so that it works as foow nop blah more-blah, i.e. whitespace-separated arguments? I only need the 1st argument.

1 Answers

You can use %defstr and then %substr in a %rep loop to figure out how long the first non-space sequence is. Then use %deftok to convert it back into a non-string token. Here's an example. (Note that the %$exit variable is only needed for some older versions of NASM that had bugs in their %exitrep handling .)

%macro bar 1.nolist
 %push
 %defstr %$string %1
 %strlen %$length %$string
 %assign %$ii 0
 %assign %$exit 0
 %rep %$length
  %substr %$point %$string %$ii + 1, 1
  %if %$point == 32 || %$point == 9
   %assign %$exit 1
   %exitrep
  %endif
  %ifn %$exit
   %assign %$ii %$ii + 1
  %endif
 %endrep
 %substr %$word %$string 1, %$ii
 %deftok %$token %$word
        %$token
 %pop
%endmacro


bar nop quux xyzzy

Here's a test run:

$ nasm -E test.asm
%line 22+1 test.asm


 nop
$ 
Related