Author Topic: NASM Recursive macro demo.  (Read 17988 times)

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
NASM Recursive macro demo.
« 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
« Last Edit: December 13, 2012, 07:18:24 AM by Mathi »

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: NASM Recursive macro demo.
« Reply #1 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

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: NASM Recursive macro demo.
« Reply #2 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.

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: NASM Recursive macro demo.
« Reply #3 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.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: NASM Recursive macro demo.
« Reply #4 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. 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 ;)