Array of Longs in PASM2
JonnyMac
Posts: 9,585
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
Add another
noporaltdis an alternative that doesn't requirenopThat did it. Thank you, Tony.
setdis an instruction I've never used butaltdandaltsI use a lot.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...
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.
Yeah, that sometimes gets me, too.
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 endThanks, again!
Jon, I think you are interested in PASM2 tips and the (untested) code below is optimal for speed. Two improvements:
altd/alts/altradds sign-extended S[17:9] to D (which leaves D unchanged for 9-bit #S) andreprepeats 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 endThat'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 endAgain, thanks!