Copyright (C) 2000-2012 |
GNU Info (nasm.info)Section 4.54.5. Preprocessor Loops: `%rep' =============================== NASM's `TIMES' prefix, though useful, cannot be used to invoke a multi-line macro multiple times, because it is processed by NASM after macros have already been expanded. Therefore NASM provides another form of loop, this time at the preprocessor level: `%rep'. The directives `%rep' and `%endrep' (`%rep' takes a numeric argument, which can be an expression; `%endrep' takes no arguments) can be used to enclose a chunk of code, which is then replicated as many times as specified by the preprocessor: %assign i 0 %rep 64 inc word [table+2*i] %assign i i+1 %endrep This will generate a sequence of 64 `INC' instructions, incrementing every word of memory from `[table]' to `[table+126]'. For more complex termination conditions, or to break out of a repeat loop part way along, you can use the `%exitrep' directive to terminate the loop, like this: fibonacci: %assign i 0 %assign j 1 %rep 100 %if j > 65535 %exitrep %endif dw j %assign k j+i %assign i j %assign j k %endrep fib_number equ ($-fibonacci)/2 This produces a list of all the Fibonacci numbers that will fit in 16 bits. Note that a maximum repeat count must still be given to `%rep'. This is to prevent the possibility of NASM getting into an infinite loop in the preprocessor, which (on multitasking or multi-user systems) would typically cause all the system memory to be gradually used up and other applications to start crashing. automatically generated by info2www version 1.2.2.9 |