6
\$\begingroup\$

In a PIC24FV32KA302 C code, I want to add inline assembly. For this, I use, for instance, __asm("BSET PORTB,8");.

When compiling, I get the following error message :

"Error: Invalid operands specified ('bset PORTB,8'). Check operand #0."

If I change "PORTB" by "0x2CA", I get the same error message.

Using XC16 with MPLABX.

What am I doing wrong?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Hello ... can I ask what compiler and assembler you're using? And version? \$\endgroup\$ Commented Jul 15 at 13:16
  • 2
    \$\begingroup\$ You have not told which compiler environment you are using, shown the C code, or other details. Why do you want to use inline asm and what makes you believe it can be used - do you have an example you are following? \$\endgroup\$ Commented Jul 15 at 13:34

1 Answer 1

12
\$\begingroup\$

This:

asm volatile ("BSET PORTB, #8"); 

should work if you are using the assembler from the xc16 compiler.
It's a syntax problem. The bit number is an immediate operand and not an address, so you need the #in front of the 8.
Here is a link to the xc16 userguide. See section 16 for information about the assembler.

\$\endgroup\$
3
  • \$\begingroup\$ Can you provide a reference to e.g. the compiler manual? Can you explain why this works while the OP's example does not? \$\endgroup\$ Commented Jul 16 at 7:32
  • 2
    \$\begingroup\$ It seems the reason is just the #: the operand is an immediate, not an address. So just syntax of specific assembler \$\endgroup\$ Commented Jul 16 at 8:21
  • 2
    \$\begingroup\$ The prima facia error is from the lack of an octothorpe to mark the operand as immediate, as this answer correctly indicates. The 'volatile' keyword is also recommended to keep the optimization from messing with the inline assembly. There's an extended form of inline assembly code that uChip encourages the use of.. details in the XC16 User's Guide \$\endgroup\$ Commented Jul 16 at 13:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.