When to use ASM directives and PIC instructions confusion

Discussion in 'Embedded Systems and Microcontrollers' started by rockrockmcrock, Mar 3, 2013.

  1. rockrockmcrock

    Thread Starter New Member

    Jan 6, 2012
    19
    0
    Hi all,

    Can any guru clear this up for me please - I'm getting all confused about directives and instructions.

    I was pretty happy when I learnt to programme pic's that directives (#Define) controlled the assembler and instructions (decfsz) controlled the MC.

    Now I'm coming across mpasm code with 'IF/ELSE' and 'WHILE' type directives that are actively part of the programme - rather than controlling how it is assembled. For example in a macro:

    Somemacro macro somevar
    IF someVar == 1
    bsf GPIO, 1
    ELSE
    bcf GPIO, 1
    ENDIF
    endm

    I didn't think asm had an IF or WHILE instruction! Assuming asm does, when would I use these rather than simulating them with decfsz and btfss etc.? IF and WHILE loops are obviously much easier to read and understand in a block of asm, but are they as efficient as simulation through pic instructions?

    Efficiency is quite important in my current code (hence choice of asm rather than C) as my current project acts as a slave for a bespoke protocol (similar to I2C) translating it to standard I2C, so all work needs to be done between bespoke protocol packets as there is no way of pausing transmission.
     
    Last edited: Mar 3, 2013
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    All these macros only generate assembler at compile time - have a look at the disassembly window to see what was generated.
     
  3. rockrockmcrock

    Thread Starter New Member

    Jan 6, 2012
    19
    0
    Thanks mate - but yeah I get that, macros are a repeatable way of generating asm code in your program.

    What was confusing me is that I didn't think asm had an IF statement for variable. The only IF's I've ever used have been for conditional asssembly(e.g. if PIC==pic12Fxxx then include pic12Fxxx.inc file).

    From what you imply IF's and WHILE's can be used for more than conditional assembly (that right?) so I'll check the generated code.
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I hope I didn't imply that. After the program has been compiled all is left is normal assembler (no IF or WHILE). The variables in the macros are only used while compiling.
     
  5. rockrockmcrock

    Thread Starter New Member

    Jan 6, 2012
    19
    0
    ah, that's what's confusing me. All that should be left (beyond the compiler log etc. files) is a hex file translated from the asm by the compiler for u to whack on your pic.

    I'm assuming now that mplab translates the IF an WHILE statements to equivalent asm and then hex (or probably straight to hex), as they are definely not being used for conditional assmebly. like my exaqmple they are being used for conditional calls to set pic port values while the prog is running. As far as I can see this has to be the answer - weird!
     
  6. Lourens

    New Member

    Jul 16, 2012
    14
    1
    MPLAB's assembler do not generate any code (including hex) from IF, ELSE, WHILE, (and some others) directives. These are directives for you to use in your code to manipulate the real code, i.e. include one of two alternate pieces of code according to some switch/value you set.

    MySwitch EQU 5
    .....
    IF MySwitch == 5
    ....your assembler instructions 1...
    ELSE
    ....your assembler instructions 2...
    ENDIF
    ....

    For the above example the hex code generated will only contain:
    ...your desired assembly 1...
     
  7. rockrockmcrock

    Thread Starter New Member

    Jan 6, 2012
    19
    0
    Thanks that confirms my original thoughts - i went back to the code and 2 of the 3 are ok and work as u say, the 3rd that was confusing me would appear to be a bug as it would make sense if there was a btfsc instruction before calling the macro

    Cheers all
     
Loading...