NASM - The Netwide Assembler

NASM Forum => Example Code => Topic started by: Mathi on December 13, 2012, 07:16:41 AM

Title: NASM Recursive macro demo.
Post by: Mathi on December 13, 2012, 07:16:41 AM
A small recursive macro demo which outputs the Hanoi Problem solution.
Assemble with: nasm -E hanoi.asm

Also, Is there any way i can supress the "%line" lines from the preprocessor output ??
 
Code: [Select]
;; hanoi.asm - Hanoi tower solution using NASM's recursive macro feature
;; Needs NASM 2.10rc2 or above. 
;; Assemble with :  nasm -E hanoi.asm
%irmacro HANOITOWER 4
%push HANOI
%if %1 > 0
%assign %$noofdisks %1 - 1
HANOITOWER %$noofdisks, %2, %4, %3
DISK MOVED FROM %2 TO %4
HANOITOWER %$noofdisks, %3, %2, %4
%endif
%pop
%endmacro
%push MAINCTX
%assign %$DISKS 3   ;; ***Change Disks here***
NO OF DISKS = %$DISKS
HANOITOWER %$DISKS,"A","B","C"
%pop
Title: Re: NASM Recursive macro demo.
Post by: Rob Neff on December 13, 2012, 05:34:48 PM
Also, Is there any way i can supress the "%line" lines from the preprocessor output ??
 

Is this what you want:
Code: [Select]
%irmacro HANOITOWER 4.nolist
Title: Re: NASM Recursive macro demo.
Post by: Keith Kanios on December 14, 2012, 12:41:38 AM
Note that the recursive macro capabilities (and the entire rewritten processor) were removed/reverted around NASM 2.10rc12 due to the potential for certain projects to utilize undocumented/undefined preprocessor features. Namely it was one bug report that demonstrated fundamentally incorrect nesting of %if and %rep.

The way the current (original) preprocessor works is like HTML, in that such mismatches or incorrect nesting are treated with leniency and the preprocessor happily carries on.

The recursive (prototype) preprocessor is more strict, like XML or C, in that mismatches or incorrect nesting are explicitly erroneous.

I'm not sure if the recursive preprocessor will ever be re-added. NASM's macro/endmacro style syntax for blocks lend toward a lenient coding style, where-as the more strict recursive preprocessor is better suited for C-like ({ }) blocks.
Title: Re: NASM Recursive macro demo.
Post by: Mathi on January 10, 2013, 12:06:34 PM
Keith,
      I didn't get it. Recursive macro capability is still there in
NASM version 2.10.07 compiled on Jan  2 2013

The hanoi.asm which i pasted works fine in that.
Plz clarify.


Thanks & Regards,
Mathi.
Title: Re: NASM Recursive macro demo.
Post by: Keith Kanios on January 12, 2013, 01:50:33 AM
I didn't get it. Recursive macro capability is still there in NASM version 2.10.07 compiled on Jan  2 2013

Short explanation: It's broken, don't rely on it.

Long explanation: The fact that it assembles correctly is actually due an early attempt at recursive macros and a bug that has persisted since 2.08rc10 (http://repo.or.cz/w/nasm.git/blob/f09116f44582d31321dd2b6d0e203f1714644d05:/preproc.c#l2088). It's easy to glance over, obviously, but that linked line of code should really be: if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {

I initially tried adding recursive macros to the original preprocessor, around the 2.07 series, but it was found to be unreliable due to the nature of the original processor, so it was (mostly) turned off in 2.08rc10.

I went on to do the recursive preprocessor for the 2.10 series, but the original preprocessor was merged back in as previously explained.

Your code doesn't seem to invoke any unreliable logic, so for the time being it works... until Cyrill sees this message and 2.10.08 is released ;)