Shop OBEX P1 Docs P2 Docs Learn Events
Word Array — Parallax Forums

Word Array

SailerManSailerMan Posts: 337
edited 2007-03-28 00:26 in General Discussion
I know that there are no Word Arrays in SX/B so What would be a way to simulate them?

There has to be a way. but I'm not saavy enough to figure it out. [noparse]:)[/noparse]

I need a 4 element word array. I'm using an SX-28.

Thanks in Advance for any help,
Regards,
Eric

·

Comments

  • JonnyMacJonnyMac Posts: 9,289
    edited 2007-03-09 02:30
    This is what I come up with off the top of my head -- you'll have to move the array elements in and out of Word variables to do things like math, etc.

    Start with some variables:

    tmpB1        VAR    Byte
    tmpW1        VAR    Word
    
    myWords        VAR    Byte (8)
    myWord0_LSB    VAR    myWords(0)
    myWord0_MSB    VAR    myWords(1)
    myWord1_LSB    VAR    myWords(2)
    myWord1_MSB    VAR    myWords(3)
    myWord2_LSB    VAR    myWords(4)
    myWord2_MSB    VAR    myWords(5)
    myWord3_LSB    VAR    myWords(6)
    myWord3_MSB    VAR    myWords(7)
    



    Declare a subroutine and function

    PUT_WORD    SUB     2, 3
    GET_WORD    FUNC    2, 1
    



    And here is the code for those declarations:

    ' Use: PUT_WORD index, value
    '
    PUT_WORD:
      tmpB1 = __PARAM1
      IF __PARAMCNT = 1 THEN
        tmpW1 = __PARAM2
      ELSE
        tmpW1 = __WPARAM23
      ENDIF
      tmpB1 = tmpB1 << 1
      myWords(tmpB1) = tmpB1_LSB
      INC tmpB1
      myWords(tmpB1) = tmpB1_MSB
      RETURN
    
    
    ' Use: value = GET_WORD index
    '
    GET_WORD:
      tmpB1 = __PARAM1
      tmpB1 = tmpB1 << 1
      tmpW1_LSB = myWords(tmpB1)
      INC tmpB1
      tmpW1_MSB = myWords(tmpB1)
      RETURN tmpW1
    

    Post Edited (JonnyMac) : 3/9/2007 3:00:08 AM GMT
  • SailerManSailerMan Posts: 337
    edited 2007-03-27 00:41
    I finally got around to trying this but it's not working.

    I get error until I Change


    myWords(tmpB1) = tmpB1_LSB <--- To TmpW1_LSB

    myWords(tmpB1) = tmpB1_MSB <--- To TmpW1_MSB


    I'm still trying to understand what tmpB1 = tmpB1 << 1 is doing can you explain.
  • BeanBean Posts: 8,129
    edited 2007-03-27 01:31
    Eric,
    Byte variables do not have a _LSB or _MSB suffix.

    tmpB1 = tmpB1 << 1 ' means to shift tmpB1 to the left 1 bit (effectively multiplying it by two)

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"

    Benjamin Franklin
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • SailerManSailerMan Posts: 337
    edited 2007-03-27 01:48
    Bean,

    I understand what "tmpB1 = tmpB1 << 1" means I'm just not sure why he used it in the code above.

    Regards,
    Eric
  • JonnyMacJonnyMac Posts: 9,289
    edited 2007-03-27 02:10
    Because tmpB1 gets the index of the desired Word, and you have to multiply that by two in order to be pointing at the LSB of that Word in the array. The odd bytes are the MSBs.
  • SailerManSailerMan Posts: 337
    edited 2007-03-27 02:19
    Duh! Got it... Thanks.. [noparse]:)[/noparse]
  • SailerManSailerMan Posts: 337
    edited 2007-03-27 23:13
    OK I got it working now... The Revised Code is below with two minor changes.

    Thanks Jonny for the code.··


    [size=2][code]
    tmpB1        VAR    Byte
    tmpW1        VAR    Word
    
    myWords        VAR    Byte (8)
    
    



    Declare a subroutine and function

    PUT_WORD    SUB     2, 3
    GET_WORD    FUNC    2, 1
    



    And here is the code for those declarations:

    ' Use: PUT_WORD index, value
    '
    PUT_WORD:
      tmpB1 = __PARAM1
      IF __PARAMCNT = 1 THEN
        tmpW1 = __PARAM2
      ELSE
        tmpW1 = __WPARAM23
      ENDIF
      tmpB1 = tmpB1 << 1
      myWords(tmpB1) = tmpW1_LSB                       ' I changed this line
      INC tmpB1
      myWords(tmpB1) = tmpW1_MSB                       ' I changed this line
      RETURN
    
    
    ' Use: value = GET_WORD index
    '
    GET_WORD:
      tmpB1 = __PARAM1
      tmpB1 = tmpB1 << 1
      tmpW1_LSB = myWords(tmpB1)
      INC tmpB1
      tmpW1_MSB = myWords(tmpB1)
      RETURN tmpW1
    


    [/code][/size]
  • JonnyMacJonnyMac Posts: 9,289
    edited 2007-03-28 00:26
    Looks like I had a typo in my example -- good catch! Of course, byte vars don't have _LSB and _MSB extensions.
Sign In or Register to comment.