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

Page: < 12 Showing page 2 of 2
Author
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:15:43 (permalink)
+1 (1)
I suspect that the preprocessor and the compiler do not totally agree on this.
 
As You can see from my 8 year old post above, this problem has been quite persistent.
#21
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 10:33:50 (permalink)
0
me21
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.


I'm using Mplab X Ide v3.61 together with XC8 v1.42 on Ubuntu 16.04.
And I have had no change in the weird behavior regarding assembler code in c functions for many years.
 
Actually I write all my code in assembler, and only use c code as a framework to structure my programs and to pass arguments to functions.
 
I am beginning to suspect, that the cause, that some people cant get assembler code to work inside c functions may be related to setup of the building tools.
 
Be sure that in:
Tools -> Options -> Embedded -> Build Tools
(choosing XC8 at the left)
both Compiler and Assembler
Are pointing at the same. In my situation: "/opt/microchip/xc8/v1.42/bin/xc8"
Yes, compiler and assembler is the same program.
--
Maybe some folks have made these two point to different things: compiler to xc8 and assembler to mpasm?(?)
#22
Hairyloon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2014/12/18 06:33:07
  • Location: 0
  • Status: offline
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/21 13:35:22 (permalink)
0
Did anybody manage to resolve this one?
#23
Mysil
Super Member
  • Total Posts : 3206
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/21 17:07:49 (permalink)
+1 (1)
Hi,
Have been using assembly inline in C code for 8-bit PIC many times.
 
Be aware that the assembler used within XC8 is ASPIC or ASPIC18 which are parts of the compiler installation,
and not MPASM and there are some differences.
Syntax checker in MPLAB X do not much like inline assembly, and may make lots of red squigglies.
 
Here is a sample:

/*
 *  PIC16, PIC18
 *    Calculate average from a running accumulator  Avg = Avg >> 10;
 *    Modified from code contributed on Microchip forum by "co_worker"
 *  containing suggestions from "1and0" and "andig"
 *  Microchip Forum http://www.microchip.com/forums/FindPost/848067
 */
#include <xc.h>
#include <stdint.h>

#define IN_ASM
#ifndef     Avgshift
    #define Avgshift    10
#endif
/*
 * Unsigned integer shift
 *
 *   Result = Arg >> 10;
 *
 * Parameters:
 *   a = 24-bit unsigned integer accumulating samples.
 *
 * Return Value:
 *   The function returns a 16-bit unsigned integer Result.
 *
 * Program Memory:
 *   20 instructions including call and return
 *
 * Execution Time:
 *   14 cycles excluding the call (prolog) and return (epilog).
 *
 * Remarks:
 *
 *
 * History:
 *      2016-05-04                            - by Arne Bergseth "Mysil"
 */
uint16_t uShift_24_10(uint24_t Arg)
{
    uint16_t Result = 0;    /* Zero to make compiler happy, */
                             /* zeroing may be omitted if compiler warning is acceptable. */
#if defined(IN_C)
    result  = a >> avgshift;
#endif

#if defined(IN_ASM) /* ? instructions, ? cycles, ? avg ? to be tested. */
/*
 * Ma.
 *
 * rh:rl = (au:ah:al >> 10)
 *
 */
#asm
#ifdef _PIC18
    #define rh (uShift_24_10@Result+1)
    #define rl (uShift_24_10@Result)
    #define au (uShift_24_10@Arg+2)
    #define ah (uShift_24_10@Arg+1)
    #define al (uShift_24_10@Arg)

#elif  _PIC14 || _PIC14E
    #define rh (uShift_24_10@Result+1)&0x7F
    #define rl (uShift_24_10@Result)&0x7F
    #define au (uShift_24_10@Arg+2)&0x7F
    #define ah (uShift_24_10@Arg+1)&0x7F
    #define al (uShift_24_10@Arg)&0x7F
#endif
    BANKSEL(uShift_24_10@Arg)
                    /*    rh:rl = au:ah >> 1    Do 8 bit shift by picking bytes
                     *                            then 1 bit shift by instruction */
    bcf        STATUS,0    /* clear the carry bit. */
#if (Avgshift == 8)        /* Do 8 bit shift by picking bytes . */
    movf    au,W
    movwf    rh
    movf    ah,W
    movwf    rl
#elif (Avgshift > 8)    /* then 1 bit shift by instruction. */
    rrcf     au,W        /* Move from Au into W */
    movwf    rh            /* store in Rh */
    rrcf    ah,W
    movwf    rl
#endif
#if (Avgshift == 10)    /* ru:rh:rl = ru:rh:rl >> 1 */
    clrc                /* one more rotation for 10 bit shift */
    rrcf    rh,F
    rrcf    rl,F        /* Shift into rl from the carry bit */
#else
    movf    rl,W        /* start with low byte */
#endif   

#endasm
#endif
    return Result;
}

 
Have Not tried this with C99 version of XC8.
 
    Mysil
#24
Hairyloon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2014/12/18 06:33:07
  • Location: 0
  • Status: offline
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/21 17:31:36 (permalink)
0
Alas that just gives me errors:
make[2]: *** [build/default/production/test.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
test.c:54:2: error: invalid preprocessing directive
#asm
 ^
test.c:92:2: error: invalid preprocessing directive
#endasm
 ^
2 errors generated.
(908) exit status = 1

#25
qhb
Superb Member
  • Total Posts : 9202
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/21 17:44:04 (permalink)
+1 (1)
Haryloon, the problem with hijacking someone else's topic like this is that you have not told us which version compiler you are using, and in which mode (C90, C99?)
Do you have CCI mode enabled?
Please show the entire C file you used to test this feature.
 
 
#26
Hairyloon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2014/12/18 06:33:07
  • Location: 0
  • Status: offline
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/22 03:20:23 (permalink)
0
Hardly a hijack when it's been all but dead for five years.
I'm using the XC8 compiler with the C90 libraries.
I didn't have CCI mode enabled, but I do now & it makes no discernible difference.

#include <xc.h>
void main(void) {
    _asm
      nop
    _endasm          
    return;
}

I've tried #asm instead of _asm. #asm gives "invalid preprocessing directive", _asm gives  use of "undeclared identifier".
 
 
#27
qhb
Superb Member
  • Total Posts : 9202
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/22 04:27:49 (permalink)
+1 (1)
Hairyloon
I'm using the XC8 compiler with the C90 libraries.

That only sets the libraries.
Go back to "XC8 Global Options", and change "C Standard" from "C 99" to "C 90",
and it will accept "#asm".
 
Note this statement from C:\Program Files (x86)\Microchip\xc8\v2.00\docs\Readme_XC8_for_PIC.htm

In-line assembly The #asm ... #endasm form of inline assembly is no longer accepted when building for C99. The asm() form of inline assembly is now the only way to embed assembly instructions inline with C code.

 
 
#28
Hairyloon
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2014/12/18 06:33:07
  • Location: 0
  • Status: offline
Re:#asm not recognised by MPLAB X or XC8 compiler 2018/12/22 05:37:39 (permalink)
0
Excellent. Thank you very much
#29
crosland
Super Member
  • Total Posts : 1525
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re:#asm not recognised by MPLAB X or XC8 compiler 2019/01/18 13:14:20 (permalink)
+1 (1)
What do you recommend for building a C99 bootloader with the recommended:

 
 
 
#define PROG_START 0x1000
 
 
 
#asm
PSECT intcode
GOTO PROG_START+0x8
PSECT intcodelo
GOTO PROG_START+0x18
#endasm
 

 
?
 
The asm() form does not substitute #defined macros, leading to error-prone replicatipn of constants that should be defined once.
 
I found the answer - ___mkstr() :)
post edited by crosland - 2019/01/18 13:35:38
#30
Page: < 12 Showing page 2 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5