Shop OBEX P1 Docs P2 Docs Learn Events
Help Needed about all those orgh org @@@ $ — Parallax Forums

Help Needed about all those orgh org @@@ $

Christof Eb.Christof Eb. Posts: 1,329
edited 2025-06-05 12:28 in PASM2/Spin2 (P2)

Hi,
I am trying to get a minimal XBYTE interpreter in a file starting from SPIN2 working.
Seems I am awfully stuck with the PASM inside a SPIN2 program file.

Where can I find docu about all these address symbols?
org, orgh, $, @@@, @
When do I use what?

Is this correct:
@@@ gives an absolute address in HUB

This statement from the SPIN2 docu seems to be wrong, at least when you use orgh a second time?!!!?

The following program seems to place the luttable correctly into LUT but the branch seems to crash.
I assume, this has to do with "long (progstart-$) ' relative distance" ?

Any help appreciated!
Christof

{ Minimal Program starting cog with XBYTE
}

_xtlfreq =  25_000_000
_clkfreq = 200000000


VAR
    long buf[16]

PUB testa() | i
    DEBUG("Hello from P2")
    repeat i from 0 to 63
        byte[@buf][i]:=i ' preload buffer with counting pattern

    coginit(NEWCOG, @xforth, @luttable)
    repeat

DAT
    orgh

xforth
    org 
                DEBUG("XBYTE Cog started")
                'DEBUG(UHEX_LONG(ptra))  ' show where are we reading from

                setq2     #$FF           ' Fill Luttable length 256
'                rdlong    $100,luttab_ptr ' starting from $100
                rdlong   0,luttab_ptr   ' starting from 0 in LUT

                rdlut   temp,#0
                debug(uhex_long(temp))
                rdlut   temp,#1
                debug(uhex_long(temp))

                rdfast  #0,prog_ptr   'init fifo read at start of bytecodes

                push    #$1FF           'push $1FF for xbyte
'        _ret_   setq    #$100           'start xbyte with LUT base = $100, no stack pop
'        _ret_   setq    #0           'start xbyte with LUT base = $100, no stack pop

          rep       @.r,#8        'prepare to single-step by stuffing stack with byteloop address
          push      ##byteloop    '(bottom stack value gets copied each _RET_ / RET)
.r
byteloop  nop                     '21-NOP landing strip for any trailing skip pattern
          nop                     'that XBYTE would have canceled on _RET_ / RET
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          nop
          rfbyte    pa                         'get next bytecode into pa
          getptr    pb                         'get next bytecode address into pb
          debug(uhex_byte(pa),uhex_long(pb))   'show bytecode and next bytecode address
'          add pa,#$100             ' if lut table to start from $100 in LUT
          rdlut     temp,pa                    'lookup EXECF long from LUT
          execf     temp                       'do EXECF to execute bytecode, returns to byteloop





' Bytecode routines
itog_led _ret_ drvnot    #57                        ' blink led of Kissboard

ipause1       waitx     ##200_000_000               ' wait for a second
              ret

ibranch       rflong pa                  ' get offset
              getptr pb                  ' necessary?
              add pb,pa
        _ret_ rdfast #0,pb

' registers in cog ram
temp        long 0
prog_ptr    long @@@progstart
luttab_ptr  long @@@luttable

    fit $1C0

        orgh
progstart
        byte  0                 ' blink
        byte  1                 ' pause
        byte  1                 ' pause        
        byte  2                 ' branch
        long  (progstart-$)       ' relative distance

luttable
        long  itog_led
        long  ipause1
        long  ibranch

Edit:
Now I found that using FlexSpin orgh seems to have a different meaning.

This seems to work:
orgh

progstart
        byte  0                 ' blink
        byte  1                 ' pause
        byte  1                 ' pause        
        byte  2                 ' branch
'        long  (progstart-$)       ' relative distance
'   long -(4+4)     ' works
    long progstart-endprog
endprog

Comments

  • Christof Eb.Christof Eb. Posts: 1,329
    edited 2025-06-06 07:02

    Hm, experimental results:
    seems that @@@ is not necessary here, just works as well without:
    temp long 0
    prog_ptr long @@@progstart
    luttab_ptr long @@@luttable

    Also both "ORG" or "ORG 0" can be used?

    What $ exactly does or where it points to, is still a micracle?

  • ersmithersmith Posts: 6,176

    @@@sym always means the absolute address of symbol sym. @sym means that for a pure assembly program, but for a mixed Spin2/PASM program it means the address relative to the start of the object.
    IIRC @ and @@@ end up meaning the same thing in flexspin Spin2 code. In Spin1 they're different, and I think in PNut there is no @@@ (and @ has the same meaning for pure assembly programs, but not for mixed Spin2 and PASM).

    $ means the address of the current instruction or data item.

    ORG and ORG 0 both mean the same thing (start assembling code that will be loaded into COG memory at address 0).

    ORGH means to stop assuming code will be in COG memory, and assume it will be in hub memory instead (the H stands for HUB). In pure assembly programs ORGH on its own is equivalent to ORGH $400 (just like ORG is equivalent to ORG 0) but in mixed language programs that is not the case, because it's not possible to place code at arbitrary hub addresses; in this case ORGH just means "resume assembling for HUB at the current HUB address".

  • @ersmith said:
    @@@sym always means the absolute address of symbol sym. @sym means that for a pure assembly program, but for a mixed Spin2/PASM program it means the address relative to the start of the object.
    IIRC @ and @@@ end up meaning the same thing in flexspin Spin2 code. In Spin1 they're different, and I think in PNut there is no @@@ (and @ has the same meaning for pure assembly programs, but not for mixed Spin2 and PASM).

    $ means the address of the current instruction or data item.

    ORG and ORG 0 both mean the same thing (start assembling code that will be loaded into COG memory at address 0).

    ORGH means to stop assuming code will be in COG memory, and assume it will be in hub memory instead (the H stands for HUB). In pure assembly programs ORGH on its own is equivalent to ORGH $400 (just like ORG is equivalent to ORG 0) but in mixed language programs that is not the case, because it's not possible to place code at arbitrary hub addresses; in this case ORGH just means "resume assembling for HUB at the current HUB address".

    Thank you, Eric!
    Perhaps you could copy some of these explanations into your docu?

    I was confused about $ in the example for XBYTE in the P2 doc v35. My confusion probably came from the fact, that XBYTE does update pb but RFVARS does not.
    Chip and you both seem to have something in mind, about how assembler syntax is working, so there is no description about this (as far as I know). For the guys, who have learned assembler (probably a long time ago) this seems not to be a big problem. Perhaps a link to a good assembler language guide pdf would be helpful? The Manual for P1 tried to explain some things. Perhaps it would be good to mention this document even for people who want to program P2?
    Thanks again!
    Christof

  • Christof Eb.Christof Eb. Posts: 1,329
    edited 2025-06-10 07:42

    So I tried to make a table about these things:

  • ersmithersmith Posts: 6,176

    @"Christof Eb." I've been reluctant to try to write an assembly language manual because (1) it's a lot of work, and (2) Parallax has been working on one for a while now. The P1 manual is pretty good, so I hope the P2 version is finished soon (I think a preliminary version is available, somewhere behind the P2 Docs button).

  • @ersmith said:
    @@@sym always means the absolute address of symbol sym. @sym means that for a pure assembly program, but for a mixed Spin2/PASM program it means the address relative to the start of the object.
    IIRC @ and @@@ end up meaning the same thing in flexspin Spin2 code. In Spin1 they're different, and I think in PNut there is no @@@ (and @ has the same meaning for pure assembly programs, but not for mixed Spin2 and PASM).

    I'm pretty sure that all got changed to match PNut, didn't it?
    (PNut does not have the triple-@ operator due to how it links objects...)

  • ersmithersmith Posts: 6,176

    @Wuerfel_21 said:

    @ersmith said:
    @@@sym always means the absolute address of symbol sym. @sym means that for a pure assembly program, but for a mixed Spin2/PASM program it means the address relative to the start of the object.
    IIRC @ and @@@ end up meaning the same thing in flexspin Spin2 code. In Spin1 they're different, and I think in PNut there is no @@@ (and @ has the same meaning for pure assembly programs, but not for mixed Spin2 and PASM).

    I'm pretty sure that all got changed to match PNut, didn't it?
    (PNut does not have the triple-@ operator due to how it links objects...)

    I think you're right, it's been a while since I looked at this. Flexspin has to match PNut as much as possible, so @ on its own does mean "address relative to start of object". For a PASM only project the "object" starts at address 0, so @ and @@@ evaluate to the same value. The reason for the triple @@@ is that it combines single @ (address relative to start of object) with double @@ (base address of object) to get the real address of the symbol.

Sign In or Register to comment.