It’s often useful to be able to handle strings in macros. NASM supports two simple string handling macro operators from which more complex operations can be constructed.
The %strlen
macro is
like %assign
macro in that it creates (or redefines) a
numeric value to a macro. The difference is that with %strlen
, the numeric value is the length of a string. An example of the
use of this would be:
%strlen charcnt 'my string'
In this example, charcnt
would receive the value 8, just
as if an %assign
had been used. In this example,
'my string'
was a literal string but it could also have been
a single-line macro that expands to a string, as in the following example:
%define sometext 'my string' %strlen charcnt sometext
As in the first case, this would result in charcnt
being
assigned the value of 8.
Individual letters in
strings can be extracted using %substr
. An example of its use is probably
more useful than the description:
%substr mychar 'xyz' 1 ; equivalent to %define mychar 'x' %substr mychar 'xyz' 2 ; equivalent to %define mychar 'y' %substr mychar 'xyz' 3 ; equivalent to %define mychar 'z'
In this example, mychar gets the value of 'y'
. As with
%strlen
(see Section 4.2.1), the first parameter is the
single-line macro to be created and the second is the string. The third parameter
specifies which character is to be selected. Note that the first index is 1, not 0 and
the last index is equal to the value that %strlen
would
assign given the same string. Index values out of range result in an empty string.