On 10/3/2021 at 8:22 PM, Ed Minchau said:
I don't use macros at all; my editor doesn't have that feature. I can however copy and paste code.
Macros don’t come from the editor. The assembler does that. Technically, it’s part of the preprocessor, but most assemblers just integrate the preprocessor and the actual assembler.
So there’s usually a MACRO statement, which you follow with assembly code, like this:
name .macro
jsr print
.null "Hello @1!";first parameter
.endm
Source: http://tass64.sourceforge.net/#macro
Then you will invoke the macro, which replaces the macro call with the actual assembly:
#name “Tom”
Now you can use that anywhere in your code as a shorthand for a larger procedure. While this is obviously a trivial example, it’s very useful for doing repetitive tasks, like setting up KERNAL calls or 16-bit math. For example:
Add16 (adds two 16 bit numbers and stores the result in the specified address)
Print (takes the address of a null-terminated string and prints that to the screen using CHROUT)
Open and Close (for file I/O)
While Add16 seems trivial, it’s much easier to write something like Add16 Addr, 23 than to repeatedly write out the the several lines of code needed for a simple 16 bit add.
Another reason macros are useful over copypasta is that you can fix a bugged macro much more easily than dozens or hundreds of places in a program where you repeat a piece of code. If it turns out you forgot to clear the Carry flag before an add, and you pasted that 130 times, you’d have to fix 130 instances of your error. But if you make an Add16 macro, you can do it in the macro, then just re-assemble the program.