Strategies for implementing macro functionality in assemblers

You can talk about almost anything that you want to on this board.

Moderator: Moderators

zzo38
Posts: 1075
Joined: Mon Feb 07, 2011 12:46 pm

Re: Strategies for implementing macro functionality in assemblers

Post by zzo38 » Fri Aug 21, 2020 6:52 pm

I think the C #define macros can be useful, but they aren't powerful enough. What I have done is like:
  • Arguments are not expanded normally.
  • Inside of the definition of a macro, you can specify a command which does expand its argument (and/or evaluate arithmetic) and assigns the result to one of the macro argument registers.
  • There are also other commands usable in macros, such as unique labels, tail recursion of macros, etc.
[url=gopher://zzo38computer.org/].[/url]

nocash
Posts: 1228
Joined: Fri Feb 24, 2012 12:09 pm
Contact:

Re: Strategies for implementing macro functionality in assemblers

Post by nocash » Sat Aug 22, 2020 5:54 am

I would process macros is two passes, first replacing the parameter names by the actual parameters, and then assembling the resulting source code as usually.

That steps can be implemenented line by line, so there aren't huge amounts of memory required during macro processing. However, it would be best to have the original macro definition cached in RAM for faster processing (just memorizing the filename and file offset where the definition can be found does work too, but the OS file read functions would be kinda slow when reloading the macro hundreds of times).

I think the most important feature is having some kind of escape character to mark the begin/end of the parameter names. That isn't required if the parameter is enclosed in normal separators (comma, space, plus, minus, brackets, end of line, etc.) but it's needed when appending/inserting parameters to label strings. Borland tasm uses & for that, for example wìth param=0:

Code: Select all

mov a,param               --> mov a,0
mov x,function&param      --> mov x,function0
mov y,function&param&end  --> mov y,function0end
A double && translates to & after macro processing, that can be useful when needing & as such, or when intending to have the & to be processed in a nested macro.

Using things like "x,y,z" or "cx,cy" as parameter names can produce nasty results if the CPU has registers/opcodes with identical names, eg. "x,y" on 6502 or "cx" on 80x86. Best workaround is to avoid using reserved words as parameter names (or better, throw error messages).
homepage - patreon - you can think of a bit as a bottle that is either half full or half empty

creaothceann
Posts: 256
Joined: Mon Jan 23, 2006 7:47 am
Location: Germany
Contact:

Re: Strategies for implementing macro functionality in assemblers

Post by creaothceann » Tue Sep 01, 2020 7:43 am

nocash wrote:
Sat Aug 22, 2020 5:54 am
I think the most important feature is having some kind of escape character to mark the begin/end of the parameter names. That isn't required if the parameter is enclosed in normal separators (comma, space, plus, minus, brackets, end of line, etc.) but it's needed when appending/inserting parameters to label strings. Borland tasm uses & for that, for example with param=0:

Code: Select all

mov a,param               --> mov a,0
mov x,function&param      --> mov x,function0
mov y,function&param&end  --> mov y,function0end
A double && translates to & after macro processing, that can be useful when needing & as such, or when intending to have the & to be processed in a nested macro.
A macro can be thought of as a function that is called during compilation, accepts strings as parameters (empty if omitted), and returns a string. In that case the code above could be written as:

Code: Select all

macro param(prefix, postfix)  {return prefix + "0" + postfix;}

mov a, param                     --> mov a, 0
mov x, param("function")         --> mov x, function0
mov y, param("function", "end")  --> mov y, function0end
My current setup:
Super Famicom ("2/1/3" SNS-CPU-GPM-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10

Post Reply