Shop OBEX P1 Docs P2 Docs Learn Events
Array as local variable in Inline Assembly? — Parallax Forums

Array as local variable in Inline Assembly?

bob_g4bbybob_g4bby Posts: 506
edited 2026-01-12 14:44 in PASM2/Spin2 (P2)

I wanted to declare a local variable as a small array of longs in cog ram -
pub xytopol1(buffin, buffout) | array[32], buffinptr, buffoutptr, counter
- but it makes Pnut choke with -
For access via inline assembly, a local variable must be either a long, a structure, or a pointer, and be both within the first 16 longs and long aligned. I'm guessing a Structure or Pointer refers to variables in hub ram?
Anyone got a solution?
Here's the method I'm trying to write:-

pub xytopol1(buffin, buffout) | array[32], buffinptr, buffoutptr, counter

    org

        mov buffinptr, buffin
        mov buffoutptr, buffout
        mov counter, #(sigbuffsize/16)
.xypol1
            setq #(32-1)
            rdlong array, buffinptr

            qvector array, array+1
            nop
            qvector array+2, array+3
            nop
            qvector array+4, array+5
            nop
            qvector array+6, array+7
            nop
            qvector array+8, array+9
            nop
            qvector array+10, array+11
            nop
            qvector array+12, array+13
            nop
            qvector array+14, array+15
            getqy   array
            getqx   array+1
            nop
            qvector array+16, array+17
            getqy   array+2
            getqx   array+3
            nop
            qvector array+18, array+19
            getqy   array+4
            getqx   array+5
            nop
            qvector array+20, array+21
            getqy   array+6
            getqx   array+7
            nop
            qvector array+22, array+23
            getqy   array+8
            getqx   array+9
            nop
            qvector array+24, array+25
            getqy   array+10
            getqx   array+11
            nop
            qvector array+26, array+27
            getqy   array+12
            getqx   array+13
            nop
            qvector array+28, array+29
            getqy   array+14
            getqx   array+15
            nop
            qvector array+30, array+31
            getqy   array+16
            getqx   array+17
            getqy   array+18
            getqx   array+19
            getqy   array+20
            getqx   array+21
            getqy   array+22
            getqx   array+23
            getqy   array+24
            getqx   array+25
            getqy   array+26
            getqx   array+27
            getqy   array+28
            getqx   array+29
            getqy   array+30
            getqx   array+31

            setq #(32-1)
            wrlong array, buffoutptr
            add buffinptr, #32
            add buffoutptr, #32
            djnz counter, @.xypol1

    end

Cheers, bob

Comments

  • JonnyMacJonnyMac Posts: 9,578
    edited 2026-01-12 17:08

    PNut will transfer up to 16 longs from the method definition; these includes method parameters and locals. You can define the array in your code like this:

    pub method(p_in, p_out)
    
      org
    
    .done                   ret
    
    array                   long    0[32]
    
      end 
    

    After a cup of tea waiting for the weather to warm so I can go for a walk, I tried this -- it works.

    var { globals }
    
      long  array1[32]
      long  array2[32]
    
    
    pub main() | i
    
      setup()
      wait_for_terminal(true, 250)
    
      repeat i from 0 to 31
        array1[i] := rndm.xrandomize(0, 1000)
    
      method(@array1, @array2)
    
      repeat i from 0 to 31
        term.fstr3(@"%2d  %4d  %4d\r", i, array1[i], array2[i])
    
      repeat
      ' forever
    
    
    pub method(p_in, p_out) | i, x
    
      org
    
                            setq #(32-1)
                            rdlong array, p_in
    
                            add     array+00, #1
                            add     array+01, #1
                            add     array+02, #1
                            add     array+03, #1
                            add     array+04, #1
    
                            setq #(32-1)   
                            wrlong array, p_out
    
    .done                   ret
    
    array                   long    0[32]
    
      end  
    

    Simple, yes, but demonstrates the idea of embedding the array in your code instead of the locals.

Sign In or Register to comment.