Move seperate bytes into a word


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    "Z" holds
    A= 0-3,
    B= 4-7,
    C=8-11,and
    D=12-15.
    First thing is to isolate each 4LSB of each variable. Easy way, using boolean AND (&)
    Code:
    A = A & $0F
    B = B & $0F
    C = C & $0F
    D = D & $0F
    Now, you need to shift some bits of your Bytes variables. This is done using left shift (<<). Check your manual about that. Knowing that, we could modify the above like
    Code:
    A = A & $0F
    B = B << 4  
    C = C & $0F
    D = D << 4
    Now you need to combine A & B, and C & D, then the whole thing together. Boolean OR do the trick
    Code:
    A = (A & $0F) | (B <<4)  ' a hold A & B
    C = (C & $0F) | (D <<4)  ' C hold C & D
    Then you need to send A and C where they're suppose to go in Z
    Code:
    Z.LowByte = A
    Z.HighByte = C
    A is now LSB of Z(0-7), C is now MSB of Z(8-15).

    This could be shorten like
    Code:
    Z.LowByte = (A & $0F) | (B <<4)  
    Z.HighByte = (C & $0F) | (D <<4)
    If you're REALLY SURE you NEVER EVER mess with bits <7:4> of your variables, then yes, this could reduce to
    Code:
    Z.HighByte = (D<<4) | C
    Z.LowByte = (B<<4) | A
    OR
    Code:
    Z = (D<<12) | (C<<8) | (B<<4) | A
    I see other ways, but this should be more than enough to start
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    One step further..
    Code:
    Z VAR WORD
    A VAR Z.LOWBYTE
    B VAR BYTE
    C VAR Z.HIGHBYTE
    D VAR BYTE
    
    '
    '    Some other code where you set A, B, C & D BYTE variables
    '
    
    Z.HIGHBYTE = Z.HIGHBYTE | (D<<4)
    Z.LOWBYTE = Z.LOWBYTE | (B<<4)
    ' Now you have everything in Z... easy huh?
    or.. yet another
    Code:
    Z = Z | (B<<4) | (D<<12)
    Enjoy
    Last edited by mister_e; - 11th November 2008 at 23:10.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    I've tried Ioannis suggestion and it seems to work fine, I'll will try the others to. I do not plan to ever use bits 7-4 so I will give these a try. I am just playing around trying to see what does and does not work. I learned something new and appreciate all the help.

    Again, Many Thanks

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    Steve's solutions cover the case that high nibble could hold some bits at 1 and masks to zero. Mine is based on the assumption that high nibbles will always be 0.

    It would also be interesting to know which way is less memory hungry.

    Ioannis

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    this one
    Code:
    Z VAR WORD
    A VAR Z.LOWBYTE
    B VAR BYTE
    C VAR Z.HIGHBYTE
    D VAR BYTE
    
    '
    '    Some other code where you set A, B, C & D BYTE variables
    '
    
    Z.HIGHBYTE = Z.HIGHBYTE | (D<<4)
    Z.LOWBYTE = Z.LOWBYTE | (B<<4)
    ought to be the the less memory hungry.

    while changing the last two line by
    Code:
    Z = Z | (B<<4) | (D<<12)
    should use more code space.

    In the above A & C are Aliases, so shouldn't need any extra RAM.

    this
    Code:
    z=a+b<<4+c<<8+d<<12
    ought to use load of RAM and time to execute... BUT this said, I'm rusty and my memory/capacity fade since few months...
    Last edited by mister_e; - 11th November 2008 at 23:37.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Here's one that won't get used.
    But since the questions of how big and how fast came up, you won't find anything smaller or faster. (unless Bruce shows up)
    Code:
    GroupVars  VAR BYTE[6]
      A    VAR GroupVars(0)
      B    VAR GroupVars(1)
      C    VAR GroupVars(2)
      D    VAR GroupVars(3)
      ZW   VAR WORD EXT : @ZW = _GroupVars + 4
    
    ASM
        CHK?RP _GroupVars
        swapf  _D, W     ; swap nibbles in D
        iorwf  _C, W     ; or with C
        movwf  ZW + 1    ; move to Highbyte of word
        swapf  _B, W     ; swap nibbles in B
        iorwf  _A, W     ; or with A
        movwf  ZW        ; move to LowByte of word
    ENDASM
    DT

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    yeah, no complaint at all, ASM route is easy & fast, but the only minor problem it use a bit more of RAM?

    Who cares anyway
    Last edited by mister_e; - 12th November 2008 at 01:55.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 08:55
  2. Read/Write Problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th February 2010, 02:51
  3. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 08:25
  4. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th July 2008, 00:19
  5. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 16:23

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts