CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

dsPIC: Trouble getting ASM load/modify/store working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
icefield



Joined: 09 May 2005
Posts: 20
Location: Canada

View user's profile Send private message

dsPIC: Trouble getting ASM load/modify/store working
PostPosted: Thu Sep 09, 2010 10:13 am     Reply with quote

The C code below compiles to 6 or 11 instructions for a load/modify/store and this is a time-sensitive operation. Instead, I want to do it with 4 instructions in assembly. But, for some reason, I cannot get CCS (4.112) to generate the elusive MOV.B W0L,2D4. Presumably this is coded as MOV.B W0, REG_PORTD_LSB, but the compiler generates an "Expecting an opcode mnemonic" error.

Code:
#use delay(clock=20000000)

#byte REG_LATD_LSB = 0x2D6
#byte REG_PORTD_LSB = 0x2D4

void main()
{
   unsigned int8     u8PortReg, u8Which;
         
// C Method #1
   u8PortReg = REG_LATD_LSB;     // read latched output value
   u8PortReg &= 0xf8;
   u8PortReg |= u8Which;
   REG_PORTD_LSB = u8PortReg;
   
// C Method #2
   REG_PORTD_LSB = (REG_LATD_LSB & 0xf8) | u8Which;

// ASM Method
#asm
   MOV.b    REG_LATD_LSB, W0
   AND.b    #0xF8, W0
   IOR.b    u8Which, W0
   MOV      W0, REG_PORTD_LSB
#endasm
}


The assembled code is as follows. The instruction at 00226 is the one I want to generate, but cannot get the compiler to produce - I can only get the line at 0023A, which is NOT what I want.

Code:
.................... // C Method #1
....................    u8PortReg = REG_LATD_LSB;     // read latched output value
00212:  MOV.B   2D6,W0L
00214:  MOV.B   W0L,801
....................    u8PortReg &= 0xf8;
00216:  MOV     800,W4
00218:  LSR     W4,#8,W4
0021A:  AND.B   #F8,W4L
0021C:  MOV.B   W4L,W0L
0021E:  MOV.B   W0L,801
....................    u8PortReg |= u8Which;
00220:  MOV.B   802,W0L
00222:  IOR.B   801
....................    REG_PORTD_LSB = u8PortReg;
00224:  MOV.B   801,W0L
00226:  MOV.B   W0L,2D4
....................     
.................... // C Method #2
....................    REG_PORTD_LSB = (REG_LATD_LSB & 0xf8) | u8Which;
00228:  MOV     2D6,W5
0022A:  AND     #F8,W5
0022C:  MOV.B   802,W0L
0022E:  MOV.B   W0L,2D4
00230:  MOV     W5,W0
00232:  IOR.B   2D4
.................... 
.................... // ASM Method
.................... #asm
....................    MOV.b    REG_LATD_LSB, W0
00234:  MOV.B   2D6,W0L
....................    AND.b    #0xF8, W0
00236:  AND.B   #F8,W0L
....................    IOR.b    u8Which, W0
00238:  IOR.B   802,W0L
....................    MOV      W0, REG_PORTD_LSB
0023A:  MOV     W0,2D4
.................... #endasm


Does anyone know how to get the compiler to generate the magic code at 00226?

Cheers!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Sep 09, 2010 12:28 pm     Reply with quote

Don't know why PCD assembler refuses to translate legal asm syntax arbitrarily. My solution is to use register variables in C code, either together with assembler instructions, or without it, as shown below:
Code:
#byte W0L=0x00

W0L = REG_LATD_LSB;
W0L &= 0xF8;
W0L ^= u8Which;
REG_PORTD_LSB = W0L;
icefield



Joined: 09 May 2005
Posts: 20
Location: Canada

View user's profile Send private message

dsPIC: Trouble with ASM load/modify/store [resolved (sort of
PostPosted: Fri Sep 10, 2010 3:33 pm     Reply with quote

Most excellent! It works.

Now, if CCS would just get their inline assembler to accept legal mnemonics, we'd be spared this bother! It boggles my mind that the compiler can generate the very instructions it refuses to parse.

Many thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sat Sep 11, 2010 3:01 am     Reply with quote

The question is "who's" legal mnemonics?....

CCS assembler, is not Microchip assembler. They have their own syntax, that is nearer to C in a lot of places. The big problem is not that it doesn't accept other people's mnemonics, but that they don't document what it does accept.... Crying or Very sad

It really calls for a separate assembler pdf, with details of the accepted instructions for PCM (most known), PCH (ditto), and PCD (still large gaps...), and syntax details for things like accessing addresses etc..

However, it has become less and less necessary, with 98% of stuff that used to require assembler, now being directly do-able in the C.

Best Wishes
RenSeven



Joined: 28 Oct 2010
Posts: 5

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 2:45 pm     Reply with quote

Quote:
The question is "who's" legal mnemonics?....


Not trying to be snarky, but the answer to that can be had in the help file for the error message:

Quote:
Expecting an opcode mnemonic
This must be a Microchip mnemonic such as MOVLW or BTFSC.


And while most stuff might be do-able in C, very tight loops where every cycle counts, probably needs assembly. I completely concur on the need for a separate PDF!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:35 pm     Reply with quote

Quote:
Not trying to be snarky, but the answer to that can be had in the help file for the error message.

Expecting an opcode mnemonic
This must be a Microchip mnemonic such as MOVLW or BTFSC
.

I suggest, to compile the said code yourself before jumping to conclusions, and you hopefully understand, what's the original poster talking about. Just as a hint, the code is using a legal mnemonic, but it's not understood by the compiler anyway. Unfortunately, not everything can be explained from the manual, even when reading it thoroughly.
RenSeven



Joined: 28 Oct 2010
Posts: 5

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 7:05 pm     Reply with quote

Not sure what conclusions you think I'm jumping to. I found this thread because I am trying to assemble the same mnemonics and getting the same error codes as the OP. So I guess I did compile the said code myself. I believe Ttelmah's point was that CCS uses their own mnemonics, and I simply pointed out that their help file ironically claims they use Microchip's mnemonics.

FWIW (thanks to DR @ CCS support) the (or a) correct syntax is:
Code:
MOV      REG_PORTD_LSB

...with the W0 implied. I think that answers the OP question. At least it does for me on PIC24.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 11:49 pm     Reply with quote

Sorry for misinterpreting your statement. Others should decide, if it can be easily understood as an ironic comment about the confuse CCS documentation.

It seems to me, that the piece of information given in the laconic FWIW would be much more worth a posting than your first cryptic attempt.

I basically agree to your conclusion:
Quote:
I completely concur on the need for a separate PDF!

As a first step, CCS may want to make the manual part describing the assembler syntax complete and free of errors.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Fri Oct 29, 2010 9:34 am     Reply with quote

First, CCS's error messages have little to do with real errors. If you have used the compiler for a while, you will have found that 90% of the time, the error message actually doesn't reflect where the real error lies...

Now, the point about the CCS assembler, is that it's 'core syntax', is not Microchip. Unfortunately, this syntax is almost completely undocumented. The manual section is a joke, and most of the more sophisticated constructs have been discovered by past experimentation, hints from CCS etc..
As you see from what you have now found to be the correct syntax, it was not the actual mnemonic that was the problem, but the syntax of the line after it, with CCS 'implying' W0, without having to be told.

Let's all start to ask for better documentation here. Whenever an answer like the one you now have is given, 'ask back' for better documentation. Eventually, someone at CCS may actually listen.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group