Shop OBEX P1 Docs P2 Docs Learn Events
How does this instruction work? in Beau's SPI_Asm (self modifying code) — Parallax Forums

How does this instruction work? in Beau's SPI_Asm (self modifying code)

MajorStrainMajorStrain Posts: 6
edited 2011-07-17 00:32 in Propeller 1
For the life of me I can't work out what this instruction does.
add     :arg,d0
d0 is predefined to equal $200 or 512d. Why $200 ?
'' SPI Engine - main loop
'
loop          rdlong  t1,par          wz                ''wait for command
        if_z  jmp     #loop
              movd    :arg,#arg0                        ''get 5 arguments ; arg0 to arg4
              mov     t2,t1                             ''    │
              mov     t3,#5                             ''───┘
:arg          rdlong  arg0,t2
              add     :arg,d0
              add     t2,#4
              djnz    t3,#:arg
              mov     address,t1                        ''preserve address location for passing
                                                        ''variables back to Spin language.
              wrlong  zero,par                          ''zero command to signify command received
              ror     t1,#16+2                          ''lookup command address
              add     t1,#jumps
              movs    :table,t1
              rol     t1,#2
              shl     t1,#3
:table        mov     t2,0
              shr     t2,t1
              and     t2,#$FF
              jmp     t2                                ''jump to command
jumps         byte    0                                 ''0
              byte    SHIFTOUT_                         ''1
              byte    SHIFTIN_                          ''2
              byte    NotUsed_                          ''3
NotUsed_      jmp     #loop

The code is well documented except for this section where it is getting the parameters from hub ram via decoding the command long pointed to by par.
I understand that the loop is self modifying the destination section in the rdlong command to point to the next reserved arg location, but adding 512 to :arg has lost me.

I'm one of those guys that to modify code I need to understand the workings of the original code.
Will someone put me out of my misery please.

Another less pressing question is why the +2 in the following line
              ror     t1,#16+2                          ''lookup command address
In encoding the command we shifted left only 16 bits

Sorry for the newbie questions.
Cheers,
Phil

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-15 23:43
    Adding $200 increments the destination field of the target instruction by one.

    -Phil
  • MajorStrainMajorStrain Posts: 6
    edited 2011-07-16 01:34
    Thanks Phil,

    Now that you point it out, it's just so simple. I just couldn't see it.

    The only problem I have now is how long it will take this big red hand print on my forehead to go away.

    Cheers,
    Phil
  • MajorStrainMajorStrain Posts: 6
    edited 2011-07-16 05:31
    Was just reading Phil's "Propeller Tips & Traps" again.
    It's easy to see now I know what I'm looking for.

    On page 13 under the heading "Embedding Array Pointers" the same technique is used just with the " $200 " written a different way " 1 << 9".

    Hope that helps someone else make the link in the future.

    Cheers,
    Phil
  • kuronekokuroneko Posts: 3,623
    edited 2011-07-16 05:44
    Another less pressing question is why the +2 in the following line
    ror     t1,#16+2                          ''lookup command address
    
    In encoding the command we shifted left only 16 bits
    The commands are packed into a long (see jumps table). So - because cog memory is long addressed - we want the relevant long address derived from the command index. Which means divide by four (ror/shr #2). Not the most efficient way though.
  • MajorStrainMajorStrain Posts: 6
    edited 2011-07-17 00:32
    Thanks for that Kuroneko,
    One more bit of the ASM puzzle solved. :)
    Cheers,
    Phil
Sign In or Register to comment.