Odd usage of macros in PIC assembler

Discussion in 'Embedded Systems and Microcontrollers' started by Markd77, Feb 1, 2013.

  1. Markd77

    Thread Starter Senior Member

    Sep 7, 2009
    Just thought I'd post this in case anyone else finds it amusing. Each macro is used once, later in the program. This is actually from a Microchip app note, which are mostly pretty well written, but this seems to be just using macros for the sake of it. If they were being used ten times each I could see the justification.

    Code ( (Unknown Language)):
    1. ;*******************Delay Macros**********************************
    2. ;           Each macro delay for 1 inst. cycle less than
    3. ;           required, as the instruction immediately
    4. ;           before the macro call provides 1 inst. cycle.
    5. ;*****************************************************************
    6. THIGH   macro                   ; Clock high time delay (5 us)
    7.         goto    $+1             ; 2-inst. cycle delay (2 us)
    8.         goto    $+1             ; 2-inst. cycle delay (2 us)
    9.         endm
    11. THDSTA  macro                   ; Start condition hold time delay (5 us)
    12.         goto    $+1             ; 2-inst. cycle delay (2 us)
    13.         goto    $+1             ; 2-inst. cycle delay (2 us)
    14.         endm
    16. TSUSTA  macro                   ; Start condition setup time delay (5 us)
    17.         goto    $+1             ; 2-inst. cycle delay (2 us)
    18.         goto    $+1             ; 2-inst. cycle delay (2 us)
    19.         endm
    21. TSUSTO  macro                   ; Stop condition setup time delay (5 us)
    22.         goto    $+1             ; 2-inst. cycle delay (2 us)
    23.         goto    $+1             ; 2-inst. cycle delay (2 us)
    24.         endm
    26. TAA     macro                   ; Output valid from clock delay (4 us)
    27.         goto    $+1             ; 2-inst. cycle delay (2 us)
    28.         nop                     ; 1-inst. cycle delay (1 us)
    29.         endm
  2. atferrari

    AAC Fanatic!

    Jan 6, 2004
    Albeit not frequently, I use macros once in the whole code, eventually consisting of just one line long.

    For me, it is much more meaningful, "START_TRANSMISSION" than "BSF LCD_FLAGS,XMIT"

    In the beginning I feared I was doing something "wrong", but, if it works, why not?

    The trick, I think, is to use a really meaningful name (to you) for the macro.

    Amusing? Not really.
    Last edited: Feb 1, 2013
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    ...if they were named better, I'd say that they were to exacerbate the comments and show exactly what the code does, but like this....seems like it's only meant to confuse...:confused:
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Worse then the macros you write are the macro's you may not even be aware of.

    I remember a post once where the OP had a "typo" in his code, he typed "MOVFW" where he intended "MOVWF" No harm no foul you think, as the compiler should catch that?

    Nope. Turns out "MOVFW" is one of the "Pseudo-Instruction"built into MPLAB. They are very obscure, albeit well defined. You can find them in the "MPLAB Assembler Help" chm under "12-Bit/14-Bit Instruction Width Pseudo-Instructions."
    tshuck likes this.
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    There were a lot of overloaded macro instruction names we used in the old days, to make the PIC asm easier to work with, things like TESTF (which is movf,f for the purpose of testing if it ==0).

    I had a lot of favorites, especially things like SKIPZ, SKIPNZ, SKIPWLE, SKIPWGT which are much easier to work with than ugly bit tests on specific bits fo the status register.

    I even created some like SKIP2, SKIP3, which skip forward 2 or 3 instructions (saving the need for ugly labels and are really useful) and likewise things like BACK1, BACK2, BACK3 etc.

    Creating and using macros really let you add to the assembler and customise it for speed of coding, because let's face it RISC asm is a really ugly clunky way to make code.