Bitwise rotate left|right in mikropascal PRO for PIC ???

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
rainer
Posts: 320
Joined: 07 Dec 2006 11:00
Location: Vienna, Austria
Contact:

Bitwise rotate left|right in mikropascal PRO for PIC ???

#1 Post by rainer » 24 Mar 2024 09:47

Hello.
I noticed right now that such basic function as
Rotate left|right
appears to be missing in mikroPascal PRO for PIC?
Do I really have to code that by hands? I cannot believe that this is really the case, I possibly only searched for th wrong name.
This is one of the instructions literally every microcontroller can do, and mikroPascal doesnÄt have it implemented?

Please, can you tell me the name of this command?

If it is really necessary to manually implement that, I would like to do this by a few assembly lines. But I never ever used assembly language within Mikropascal, so I need help her.
If I want to bitwise rotate a byte-var, how is the assembly code for that?

Code: Select all

var
  bytevar   ; byte;

function RolL( b: byte): byte;
begin
  // Use some assembly on "b" to rotate it 1 bit to the left
  // Highest bit becomes lowest bit hereby, all other bits shift left
  asm
    ???    // No ideas what to do
  end;
  RESULT:=b;  // Return the result
end;
Because I never used assembly, I need your help.
How do I access the variable "b" from assembly at all?
What is the command for rotating the bits?

Thanks.

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: Bitwise rotate left|right in mikropascal PRO for PIC ???

#2 Post by IvanJeremic » 24 Mar 2024 10:31

Hi,

Try something like the code below:

Code: Select all

// CODE THAT DOES FULL ROTATION OF VARIABLE,
// TAKING CARE OF SETTING THE CARRY BIT.
// CODE IS A LITTLE BIGGER BECAUSE IT IS CLEAN
// WITHOUT GLICH (BIT TOGGLE EFFECT), CAN BE USED ON PORTS
void main(){
  if (variable) // dymmy if to set bank for variable
  ;
  asm{
startRotate:
    RLF  _variable, 1
    BTFSS STATUS, C
    GOTO clrBit
    BSF _variable, 0
    GOTO endRotate
clrBit:
    BCF _variable, 0
endRotate:
  }
}
Regards,

Ivan.

rainer
Posts: 320
Joined: 07 Dec 2006 11:00
Location: Vienna, Austria
Contact:

Re: Bitwise rotate left|right in mikropascal PRO for PIC ???

#3 Post by rainer » 24 Mar 2024 11:14

Hi, Ivan.

Thanks for the code,

So the PIC controller indeed has no direct assembly command for rotating a byte in itself? I am a bit surprised. :)

At the moment I wrote this functions code in Pascal:

Code: Select all

function RotL(b: byte; n: byte): byte;
var Carry: byte;
begin
  while n>0 do  // Loop for <n> passes
  begin
    Carry:=b shr 7;        // Keep highest bit of b in bit 0 position of Carry
    b:=(b shl 1) or Carry; // add the saved bit
    dec(n);
  end;
  result:=b;
end;

function RotR(b: byte; n: byte): byte;
var Carry: byte;
begin
  while n>0 do
  begin
    Carry:=b shl 7;        // Keep lowest bit of b in bit 7 position of Carry
    b:=(b shr 1) or Carry; // Add bit
    dec(n);
  end;
  result:=b;
end;
Will for sure work, and also allows to say how many bits to rotate. I also didn't think about saving possibly a few clock cycles by or to optimize the code by putting everything in one command line (without using the Carry var. I hope the compiler will do that by itself.
Do you think that "b:=(b shl 1) or (b shr 7)" gives smaller code?

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: Bitwise rotate left|right in mikropascal PRO for PIC ???

#4 Post by IvanJeremic » 07 Apr 2024 07:52

Hi,

"Do you think that "b:=(b shl 1) or (b shr 7)" gives smaller code?" Both achieve the same thing and none necessarily make the code smaller.

Regards,

Ivan.

Post Reply

Return to “mikroPascal PRO for PIC General”