Menu

ROTATE Problem

Help
joe rocci
2009-07-28
2013-05-30
  • joe rocci

    joe rocci - 2009-07-28

    I'm trying to use ROTATE to rotate all the bytes in a multi-byte variable. My reasoning was to  set up an n-byte array (BYTES(n)) and then do this:

    SET STATUS.C = 0 'Appears to be a necessary undocumented step
    for i = 1 to n
    ROTATE BYTES(i) LEFT
    next i

    This doesn't seem to work,  but if I run it on individual bytes of a non-array set like this, it does work correctly:
    ROTATE BYTE0 LEFT
    ROTATE BYTE1 LEFT
    ROTATE BYTE2LEFT
       .
       .
       .
    ROTATE BYTEn LEFT

    Is it not permissable to ROTATE bytes in an array? What is the algorithmic alternative for a multi-byte rotate?

    Joe

     
    • joe rocci

      joe rocci - 2009-07-28

      In response to my own question, I've determined that attempting to ROTATE the bytes in a byte array doesn't work because the STATUS.C bit gets changed by GCBASIC in the process of it handling the array. I've worked around it by creating a ROTATE sub that preserves and restores the STATUS.C bit. Here it is:

      'The byte array to be shifted must be named "BinaryBytes(n)", and can be of arbitrary size.
      'You call it by including the number of bytes to be rotated in the subroutine call.

      'The byte array must be declared:
      dim BinaryBytes(n) 'Specify the size of the byte array to be shifted

      sub RotateByteArrayLeft (in ByteCount) 
        for i = 1 to (ByteCount*8)      
            'HSerPrintByte i
            'HSerPrint ": "
            for j = 0 to ByteCount-1
                temp = BinaryBytes(j) 'All operations are performed on a copy of the array variable
                if j = 0 then
                   STATUS.C = 0
                else
                   STATUS.C = CarryBit
                end if
                CarryBit = temp.7        
                ROTATE temp LEFT            
                BinaryBytes(j) = temp 'then the copy is stored back in the array variable
                'HserPrintByte BinaryBytes(j)
                'Hserprint" "
            next j
            'HserPrintCRLF     
        next i
      end sub 

      This seems to work for my needs, but comments or suggestions for improvement are appreciated.

      Joe
      The commented-out print statements are only used for debugging purposes.

       
    • Nobody/Anonymous

      i have similar problem. i solved this by asm coding. a little bit later I will try to code in baic again and then will se results. my project is for 19 pin socapex cable tester with pic16ff57.
      I will give additional if necessary when start to rewrite project again.

       
    • joe rocci

      joe rocci - 2009-07-28

      In response to my own question, I've determined that attempting to ROTATE the bytes in a byte array doesn't work because the STATUS.C bit gets changed by GCBASIC in the process of it handling the array. I've worked around it by creating a ROTATE sub that preserves and restores the STATUS.C bit. Here it is:

      'The byte array to be shifted must be named "BinaryBytes(n)", and can be of arbitrary size.
      'You call it by including the number of bytes to be rotated in the subroutine call.

      'The byte array must be declared:
      dim BinaryBytes(n) 'Specify the size of the byte array to be shifted

      sub RotateByteArrayLeft (in ByteCount) 
        for i = 1 to (ByteCount*8)      
            'HSerPrintByte i
            'HSerPrint ": "
            for j = 0 to ByteCount-1
                temp = BinaryBytes(j) 'All operations are performed on a copy of the array variable
                if j = 0 then
                   STATUS.C = 0
                else
                   STATUS.C = CarryBit
                end if
                CarryBit = temp.7        
                ROTATE temp LEFT            
                BinaryBytes(j) = temp 'then the copy is stored back in the array variable
                'HserPrintByte BinaryBytes(j)
                'Hserprint" "
            next j
            'HserPrintCRLF     
        next i
      end sub 

      This seems to work for my needs, but comments or suggestions for improvement are appreciated.

      Joe
      The commented-out print statements are only used for debugging purposes.

       
    • joe rocci

      joe rocci - 2009-07-29

      In response to my own question, I've determined that attempting to ROTATE the bytes in a byte array doesn't work because the STATUS.C bit gets changed by GCBASIC in the process of it handling the array. I've worked around it by creating a ROTATE sub that preserves and restores the STATUS.C bit. Here it is:

      'The byte array to be shifted must be named "BinaryBytes(n)", and can be of arbitrary size.
      'You call it by including the number of bytes to be rotated in the subroutine call.

      'The byte array must be declared:
      dim BinaryBytes(n) 'Specify the size of the byte array to be shifted

      sub RotateByteArrayLeft (in ByteCount) 
        for i = 1 to (ByteCount*8)      
            'HSerPrintByte i
            'HSerPrint ": "
            for j = 0 to ByteCount-1
                temp = BinaryBytes(j) 'All operations are performed on a copy of the array variable
                if j = 0 then
                   STATUS.C = 0
                else
                   STATUS.C = CarryBit
                end if
                CarryBit = temp.7        
                ROTATE temp LEFT            
                BinaryBytes(j) = temp 'then the copy is stored back in the array variable
                'HserPrintByte BinaryBytes(j)
                'Hserprint" "
            next j
            'HserPrintCRLF     
        next i
      end sub 

      This seems to work for my needs, but comments or suggestions for improvement are appreciated.

      Joe
      The commented-out print statements are only used for debugging purposes.

       
    • joe rocci

      joe rocci - 2009-07-29

      In response to my own question, I've determined that attempting to ROTATE the bytes in a byte array doesn't work because the STATUS.C bit gets changed by GCBASIC in the process of it handling the array. I've worked around it by creating a ROTATE sub that preserves and restores the STATUS.C bit. Here it is:

      'The byte array to be shifted must be named "BinaryBytes(n)", and can be of arbitrary size.
      'You call it by including the number of bytes to be rotated in the subroutine call.

      'The byte array must be declared:
      dim BinaryBytes(n) 'Specify the size of the byte array to be shifted

      sub RotateByteArrayLeft (in ByteCount) 
        for i = 1 to (ByteCount*8)      
            'HSerPrintByte i
            'HSerPrint ": "
            for j = 0 to ByteCount-1
                temp = BinaryBytes(j) 'All operations are performed on a copy of the array variable
                if j = 0 then
                   STATUS.C = 0
                else
                   STATUS.C = CarryBit
                end if
                CarryBit = temp.7        
                ROTATE temp LEFT            
                BinaryBytes(j) = temp 'then the copy is stored back in the array variable
                'HserPrintByte BinaryBytes(j)
                'Hserprint" "
            next j
            'HserPrintCRLF     
        next i
      end sub 

      This seems to work for my needs, but comments or suggestions for improvement are appreciated.

      Joe
      The commented-out print statements are only used for debugging purposes.

       
    • Nobody/Anonymous

      I solve tris prmgles in a differen way. the idea:
      dim savedcarrey as bit

      init part:
      set savesarrey on 'or off dependind on your project

      main loop begin:

      your stuff

      status.c=savedcarrey
      rotate var1
      rotate var2
      rotate var3
      savecarrey=status.c

      goto main loop

      the idea is to set saved carrey flag on init par depending on your needs.

      on rotate restore status.c from saved carrey
      do rotation
      afret rotation set, reset or save savedcarrey form status.c depending on your project.
      by this I made 19 bit shfting LED tester for SCAPEX cables.
      i will post full code when I will made some minor changes on it.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.