Shop OBEX P1 Docs P2 Docs Learn Events
Array of Longs in PASM2 — Parallax Forums

Array of Longs in PASM2

JonnyMacJonnyMac Posts: 9,585
edited 2026-01-12 21:04 in PASM2/Spin2 (P2)

While making a suggestion in another post about putting an array into inline PASM (which works), I started some personal experiments and ran into a snag. In short, I'd like to access an array in PASM2 with a variable index, but code like I used to use in the P1 isn't working. Please -- gently -- show me where I've gone wrong. Thanks.

pub method(p_in, p_out)

  org

                        setq #(32-1)
                        rdlong array, p_in

                        mov       i, #0

loop                    mov       pntr, #array
                        add       pntr, i
                        setd      update, pntr
                        nop             
update                  shr       0-0, #1                       ' NOT WORKING       
                        incmod    i, #31                wc
        if_c            jmp       #loop

                        setq #(32-1)   
                        wrlong array, p_out

done                    ret                     

array                   res       32
pntr                    res       1
i                       res       1

  end

Comments

  • TonyB_TonyB_ Posts: 2,258
    edited 2026-01-12 22:11

    Add another nop or
    altd is an alternative that doesn't require nop

    loop                    altd      i, #array
    update                  shr       0-0, #1        
    
  • JonnyMacJonnyMac Posts: 9,585

    That did it. Thank you, Tony.

  • TonyB_TonyB_ Posts: 2,258
    edited 2026-01-12 22:27

    @JonnyMac said:
    That did it. Thank you, Tony.

    setd is an instruction I've never used but altd and alts I use a lot.

  • RaymanRayman Posts: 15,951

    RES in inline assembly? Surprised that works...

    One thing that trips me up a lot is only adding 1 to long pointers when need to add 4...

  • JonnyMacJonnyMac Posts: 9,585

    @Rayman said:
    RES in inline assembly? Surprised that works...

    I was surprised, too. Remember, we have about $120 (288) instructions available for inline assembly, so we do need to be mindful of that, and that variables defined with RES are going to end up with whatever is in the cog RAM at those locations.

    One thing that trips me up a lot is only adding 1 to long pointers when need to add 4...

    Yeah, that sometimes gets me, too.

  • JonnyMacJonnyMac Posts: 9,585

    @TonyB_ said:
    setd is an instruction I've never used but altd and alts I use a lot.

    I was so excited about fixing with another nop that I jumped right to that. Then I popped back and saw the altd -- much nicer and saves a bit of code. This is the adjustment.

    pub method(p_in, p_out)
    
      org
    
                            setq #(32-1)
                            rdlong array, p_in
    
                            mov       i, #0
    
    loop                    altd      i, #array         
    update                  shl       0-0, #2
                            incmod    i, #31                wc
            if_nc           jmp       #loop
    
                            setq #(32-1)   
                            wrlong array, p_out
    
    done                    ret                     
    
    array                   res       32
    i                       res       1
    
      end
    

    Thanks, again!

  • TonyB_TonyB_ Posts: 2,258

    @JonnyMac said:

    @TonyB_ said:
    setd is an instruction I've never used but altd and alts I use a lot.

    I was so excited about fixing with another nop that I jumped right to that. Then I popped back and saw the altd -- much nicer and saves a bit of code.

    Jon, I think you are interested in PASM2 tips and the (untested) code below is optimal for speed. Two improvements: altd/alts/altr adds sign-extended S[17:9] to D (which leaves D unchanged for 9-bit #S) and rep repeats an instruction block by jumping back to the start in zero cycles.

    pub method(p_in, p_out)
    
      org
    
                            setq    #(32-1)
                            rdlong  array, p_in
    
                            mov     i, #0
                            rep     #2,#32
                            altd    i, s         
                            shl     0-0, #2
    
                            setq    #(32-1)   
                            wrlong  array, p_out
    
    done                    ret                     
    
    s                       long    1 << 9 + array  'add +1 to i in altd
    array                   res     32
    i                       res     1
    
      end
    
  • JonnyMacJonnyMac Posts: 9,585
    edited 2026-01-13 02:19

    That's pretty nifty, Tony. I'll keep both in a recipe book as the first allows random access of array elements.

    I'm still learning how to decode Chip's notes in the PASM instruction list, althought Ada's docs make mention of auto-incrementing value. By using a traditional loop and debug I was able to see this working. Based on your comment, I did change the increment value. One thing... I was a little confused that # is not required with array in the line that declares s.

    pub method(p_in, p_out)
    
      org
    
                            setq      #(32-1)
                            rdlong    array, p_in
    
                            mov       i, #0
                            mov       j, #32
    
    loop                    debug(uhex_long(i))   
                            altd      i, s         
                            shl       0-0, #2
                            djnz      j, #loop
    
                            setq      #(32-1)   
                            wrlong    array, p_out
    
    done                    ret                       
    
    s                       long      1 << 9 + array                ' add +1 to i in altd
    
    array                   res       32
    i                       res       1
    j                       res       1
    
      end
    

    Again, thanks!

Sign In or Register to comment.