Shop OBEX P1 Docs P2 Docs Learn Events
Aliasing array names — Parallax Forums

Aliasing array names

KurtKurt Posts: 7
edited 2006-04-08 18:13 in BASIC Stamp
Hi:

I want to call a subroutine to do some 32 bit math. I plan on using the Pbasic array funtion to define four byte variables for the input arguments. For the sake of this discussion, lets assume 3 different 32 bit variables defined as argA(4), argB(4), and argC(4).

Let's say that I want to multiply by 2 (<<) Here's the code:

Shifter:
FOR index = 0 TO 2                      ' Shift argument left (multiply by 2)
  temp.HIGHBYTE = argument(index)
  temp.LOWBYTE  = argument(index + 1)
  temp = temp << 1
  argument(index) = temp.HIGHBYTE
NEXT
argument(3) = temp.LOWBYTE
RETURN


So here's the question: How can I call "Shifter" for each of argA, argB, & argC? Is there a way to alias them to "argument" (in my sample code)? Or will I need to "hard code" this routine for each array?

Thanks in advance,

kk

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
kk

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-04-07 16:46
    To do the 32 bit math, it will be more efficient to work with word variables, so instead of
    byte argA(4)
    use

    argA0 VAR Word
    argA1VAR Word
    argB0 VAR Word
    argB1 VAR Word
    argC0 VAR Word
    argC1 VAR Word
    



    notice that they are not defined explicitly as an array. Because of the way the Stamp works, you can still address them as an array. For example, argA0 can be addressed as argA0(0), and argA1 can be addressed as argA0(1), and argC1 can be addressed as argA0(5), or as argC0(1). You see? That method it turns out is actually much more flexible than defining array variables explicitly, and it is a frequent tool of intermediate to advanced PBASIC programmers.

    If you define argA, argB and argC in that order, one right after another, then that is exactly how they will be arranged in Stamp memory, and you could write a routine like the following:

    doubleMultByTwo:   ' enter with x = 0, 1 or 2, corresponding to argA, B or C.
                              ' x is a nib variable
      argA0(x*2+1) = argA0(x*2+1)<<1 + argA0.bit0(x*16+7)
      argA0(x*2) = argA0(x*2) << 1
    RETURN
    



    Note the last element in the first statement. argA0.bit0(x*16+7). This is where the flexibility of the implicit array addressing shows its steroids. That statement picks off the bit that has to be shifted up to the high word. Bits in words come in multiples of 16, and you want the 7th bit up in the least significant word.

    I hope that helps with what you are trying to do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-04-08 17:55
    In PM Kurt was puzzled by why I used
    argA0.bit0(x*16 +7) '
    in the above program, and he was right, I should have used
    argA0.bit0(x*16+15) '
    The idea is to pick off the msb of the least significant WORD, and transfer it up to become the lsb in the most significant WORD.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • KurtKurt Posts: 7
    edited 2006-04-08 18:13
    Thanks Tracy - This is exactly what I needed - AND it is tighter code than I'd originally considered.

    kk

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    kk
Sign In or Register to comment.