Shop OBEX P1 Docs P2 Docs Learn Events
PASM Self Modifiying Code via Label Modification ?? Really?? — Parallax Forums

PASM Self Modifiying Code via Label Modification ?? Really??

greybeardgreybeard Posts: 65
edited 2014-10-27 13:42 in Propeller 1
I recently ran across a set of PASM code I do not understand. I have re-constructed a fragment here. The code works but I don't understand why!
I can tell that the author is working with 3 arrays that consist of next, lows, and highs each separated by the size of the arrays. What APPARENTLY happens is that the variables next1, low1, high1 is initialized first, then next2, low2, high2 ...etc . Is the code actually manipulating the local labels of the code. What is the PASM doing??

MOV X, SIZE
:P1 TJZ enabled, #label ' if not eneabled, skip --- THIS I UNDERSAND
:P2 MOV T1l, cnt ' set start time ---
:P3 ADD T1l, next1 ' set next time
:P4 MOV T1h, low1 ' get low time
:P5 ADD T1h, high3 ' set high time

ADD :P1, C1 ' move dest --- ?????
ADD :P2, C1 ' move dest
ADD :P3, C2 ' move dest and source
ADD :P4, C2 ' move dest and sourc
ADD :P5, C2
. DJNZ X, #:P1 ' go to next set


label
.
.

C1 Long 1<< array_size
C2 Long 1<< array_size +1

next1 long 0
.
.

low1 long 0
..
..


high1 long 0
.
.
T1l res 0
T1h res 0

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-10-24 12:37
    Why don't you attach the code you ran across and we go from there? But basically yes, that's how self-modifying code works. The insn in question gets modified in place, using labels makes it just look nice (you'll also see e.g. add $-1, dst1).
  • MJBMJB Posts: 1,235
    edited 2014-10-25 06:22
    greybeard wrote: »
    Is the code actually manipulating the local labels of the code. What is the PASM doing??

    MOV X, SIZE
    :P1 TJZ enabled, #label ' if not eneabled, skip --- THIS I UNDERSAND
    :P2 MOV T1l, cnt ' set start time ---
    :P3 ADD T1l, next1 ' set next time
    :P4 MOV T1h, low1 ' get low time
    :P5 ADD T1h, high3 ' set high time

    ADD :P1, C1 ' move dest --- ?????
    ADD :P2, C1 ' move dest
    ADD :P3, C2 ' move dest and source
    ADD :P4, C2 ' move dest and sourc
    ADD :P5, C2
    . DJNZ X, #:P1 ' go to next set


    label
    .
    .

    C1 Long 1<< array_size
    C2 Long 1<< array_size +1

    next1 long 0
    .
    .

    low1 long 0
    ..
    ..


    high1 long 0
    .
    .
    T1l res 0
    T1h res 0
    no, it is not modifying the labels, but the instructions at the position given by the label.
    e.g. by adding 1 to the move instruction, the destination field will be incremented by 1
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-10-25 08:19
    There's a link to deSilva's PASM tutorial in post #3 of my index. The tutorial explains the purpose of all the bits in the 32-bit instructions. I also have links to other PASM tutorials in post #3.

    There's also a link to Phil's tutorial on how to post code to the forum in post #3.
  • greybeardgreybeard Posts: 65
    edited 2014-10-27 13:42
    Thanks. I now understand what the code is doing. Looks like a handy way to work with array values.
Sign In or Register to comment.