• AVR Freaks

Hot!#asm not recognised by MPLAB X or XC8 compiler

Page: 12 > Showing page 1 of 2
Author
nickoatley
Starting Member
  • Total Posts : 33
  • Reward points : 0
  • Joined: 2012/11/15 09:20:59
  • Location: 0
  • Status: offline
2013/01/02 05:33:53 (permalink)
0

#asm not recognised by MPLAB X or XC8 compiler

I need a few lines of inline assembly code to set up the PPS stuff (Microchip says it must be done this way).  My code was initially written in MPLAB 8 and compiled with C18, no problems at all.  I'm importing it to MPLAB X + XC8 compiler, and can't get the inline assembly to compile.
 
The IDE doesn't recognise the #asm and #endasm directives.  The manual says it should.  The screenshot below shows that the popup doesn't include #asm in the list of allowed directives:

 
You can also see from the code that I've tried the asm("..."); syntax too.  That doesn't work either.  The compiler build errors say that the blank line after each set of asm("..."); statements has a syntax error. But these are blank lines!
 
I'm using the latest versions of everything (just downloaded and installed a couple of days ago).
 
How do I get this to work?
 
Nick.
#1

29 Replies Related Threads

    Z470
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2012/12/26 12:45:39
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 10:36:11 (permalink)
    -1 (1)
    .
    post edited by Z470 - 2013/01/02 10:39:05
    #2
    Guest
    Super Member
    • Total Posts : 80500
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 11:47:53 (permalink)
    0
    I don't think that #asm is what you want in my code using MPLab X with the mcc18 compiler it recognizes _asm for assembly code.  Hope this helps.
    #3
    nickoatley
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/11/15 09:20:59
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 11:59:21 (permalink)
    0
    you are correct regarding the C18 compiler (_asm ... _endasm) - that's what I had previously before I moved the project to MPLAB X / XC8 compiler.  The XC8 compiler doesn't like _asm ... _endasm (I already tried it), and anyway, the documentation says it should be #asm ... #endasm, which doesn't seem to work either.
    #4
    justin.bauer
    Super Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2011/05/16 16:52:08
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 13:49:43 (permalink)
    0
    From the XC8 manual:
     
    2.5.16.3 MIGRATION TO THE CCI
    For 8-bit compilers change any instance of #asm ... #endasm so that each instruction
    in this #asm block is placed in its own asm() statement, for example:
    #asm
    MOVLW 20
    MOVWF _i
    CLRF Ii+1
    #endasm
    to
    asm("MOVLW20");
    asm("MOVWF _i");
    asm("CLRFIi+1");
     
    But it looks like you have already tried this. Are you sure that if you took out all of the legacy keywords that it does not build? I liked using the ASM blocks as well since it was less verbose then typing in the asm("..") every time, so not sure what the reasoning is here.

    post edited by justin.bauer - 2013/01/02 13:52:01
    #5
    nickoatley
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/11/15 09:20:59
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 14:15:01 (permalink)
    0
    I've tried this:

    _asm
    MOVLB 0x0E
    MOVLW 0x55
    MOVWF EECON2, 0
    MOVLW 0xAA
    MOVWF EECON2, 0
    BCF PPSCON, 0, BANKED
    _endasm

    which causes the compiler to complain about a syntax error at the _asm 
     
    and this:

    #asm
    MOVLB 0x0E
    MOVLW 0x55
    MOVWF EECON2, 0
    MOVLW 0xAA
    MOVWF EECON2, 0
    BCF PPSCON, 0, BANKED
    #endasm

    which causes the compiler to complain about a syntax error at the #endasm, and the ide to complain about every line of assembler (can't resolve identifier)
     
    and this:

    asm("MOVLB 0x0E");
    asm("MOVLW 0x55");
    asm("MOVWF EECON2, 0");
    asm("MOVLW 0xAA");
    asm("MOVWF EECON2, 0");
    asm("BSF PPSCON, 0, BANKED");
     

    which causes the compiler to complain about a syntax error on the subsequent blank line in the code!  And subsequent references in this module (main.c to functions etc declared in other files (the peripheral library) are flagged by the ide as "unable to resolve identifier".
     
    The build process fails because of the above "errors".  Surely someone must have got some working inline assembly code in MPLAB X and XC8 compiler????
     
    Nick.
    #6
    justin.bauer
    Super Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2011/05/16 16:52:08
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 17:07:33 (permalink)
    0
    I just tried this:
     
    asm("MOVLW 0x0E");

     
    and it compiled using MPLABX v1.20 and XC8 v1.11
    #7
    mrp
    Super Member
    • Total Posts : 912
    • Reward points : 0
    • Joined: 2012/10/30 01:08:06
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/02 20:54:10 (permalink)
    +1 (1)
    The syntax error you're getting with the #asm and #endasm directives looks like it's caused by the BANKED parameter.  If you change this to a 1 (I assume that '1' = use BSR to select GPR bank = BANKED), then it compiles ok.
     
    #8
    nickoatley
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/11/15 09:20:59
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/03 07:22:16 (permalink)
    0
    ok, I tried that and it certainly seems to have helped.  Unfortunately it still didn't compile for me - the compiler complained about "warning: truncation of operand value (0xebf) to 8 bits" at the #endasm line (or the blank line after the asm("BSF PPSCON, 0, 1"); statement).  Now, 0xEBF is the location of the PPSCON register, which I suppose is location 0xBF in bank 0xE.  I therefore changed "BSF PPSCON, 0, 1" to "BSF PPSCON & 0xFF, 0, 1" and now it seems ok.  This is a bit of a pain though, and didn't have to be done with the C18 compiler.  All of this also means that copying suggested code from the datasheets doesn't work as you have to modify various things to get round deficiencies in the compiler.  How come it worked for you?
     
    Nick.
    #9
    mrp
    Super Member
    • Total Posts : 912
    • Reward points : 0
    • Joined: 2012/10/30 01:08:06
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/03 10:00:08 (permalink)
    0
    I didn't go as far as getting it to work - I was just trying to track down the asm syntax error you were seeing, and I'd previously hit similar problems myself with parameter names.
     
    Here's some more information I just found from this older thread:
    https://www.microchip.com.orums/m686283-print.aspx
    It looks like you can use "B" as an abbreviation for "BANKED".  And it also looks like you can do the whole thing in C and let the compiler handle it.
     
    #10
    nickoatley
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/11/15 09:20:59
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/03 10:20:53 (permalink)
    0
    ok, using "B" for banked looks nicer than "1"; pity it's different to what it was with the C18 compiler (and what the data sheets tell you to do).  The thread you mentioned shows other people having the same problem with truncating things like PPSCON to 8 bits - again it's different behaviour to the C18 compiler and a requirement to do something different to what's in the data sheets that's causing confusion sad
     
    I didn't want to do the PPS stuff in C-code because the data sheet says to do it in assembler as it's time critical.  I did notice that the peripheral library has a C-code implementation of the lock/unlock sequences, but if Microchip say it should be done in assembler, why are they giving us a C-code implementation?  Either the data sheets are wrong, or the library is wrong!
     
    Anyway, I've got that bit working now.  I just have to try and figure out why various other parts of the code don't work any more (like the UART and the USB) even though it all compiles ok and I didn't change anything from the previous MPLAB 8 + C18 version of the project.  Or I could abandon using the XC compiler I suppose as I've already wasted too much time trying to get it to work.
    #11
    RISC
    Super Member
    • Total Posts : 5376
    • Reward points : 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/03 11:00:54 (permalink)
    0
    Hi,
     
    You can do this lock/unlock in a C function with...asm inside ;=)
    In general C18 has peripheral libraries to do that. The C18 libraries implement properly the lock/unlock mechanism as it is used for several peripherals (RTCC, EEPROM, PPS,...)
    I am not sure XC8 does have libraries to do that but you could easily copy/adapt them from C18 as they are available in source format...
     
    regards
    #12
    nickoatley
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/11/15 09:20:59
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/01/03 11:17:09 (permalink)
    0
    yes, but what I was saying was that the peripheral library source that comes with XC8 and C18 has this in pps.h
    #define  PPSUnLock()  				{EECON2 = 0b01010101; EECON2 = 0b10101010; PPSCONbits.IOLOCK = 0;}
    which isn't assembler of course, despite the data sheets saying that it must be.
    #13
    jayhawk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2009/03/13 07:01:47
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2013/06/26 09:58:32 (permalink)
    0
    This weird construct worked for me in MplabX and xc8:
    #asm
    _asm
        nop
    _endasm
    #endasm
     
    This pleases both IDE and compiler.
    #14
    me21
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2011/08/26 01:31:26
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2016/10/24 06:34:36 (permalink)
    +1 (1)
    This post is just to update this thread to the current version (1.38) of XC8 compiler. Even the weird construct from the last post did not work for me. The only way to use inline assembler that worked for me is asm("...");
    It happened to me while building Microchip HID bootloader for PIC18F14K50, in UnlockAndActivate function. The compiler silently ignored the inline assembler, which led to non-working bootloader. However, #asm syntax seems to be working in other places.
    post edited by me21 - 2016/10/24 06:41:33
    #15
    RickyGold
    Starting Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2015/11/02 16:20:07
    • Location: Italy
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2017/06/11 08:38:53 (permalink)
    0
    I've got weird problems too including assembler code in my C source (MPLAB v3.35 with XC8 v1.37 Pro).
    In the same .c file, I have placed asm("goto 0") in three different places... well my problem is that just only 1 of the 3 instances generate an error!
    Or better, generate a red exclamation mark in the editor (unexpected token), but the compiling goes thru without errors... (?!?).
    I've tried to place instead the instructions asm("nop"); and RESET(); with no difference (placing also {} here and there and all the staff).
    Then I tried _asm("goto 0"); and this seems to work... I'm going along with it, albeit I'm a little concerned.
     
    #16
    RickyGold
    Starting Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2015/11/02 16:20:07
    • Location: Italy
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2017/06/11 08:51:18 (permalink)
    0
    Quick update:
    Since _asm("goto 0") worked properly [edit: it DIDN'T compile properly, sorry] , I went to change all the asm("nop") instances to _asm("nop")... but this gave a bunch of compiler errors!
    So I have to use _asm for a "goto 0" and asm for a "nop"...?!?
    This seems a bit messy...
     
    post edited by RickyGold - 2017/06/11 09:02:12
    #17
    NKurzman
    A Guy on the Net
    • Total Posts : 17598
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2017/06/11 08:51:30 (permalink)
    +3 (3)
    MPLabX is know for putting red squiggly lines under good code. If the compiler likes it then it is fine
    #18
    RickyGold
    Starting Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2015/11/02 16:20:07
    • Location: Italy
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2017/06/11 08:59:38 (permalink)
    +1 (1)
    You are right.
    The underscore makes MPLABX happy, but the source didn't compile properly (sorry, didn't realize it before).
    On the other hand, when MPLABX mark an error, the compiler works, and this should be enough for our poor souls...
     
    #19
    jayhawk
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2009/03/13 07:01:47
    • Location: 0
    • Status: offline
    Re:#asm not recognised by MPLAB X or XC8 compiler 2017/06/11 09:11:11 (permalink)
    0
    @RickyGold:
     
    It's hard to advise You, unless You supply some kind of example code, with a description of where Your used syntax works, and where it don't.
     
    What always works for me is this:
     
    void foo() {
      #asm
      _asm
        ; Your assembler code goes here
      _endasm
      #endasm
    }
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5