# asm instruction fails in C18 mplab

Status
Not open for further replies.

#### tubos

##### New Member
Imsk is 8 bit char type
Code:
Imsk = 255 - Imsk;		               // This works
_asm COMF Imsk,1,0 _endasm		// Invert all bits FAILS ??????
Device 18F26K22

##### Banned
What chip are you using, and what's the error?

Last edited:

#### tubos

##### New Member
- chip is 18F26K22 see topic
- There is no error message
but the result is wrong.

##### Banned
COMF Imsk,1,0
That line isn't correct, COMF requires two registers the input register and the output register, you're passing it three values and two of them are literals not valid registers.

Last edited:

#### tubos

##### New Member
I used it as described in the datasheet:

complemented. If ‘d’ is ‘0’, the result is
stored in W. If ‘d’ is ‘1’, the result is
stored back in register ‘f’ (default).
If ‘a’ is ‘0’, the Access Bank is selected.
If ‘a’ is ‘1’, the BSR is used to select the
GPR bank.
If ‘a’ is ‘0’ and the extended instruction
set is enabled, this instruction operates
mode whenever f 95 (5Fh).

##### Banned
I was looking at an older instruction set, my mistake, is the a bit set correctly? I have no idea how C would react to address mode chance. What assurances are there that the C compiler is storing the variable in an appropriate register that the COMF instruction can access?

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
In C18 when accessing local variables (globals are ok) you can't access them by name... you have to work out where they are in the stack

This is from the known problems section of the readme manual.. Inline Assembly (C18-90 / 22170)

Code:
 #include <p18f452.h>

void assignlocals( void )
{
auto char a, b;

_asm

// correctly assigns TMR0L to variable 'a'
movlw a
movff TMR0L, PLUSW2

// DOES NOT correctly assign TMR0L to variable 'b'
movff TMR0L, b

_endasm
}

#### tubos

##### New Member
Thank you that makes a lot of sense now.
Would have been nice if It generated an error.
I wasted a lot of time with this error.
Thanks for pointing that out.

Last edited:

##### Banned
This is what happens when you mix ASM into C. The compiler can't check your ASM code, it assumes you know what you're doing.

#### tubos

##### New Member
I knew what I was doing , but C18/asm did something I didnt want