Helpful ReplyMPLAB C30: Inline assembly problems

Author
rinke
Super Member
  • Total Posts : 181
  • Reward points : 0
  • Joined: 2003/11/07 12:35:59
  • Location: Germany
  • Status: offline
2012/01/26 09:33:53 (permalink)
0

MPLAB C30: Inline assembly problems

Hello world,
 
I am trying to port some assembler code from a PIC 18 to some c code for dsPIC33.
 
The code just reverses a byte by shifting left an right 8 times over the carry flag like this:
 
 
       Rrcf  bData, F   ; LSB to Carry
 Rlcf  rByte, F   ; Carry to LSB    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;    Rrcf  bData, F   ;
 Rlcf  rByte, F   ;  

 
... where bData is the input byte and rByte is the reversed output byte. This is not very sophisticated but fast and it works.
 
Now i want to do the same with some inline assembly lines in c, and get totally lost because I don`t understand the manual (yes: I read it(:-)!
 
I tried it like this:
 
 
      unsigned char Ds1820ReverseByte(unsigned char bData)
{
 unsigned char rByte = 0;
 unsigned char  oByte = 0;
 
 oByte = bData;  __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    __asm__ volatile("rrc.b %0" : "+r"(oByte));
 __asm__ volatile("rlc.b %0" : "+r"(rByte));    return(rByte);
}  

 
But I get 8 times
 
"Error: Invalid operands specified ('rrc.b w0'). Check operand #1."
"Error: Invalid operands specified ('rlc.b w0'). Check operand #1."
 
What am I doing wrong?
 
After solving this prob I would try to put this into a DO LOOP, but first I have to get rid of these errors...
 
Thanks for any help,
 
Wolfgang
 
P.S.: Don´t know why this code tag doesn´t work as expected, I didn´t write two instructions in one line...
#1
MBedder
Circuit breaker
  • Total Posts : 6203
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/26 09:58:51 (permalink)
0
Have you ever read the dsPIC Programmer's Reference Manual? What does it say about the RRC instruction syntax?
#2
eskopal
Super Member
  • Total Posts : 281
  • Reward points : 0
  • Joined: 2003/11/07 12:35:18
  • Location: Ossining, New York
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/26 10:09:08 (permalink)
0
If you change the instruction to rrc.b %0,%0 the compiler understands what you want. 
 
 unsigned char Ds1820ReverseByte(unsigned char bData)     { 
    unsigned char  oByte = 0;
    __asm__ volatile("rrc.b %0, %0" : "+r"(bData));
    __asm__ volatile("rlc.b %0, %0" : "+r"(oByte));
    // Repeated 7 times     return (oByte); }  
 
 
There are two definitions for RRC (rrc f{,wreg] and rrc ws, wd).  When the compiler uses w0, the assembler is looking for the second version of the instruction, which needs two registers.
..Eugene..
 
post edited by eskopal - 2012/01/26 13:10:54
#3
MBedder
Circuit breaker
  • Total Posts : 6203
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/26 11:55:46 (permalink)
0
What a gibberish!
#4
eskopal
Super Member
  • Total Posts : 281
  • Reward points : 0
  • Joined: 2003/11/07 12:35:18
  • Location: Ossining, New York
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/26 13:31:31 (permalink) ☄ Helpful
0
MBedder
What a gibberish!

You are correct.  I am used to dropping directly into assembler to do this sort of work (vs __asm__ statements).  I have cleaned up and simplified my response above.  Interestingly,  "rrc 0" works as expected.   The assembler complains with "rrc w0" but, since w0 = 0 you would expect it to work.  The programming manual indicates that the 'f' argument can be zero.  Sometimes, the assembler can be too helpful... 
 
As I mentioned above, "rrc w0,w0" works.
 
..Eugene..
#5
rinke
Super Member
  • Total Posts : 181
  • Reward points : 0
  • Joined: 2003/11/07 12:35:59
  • Location: Germany
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/27 02:04:34 (permalink)
0
Yes, it works, thanks a lot! Smile
 
Maybe its a gibberish, it is just that I´m coming from many years PIC 8 bit assembler, I´m new to dsPIC assembler, new to inline assembly, and tried to understand the mechanism how to pass c variables to assembler. A next step will probably be to check out using assambler functions in c...
 
For avoiding register usage (read something about clobbering and how to avoid it without understanding it in detail), I wanted the inline assembly lines NOT to use registers but the file directly, and I didn´t, well, still don´t know how to tell the compiler what I want it to do. I see, that the solution works, but don´t exactly know why.
 
So please be patient with me, ´m still learning...
 
best regards
 
Wolfgang
#6
MBedder
Circuit breaker
  • Total Posts : 6203
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/27 03:05:19 (permalink)
0
I said "gibberish" to Eugene's original post before he edited it. I meant the valid RRC synax is rrc Ws,Wd, not just rrc Wn as he initially stated.
#7
rinke
Super Member
  • Total Posts : 181
  • Reward points : 0
  • Joined: 2003/11/07 12:35:59
  • Location: Germany
  • Status: offline
Re:MPLAB C30: Inline assembly problems 2012/01/27 05:52:07 (permalink)
0
Ah, I see!
 
Didn´t read Eugene´s original post because he had already edited it when I came online.
 
Wolfgang
#8
Jump to:
© 2017 APG vNext Commercial Version 4.5