3

On AVR GNU assembler I'm trying to create a label inside a macro definition.

I would like to create a macro, which has a private lable, or unique label inside - just to be able to jump inside macro definition and use multiple instances of this macro.

Below is example of what I'm trying to do.

.macro SetFlag par0
    brid  local_noInt

    cli
    lds   RTMP,FLAGS_M
    sbr   RTMP,(1<<\par0)
    sts   FLAGS_M,RTMP
    sei

    rjmp  local_end
    local_noInt:

    lds   RTMP,FLAGS_M
    sbr   RTMP,(1<<\par0)
    sts   FLAGS_M,RTMP

    local_end:
.endm
Mike
  • 2,146
  • 1
  • 14
  • 29
Marcin Daw
  • 35
  • 4

1 Answers1

3

You can use numbered labels (these do not need to be unique) and jump instruction with suffix b or f with the meaning to jump to nearest label with this numerical value backward or forward respectively.

So, for your particular case following should work:

.macro SetFlag par0
    brid  1f

    cli
    lds   RTMP,FLAGS_M
    sbr   RTMP,(1<<\par0)
    sts   FLAGS_M,RTMP
    sei

    rjmp  2f

 1: lds   RTMP,FLAGS_M
    sbr   RTMP,(1<<\par0)
    sts   FLAGS_M,RTMP
 2:
.endm

(Code not tested, I have put it here as an illustration how to use labels only.)

But note that the label at the end of macro without any instruction following can cause some problems of its own too.

Martin
  • 1,054
  • 5
  • 10
  • 1b worked for me. 1f not. but i dont understand (and find a reference) why string labels notworking and gives "already defined" error. – aleXela Jul 12 '23 at 16:16