Shop OBEX P1 Docs P2 Docs Learn Events
How do you pass a global variable long to DAT block assembly code??? — Parallax Forums

How do you pass a global variable long to DAT block assembly code???

AmateurJackAmateurJack Posts: 4
edited 2006-12-21 00:16 in Propeller 1
Hey everyone, I'm just starting out with the Propeller package and I am indeed impressed. Have hit a MAJOR SNAG, I can't succesfully read global variable value in a dat block assembly operation. I have tried addressing and read the manual, but can't figure it out. Example:
VAR
long time

PUB MAIN
cognew(@start, 0)

repeat
time := time +1

DAT

long t, @time ???????
RDLONG t, @time ??????
waitcnt clkfreq, t

QUESTION: How does one read what the value of time is in the main function at a certain point in time, from the DAT block , in ASSEMBLY? How do you put it in registers? I've tried specifying specific registers, like $1EF and thing like that, but cant get it to work. Can someone please help or point me in the correct direction? It would be much appreciated. Thanks,
-Jack

Comments

  • AmateurJackAmateurJack Posts: 4
    edited 2006-12-20 10:17
    Correction, example code goes like:

    PUB MAIN
    cognew(@start, 0)

    repeat
    time := time +1

    DAT

    long t, @time
    :start RDLONG t, @time
    waitcnt clkfreq, t
  • Mike GreenMike Green Posts: 23,101
    edited 2006-12-20 15:50
    1) There are some examples in a "sticky" thread called "Assembly Code Examples for Beginners". Please look at them.
    2) You're using ":start" which is a local label. This is used only where you need a label (like for a jump) that is only defined for a few lines of code (between two regular labels). It shouldn't be usable outside the DAT block. Use a regular label (without the ":").
    3) The label in the COGNEW statement is that of the first memory location to be loaded into the cog AND the first location to be executed. The "long" that you have at the beginning of the DAT effectively doesn't exist to the cog routine.
    4) The PAR register is intended to be used to pass the address of a parameter or parameter block to a cog. For an example like this, it's really easiest to use it. Remember that the low order 2 bits are always zero, so it must be the address of a long.
    VAR
      long time
    PUB main
      time := clkfreq/50000 ' 20ms wait time
      cognew(@start,@time)
      repeat
        waitcnt(clkfreq+cnt)     ' increment once a second
        time += clkfreq/50000 ' make 20ms longer each second
    DAT
               org        0          ' assumed initially, but good to put this here
    start    mov       outa,#0 ' use LED on demo board for indicator
               mov       dira,#$100
    :loop    rdlong    t,PAR   ' get value from variable
               add        t,CNT   ' wait for requested number of clock ticks
               waitcnt  t,#0
               xor        outa,#$100 ' toggle LED
               jmp       #:loop  ' an example of the use of a local label
    t          long       0
    
    
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-20 19:22
    There is one other point I'd like to add to Mike's comments. Even though your code doesn't do it, say you declare time (which should not use the @ symbol in it's declaration) to be after "start" and within 496 longs of "start", the value of time as it exists when the cog is loaded will be loaded into the cog. However this becomes a local copy of the value and subsequent changes to time made by the Spin section will not be reflected to the cog's local copy.

    Heres the rule of thumb you should follow:

    If you have a value you want to pass to a cog, and it will not be further updated after the cog has already started, declare it in the DAT section after the code for the cog (but not more than 496 longs after it's start), and assign it's value prior to calling cognew. After the cog starts running, it can directly access the local copy of the passed value without having to do a hub access.

    If you have a value which needs further updating, declare it in the VAR section and pass a pointer to it and use hub access operations to get up to date copies of the value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • AmateurJackAmateurJack Posts: 4
    edited 2006-12-21 00:16
    Thanks for that Mike and Paul. I eventually found the reference in the 'sticky forums'. Sorry for not completely-thoroughly manually searching the forums (the search engine doesn't pick up on everything), I won't waste your time next time. Thanks again.

    -Jack
Sign In or Register to comment.