Macros

A macro is another way of ensuring modular programming in assembly language. A macro is a sequence of instructions, assigned by a name and could be used anywhere in the program.

The main uses for a macro

  • To make it easier to follow the logic of the source code by replacing a block of code with a single meaningful name.

  • To avoid repeating a block of code several times.

Macro Example

.macro  SetBitField register, literal       @ SetBitField takes a register and a bitfield from svd2forth as parameters
        ldr  r0, = \register                @ load the ADDRESS of the variable "register" into r0
        ldr  r1, [r0]                       @ load CONTENTS of "register" into r1
        ldr  r2, = \literal                 @ load the VALUE of "literal" into r2
        orrs r2, r1                         @ OR the contents of "register" and the VALUE of "literal" into r2
        str  r2, [r0]                       @ store the ORed value back into "register", any previous set bits are untouched
.endm

The following code is from my Assembly article below which should be referred to here : https://mecrisp-stellaris-folkdoc.sourceforge.io/projects/blink-f0disco-gdbtui/doc/readme.html