Search code examples
assemblymipsmplabpic32

How can one create a unique label for each macro call using mips assembly in MPLAB X


I have a macro which performs a comparison, and jumps to the end of the macro if that comparison isn't true. Here is a simplified example:

.macro do_work_if_value_not_zero value
    li s0, value
    bne s0, zero, exit_label
    nop   

    // Do work

    exit_label:
.endm

The issue however is that I call this macro several times like so:

do_work_if_value_not_zero 5
do_work_if_value_not_zero 3
do_work_if_value_not_zero 12

As a result I receive the following error:

Error: symbol `exit_label' is already defined



Because I never call the macro with the same parameter multiple times, I attempted to use the parameter name concatenated with a ':' to create a unique label like so:

.macro do_work_if_value_not_zero value
    li s0, value
    bne s0, zero, \value
    nop   

    // Do work

    \value:
.endm

However this did not seem to work, and I received more errors.

Thus my question is, how can one create a unique exit label for each macro call to avoid this issue?


Solution

  • Most assemblers allow local labels like this:

    .macro do_work_if_value_not_zero 
        li s0, value
        bne s0, zero, 1f     # 1f means branch forward to the next label '1:'
        nop   
    
        // Do work
    
        1:
    .endm
    

    From the MIPS assembler manual here:

    A generated label is a single numeric value (1...255). To reference a generated label, put an f (forward) or a b (backward) immediately after the digit. The reference tells the assembler to look for the nearest generated label that corresponds to the number in the lexically forward or backward direction.