simonowen / pyz80

pyz80 - a Z80 cross assembler
GNU General Public License v2.0
18 stars 8 forks source link

Macro implementation incomplete #15

Open stefandrissen opened 4 years ago

stefandrissen commented 4 years ago

From the source:

# TODO: define and assemble macro blocks

Z80ASM has some nice macro facilities - manual at http://s100computers.com/Software%20Folder/Assembler%20Collection/z80asm%20(SLR%20Systems).pdf

[LABEL] REPT <EXP>
Repeat Macro. The repeat macro reads a group of statements up to the next matching ENDM or MEND pseudo-op. That is the BODY of the macro. The group of statements are then scanned and assembled <EXP> times. <EXP> must be defined and absolute on the first pass.

[LABEL] IRPC <DUMMY>,<CHARLIST>
Indefinite Repeat Character. The IRPC macro reads the body of the macro, just like the REPT macro. The body of the macro is then scanned and assembled once for each character in the given CHARLIST. However, any occurrence of the item DUMMY in the body is replaced by the current character in CHARLIST.

[LABEL] IRP < DUMMY >,<<PARAMLIST>>
Indefinite Repeat. The IRP is very similar to the IRPC, except that the parameters are multi-character items. Note that the brackets around the PARAMLIST are required

<LABEL> MACRO [<DUMMYLIST>]
Macro Definition. This is the most sophisticated form of macro item. This statement declares the beginning of a MACRO definition. The <LABEL> must be present; it is the name by which the macro will be referred to later. It follows the same rules as labels. A colon after the label is optional. The <DUMMYLIST> is optional. If present, it declares <dummy> items which will be replaced by other items when the macro is called. Multiple <dummy> items are separated by commas. <Dummy> items follow the same rules as labels. All source lines following the macro header are stored in memory under the name <LABEL>, up to the next matching ENDM or MEND. Since macros may contain macro definitions (nested macro definitions), matching means the ENDM at the same nest level. To call the macro, just use the macro name as you would any otheropcode or pseudo-op, passing with it any desired parameters.

LOCAL <LABELIST>
Local Labels. This pseudo-op declares a label or set of labels to be local to the current macro expansion, i.e., each time the macro is expanded or repeated, generate a unique label for each of these. The labels in <LABELIST> must be legal labels. The LOCAL statement should occur before the labels are referenced in the macro.

EXITM
Exit macro expansion. This pseudo-op allows an easy way to terminate a macro expansion. It is usually used in conjunction with a conditional test. EXITM is also allowed in an include file or maclib file, to halt the processing of those files.

MACLIB <FILENAME.EXT>
Include Macro Definition File. This pseudo-op is identical to an INCLUDE, except that it is ignored on the second pass. This pseudo-op is used to read macro definition files, or any source files that produce no output code. This is to save time on 2-pass assemblies since the whole MACLIB file will be skipped on the second pass.

Dummy Parameter Evaluation While reading the body of a macro, Z80ASM looks for the occurrence of any of the optional dummy parameters in the text. There are several rules that are followed.

  1. Dummy parameters are not replaced in a comment field.
  2. A dummy parameter delimited by spaces, tabs, commas, etc., and not inside a quoted string, is automatically flagged for replacement at expansion time.
  3. Any dummy parameter preceded or followed by an ampersand (&),whether or not it is contained in a quoted string, is flagged for replacement, and the leading and/or trailing ampersand is removed. This is useful for dummy replacement in strings, and also for concatenating a parameter to something else.
  4. Any dummy parameter preceded by an up-arrow (^) is ignored.The up-arrow is used to cause a character to be taken literally,i.e., an up-arrow can be represented as two up-arrows in succession.