Shop OBEX P1 Docs P2 Docs Learn Events
PASM reconstruction — Parallax Forums

PASM reconstruction

BocephusBocephus Posts: 58
edited 2011-02-04 01:33 in Propeller 1
As a learning exercise, I have started coding a program to read a binary file and separate the various bit fields with the purpose of reconstructing my PASM code from the binary source. Currently all code is PASM except a cognew from spin to run the code. It appears that two of the longs represent my cognew but I'm unsure and would like verification or correction. Also, if anyone knows the significance or meaning of each byte within these longs please explain. Here is a snip of the output. The COG_INIT and COG_NEW are the lines in question.
ADDR    HEX             ZCRI    CON        INST        DEST    SRC        EFFECT    LONG#
=========================================================================================
0064    23 52 BC F8     0010    ----        WAITCNT    0029    0023         --      0013
0068    13 54 FC E4     0011    ----        DJNZ       002A    #0013        --      0014
006C    08 00 7C 5C     0001    ----        JMP        0000    #0008        NR      0015
0070    01 4C FC 2C     0011    ----        SHL        0026    #0001        --      0016
0074    20 50 FC A0     0011    ----        MOV        0028    #0020        --      0017
0078    01 48 FC 2C     0011    ----        SHL        0024    #0001        --      0018
007C    25 4C BC E1     0110    ----        CMPSUB     0026    0025         WC      0019
0080    01 48 F0 80     0011    IF_C        ADD        0024    #0001        --      001A
0084    01 4C FC 2C     0011    ----        SHL        0026    #0001        --      001B
0088    18 50 FC E4     0011    ----        DJNZ       0028    #0018        --      001C
008C    00 00 7C 5C     0001    ----        JMP        0000    #0000        NR      001D
0090    04 00 00 10     0000    DAT_VAR     DAT_0      LONG    10000004             001E
0094    10 00 00 00     0000    DAT_VAR     DAT_1      LONG    00000010             001F
0098    20 00 00 00     0000    DAT_VAR     DAT_2      LONG    00000020             0020
009C    00 00 FF 00     0011    DAT_VAR     DAT_3      LONG    00FF0000             0021
00A0    00 00 00 01     0100    DAT_VAR     DAT_4      LONG    01000000             0022
00A4    00 7D 00 00     0000    DAT_VAR     DAT_5      LONG    00007D00             0023
00A8    00 00 00 00     0000    DAT_VAR     DAT_6      LONG    00000000             0024
00AC    00 B4 C4 04     0011    DAT_VAR     DAT_7      LONG    04C4B400             0025
00B0    70 94 00 00     0000    DAT_VAR     DAT_8      LONG    00009470             0026
00B4    34 C7 08 35     0100    COG_INIT    COG_INIT   LONG    3508C734             SP06
00B8    2C 32 00 00     0000    COG_NEW     COG_NEW    LONG    0000322C             SP07
00BC    00 00 00 00     ----    ----        VAR_0      RES     1            --      0027
00C0    00 00 00 00     ----    ----        VAR_1      RES     1            --      0028
00C4    00 00 00 00     ----    ----        VAR_2      RES     1            --      0029
00C8    00 00 00 00     ----    ----        VAR_3      RES     1            --      002A

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-01 13:43
    The two longs are Spin bytecodes for cognew and return. This translates as follows:
    34    ldlim1   'Load a value of -1 to the stack
    c7 08 lalo 8   'Load an object address with an offset of 8
    35    ldl0     'Load a value of zero to the stack
    2c    coginit  'Perform a coginit using the 3 values on the stack
    32    ret      'Return
    
    The object offset for your PASM program is 8, even though your PASM program is at the beginning of the DAT area. This is because the method table is in the first 8 bytes of your object. The parameter of -1 for coginit says that it should use the next available cog (i.e. cognew).

    Dave
  • BocephusBocephus Posts: 58
    edited 2011-02-01 15:48
    Very helpful, thanks Dave. I'll search the forum for the spin bytecodes. Will I find a list of the mnemonics, such as ldln1 and lalo 8, in a forum search as well?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-01 16:14
    There are no official mnemonics for the spin bytecodes. BST provides a description of the bytecodes in its compiler listing, but it doesn't use mnemonics. I am developing a bytecode assembler (spasm) and a spin simulator (SpinSim), and I needed mnemonics, so I defined them. There have been other attempts to define spin bytecode mnemonics, but I felt they weren't concise enough. I'll post a description of the codes when I get home this evening.

    Dave
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-02-01 20:20
    Bocephus,

    The attached file lists the Spin bytecode mnemonics with some descriptions. I need to work more on this document. The memory mnemonics have the following format:

    {ld, st, la, ex}{l, m, w, b}{l, v, o, a, i}{c, x, 0, 1, m1}

    Each field has the following meanings:

    {ld, st, la, ex} - Load, Store, Load Address, Execute
    {l, m, w, b} - Long, Medium, Word, Byte (Medium is 3 bytes)
    {l, v, o, a, i} - Local offset, Var offset, Object offset, Absolute address, Immediate
    {c, x, 0, 1, m1} - Compact, Indexed, zero, one, minus one

    The third variable on a the stack would be loaded with "ldllc 12" or "ldll 12" The ldllc generates a one-byte code and the ldll generates two bytes. An indexed byte in the DAT area would be stored with "stbox $111".

    The "ex" memory instruction perform an additional operation on a memory location, and can optionally load the result onto the stack. The additional instruction is one of the 32 math operations or it could pre or post decrement or increment, sign extension or the random function. An example of an ex instruction would be "exllc 12 preinc load", which would increment stack variable number 3 and load the result on the stack.

    Dave
  • BocephusBocephus Posts: 58
    edited 2011-02-01 23:06
    Dave, you have some great projects going there. I fiddled with BST and it is nice as well.

    Your mnemonics format seems logical, and I appreciate the explanation along with the opcodes text.

    After searching the forum for some time, I see I am well behind the curve, but I think the fog is slowly lifting.
  • BocephusBocephus Posts: 58
    edited 2011-02-04 01:33
    I got the program to read the binary file and regenerate compilable source. Thanks for the help Dave!
    CON
            _clkmode = xtal1 + pll16x
            _clkfreq = 80000000
    
    PUB main
            cognew(@_asm_, 0)
    
    DAT
                    ORG
    
    _asm_           OR      CTRA,   DAT_0   
                    CALL    #FUN_0  
                    MOV     FRQA,   DAT_6   
                    OR      DIRA,   DAT_3   
                    MOV     VAR_1,  #1      
                    SHL     VAR_1,  #16     
                    MOV     VAR_2,  CNT     
                    ADD     VAR_2,  DAT_5   
    
    :LAB_0          OR      DIRA,   DAT_1   
                    WAITCNT VAR_2,  DAT_5   
                    AND     DAT_2,  INA     WZ,NR
                    ANDN    DIRA,   DAT_1   
            IF_NZ   JMP     #:LAB_0 
                    ADD     VAR_0,  VAR_1   
                    AND     VAR_0,  DAT_4   WC,NR
            IF_C    MOV     VAR_0,  #0      
            IF_NC   ANDN    OUTA,   DAT_3   
            IF_NC   OR      OUTA,   VAR_0   
                    MOV     VAR_3,  #VSCL   
    
    :LAB_1          WAITCNT VAR_2,  DAT_5   
                    DJNZ    VAR_3,  #:LAB_1 
                    JMP     #:LAB_0 
    
    FUN_0           SHL     DAT_8,  #1      
                    MOV     VAR_1,  #32     
    
    :LAB_2          SHL     DAT_6,  #1      
                    CMPSUB  DAT_8,  DAT_7   WC
            IF_C    ADD     DAT_6,  #1      
                    SHL     DAT_8,  #1      
                    DJNZ    VAR_1,  #:LAB_2 
    
    FUN_0_RET       RET                     
    
                    DAT_0   LONG    268435460       
                    DAT_1   LONG    16      
                    DAT_2   LONG    32      
                    DAT_3   LONG    16711680        
                    DAT_4   LONG    16777216        
                    DAT_5   LONG    32000   
                    DAT_6   LONG    0       
                    DAT_7   LONG    80000000        
                    DAT_8   LONG    38000   
                    VAR_0   RES     1       
                    VAR_1   RES     1       
                    VAR_2   RES     1       
                    VAR_3   RES     1       
    
                    FIT
    
Sign In or Register to comment.