9.4.4 FORC Loops

The FORC directive is similar to FOR but takes a string of text rather than a list of arguments. The statements are assembled once for each character (including spaces) in the string, substituting a different character for the parameter each time through. <$I<< \ra (angle brackets);FORC loops> <$IAngle brackets (<< \ra);FORC loops>

The syntax looks like this:

FORC parameter, <text>
statements
ENDM

The text must be enclosed in angle brackets. The following example illustrates FORC:

FORC arg, <ABCDEFGHIJKLMNOPQRSTUVWXYZ>

BYTE '&arg' ;; Allocate uppercase letter

BYTE '&arg' + 20h ;; Allocate lowercase letter

BYTE '&arg' - 40h ;; Allocate ordinal of letter

ENDM

Notice that the substitution operator must be used inside the quotation marks to make sure that arg is expanded to a character rather than treated as a literal string.

With earlier versions of MASM, FORC is often used for complex parsing tasks. A long sentence can be examined character by character. Each character is then either thrown away or pasted onto a token string, depending on whether it is a separator character. In MASM 6.0, the predefined macro functions and string processing directives discussed in Section 9.5 are usually more efficient for these tasks.