Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Assembly Code inside MicroC program

Not open for further replies.


Member level 2
Feb 4, 2013
Reaction score
Trophy points
Activity points

Windows, MPLAB X IDE v6.05, XC8 v2.41

I have tried many internet suggestions but haven't been successful at including some assembly language inside a MicroC program.

I have tried selecting the pic-as the compiler but no luck.

As a one time assembly coder I would like to learn how to do this.

Any suggestions of examples?

MikroC or XC8? Both tools support embedding assembly code in C source.
The mikroC PRO for PIC allows embedding assembly in the source code by means of the asm declaration.
With XC8, you can write pure assembler code projects.

I have made some headway, thanks to hints here, but the following creates an "error: (800) undefined symbol" on line 11. Without line 11 it compiles and loads.

1 /* File: Asm.c */
3 #include <xc.h>
4 #include "config.h"
6 unsigned int x;
8 void main(void) {
9 x = 1;
10 asm("movlw 0xAA");
11 asm("movlw x");
12 return;
13 }

Also I would like to use:
asm {
instead of having to use asm("xxxx x"); on every line but XC8 doesn't like that.


I don't know XC8 but I do know assembler, "movlw x" on line 11 is not a valid assembly instruction because "x" has not been defined. you should try asm("x EQU 1); on line 9

I suggest that you read the User Guide for the XC8 compiler
It shows that there that the 'C' variable name is pre-pended with an underscore character so you need to access the 'C' "x" variable as "_x" in the assembler.
There is a lot of other useful information in teh user gudie.

Your suggestion that I try asm("x EQU 1"); worked. It seems that is a way to initiate a variable. I just don't know what kind of variable it is. This turns out to be the tip I needed to get going.

I'm sure I'll be back with more questions.


I am able to include inline assembly code in my main.c file using asm("....."); but can't can't find how to use inline blocks of assembly code.

I've tried:






but all yield a syntax error?

Any suggestions?

Again the User Guide shows how you can have a block of assembler code - put it in as a function in a file with the .s (or .S depending on how you want the file assembled) file type.
The user guide only shows one way to put in-line assembler code in a .c file and therefore trying other ways won't work (as you have found out).
Aa the song says "Just 'cos that's how you want it doesn't mean it will be so"

Aussie Susan,
Thanks for the tip about using _var. Of course it worked.

About the inline assembly. I created a .s file and added it to my project. I put an empty function in it and it assembles. I can add simple commands like movlw 1 to the function but if I try to address hardware it complains. I tried to #include my config.h file but the assembler didn't like that. Still it's progress.

I'm just beginning to find my way around the User Guide. I think I complied to section 4.12.1 in the User Guide accept I couldn't find an appropriate PSECT in section 4.15.1.

Here's the function:
#include <>
GLOBAL _match

movlb 0
movlw 1;
//movwf PORTC

Next problem will be how to call it from the C file.


I have my program working with the assembly code in this separate .s file.

#include <>
GLOBAL _function
PSECT text0,class=CODE,reloc=2

movlw 0b00000011
movwf PORTC

The only problem is when I run the assembler I get:
warning: (1522) RAM access bit operand not specified, assuming access-bank

Any suggestions?

BANKSEL (PORTC) is not a valid assembly command, I don't know what you are trying to do (nor does the assembler)

Then look at the next line of the example where it uses the 'BANKMASK' instruction.
These only PIC MCUs had a banked memory arrangement and you needed to set the 'bank' selection bits for many of the variable memory locations and SFRs. (Some were mapped across all banks so you could access them from anywhere in the address space.)
I can't see anywhere where you have said which MCU you are using but the data sheet for it will show the memory layout and explain the BANKSEL operation.
By the way, I'm not sure why you want to use assembler. The XC8 compiler takes care of all of the issues you have had if you wrote in C. The only time I can see a value in using assembler is when you need to guarantee the order of instructions (i.e. in any 'unlock' sequence) or when you have already discovered that there is a performance issue with a specific section of your code.

Aussie Susan,
Thanks for your replies.

The MCU is a pic18f2550 because it has exactly the number of ports and interrupts I needed.

I have written this program in MicroC and MicroBasic while teaching my grandson about coding.

I started out writing pseudo assembly in Silicon Valley a long time ago and just wanted the exercise.

Do you have any idea about the warning I'm getting on the movwf PORTC instruction?


Look at Section 5.3 of the datasheet for that MCU and you can see the memory bank system that it uses.
As for the warning, look at Section 26 for a summary of the instructions. In Table 26-2 you will see that the 'movwf' instruction takes 2 arguments, the 'f' register that is the destination, and the 'a' value hat is explained in Table 26-1.

Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to