Author Topic: Excessive use of macros?  (Read 6873 times)

Offline 256byteram

  • Jr. Member
  • *
  • Posts: 2
Excessive use of macros?
« on: December 15, 2012, 11:21:15 AM »
Hi all!

First post here (I hope I've posted this in the appropriate section of the forum). I've been using nasm for quite a while now for its macro features, to target it to a TTL processor I'm making. I'm positive this isn't the intention of the nasm project, but I haven't found another assembler that's as capable. I have, however, run into a slight problem with my excessive use of macros.

When I assemble the file linked below (and its included macro definitions file) I get this error:
Code: [Select]
nasm -l hello.lst hello.asm
hello.asm:32: error: parser: instruction expected

Looking at the preprocessor output, I notice this:
Code: [Select]
COUT:
 db 0x43,0x43,0x5F,0x13
%line 32+0 hello.asm

 dd 0x00080002
 ADDCODE 0x53
%line 35+1 hello.asm
 db 0x13,0x37,0,0
%line 35+0 hello.asm

It all runs fine up until that ADDCODE macro is called. It is called many times previously in the assembler file, but it appears to barf at that point. If I comment out everything past the "CFETCH" instruction it assembles correctly, producing the output I was expecting. Am I doing something wrong? Is nasm unable to handle such ridiculous macros?

A short description of my architecture is here. It's a stack architecture, not unlike the Forth language.
http://kaput.homeunix.org/~thrashbarg/sm32/sm32prelim.txt

Here are my macro definitions I've created for the architecture:
http://kaput.homeunix.org/~thrashbarg/sm32/macros.inc

And the sample file ("Hello World" no less):
http://kaput.homeunix.org/~thrashbarg/sm32/hello.asm

Any help is much appreciated!

-256byteram

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Excessive use of macros?
« Reply #1 on: December 15, 2012, 02:49:44 PM »
You are calling ADDCODE from within the ADDCODE macro. That type of implicit macro recursion is not allowed, as it would make the overloading of instructions impossible to achieve.

I only glanced at your code, but if I understand your intent correctly -- that of an opcode buffer -- you can replace the attempted macro recursion with a repeat of the contents of that first if block, or break down the macro even further, like so:

Code: [Select]
%define full 0

%macro SETCODE 1
%if code0 = 0
%undef code0
%define code0 %1
%undef broken
%define broken 1
%define full 0
%elif code1 = 0
%undef code1
%define code1 %1
%undef broken
%define broken 1
%define full 0
%elif code2 = 0
%undef code2
%define code2 %1
%undef broken
%define broken 1
%define full 0
%elif code3 = 0
%undef code3
%define code3 %1
%undef broken
%define broken 1
%define full 0
%else
%define full 1
%endif
%endmacro

%macro ADDCODE 1
SETCODE %1
%if full = 1
BREAK
SETCODE %1
%endif
%endmacro

Note that there are more advanced ways to simplify those macros, as you will eventually discover, but the above code stays in-line with your current code base.

Offline 256byteram

  • Jr. Member
  • *
  • Posts: 2
Re: Excessive use of macros?
« Reply #2 on: December 15, 2012, 03:08:38 PM »
Ah! Yes that would do it.  ::) Thanks for the suggestion too!