Shop OBEX P1 Docs P2 Docs Learn Events
Accessing VAR variables from assembly — Parallax Forums

Accessing VAR variables from assembly

dnaddordnaddor Posts: 26
edited 2007-05-02 19:22 in Propeller 1
I need to be able to share variables between SPIN and assembly. I have code that works but it isn't pretty:

VAR
long ReadFromHere
long WriteToHere
PUB
COGNEW(@AssemblyCog, @ReadFromHere)
DAT
org
AssemblyCog
mov Ptr, PAR
rdlong Look, Ptr
add Ptr, #4
wrlong Look, Ptr
Ptr long 0
Look long 0

What I really want is something like this:

AssemblyCog
mov Ptr, @ReadFromHere
rdlong Look, Ptr
mov Ptr, @WriteToHere
wrlong Look, Ptr

Isn't there a way to do this so my code will be more readable?

Thanks!

Comments

  • dnaddordnaddor Posts: 26
    edited 2007-05-02 15:58
    Sorry about the lousy formatting -- the real code has spaces but these got lost when I pasted it!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-02 17:01
    Your second code doesnt work because you have violated the rules of scope. The hub memory is a different scope than the cog memory, in theory if you used [url=mailto:#@ReadFromeHere]#@ReadFromeHere[/url] it could work, but that fails for a different reason: the source field of the instruction can only hold 9 bits. Unless you have located your variables in the first 512 locations in main memory, it wouldn't work and even if you did it is considered a violation of the object like nature of the language since you do not have control over where objects are placed in memory.

    If all you are looking for is code readability you can do something like this:
    CON
      NextArgument = 4
     
    VAR
      long ReadFromHere
      long WriteToHere
    
     
    PUB
      COGNEW(@AssemblyCog, @ReadFromHere)
    
     
    DAT
         org
    
     
    AssemblyCog
    
     
         mov Ptr, PAR                 'Point to ReadFromHere
         rdlong Look, Ptr             'Read ReadFromHere
         add Ptr, #NextArgument       'Point to WriteFromHere
         wrlong Look, Ptr             'Write WriteToHere
    
    Ptr  long 0
    Look long 0
    
    
    

    Of course the #1 means for increasing the readability of code is the liberal use of comments.

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

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 5/2/2007 5:05:48 PM GMT
  • dnaddordnaddor Posts: 26
    edited 2007-05-02 17:20
    Thanks for the quick reply.

    Yes, I meant #@ReadFromHere instead of @ReadFromHere. And yes, I see how the value #@ReadFromHere would exceed the 9-bit limit.

    I oversimplified my example. In the real application, VAR is a large collection of different sized values:

    VAR
    long Encoder
    long Instant
    long SomeTable[noparse][[/noparse]20]
    long Magic
    long BiggerTable[noparse][[/noparse]80]

    I was hoping for a way to access these elements BY NAME from the assembly code, rather than constantly offsetting from PAR and hoping that I was offsetting properly.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-02 18:45
    What you are specifically looking for isn't possible, think of it as writting a C subroutine comprised of assembly inside it. In order to get your arguments from the stack you need to do the same thing, access them via offsets of the stack pointer.

    Now if you are willing to sacrifice a long for each variable in your cog you can do:

    PUB
      EncoderPtr := @Encoder
      InstantPtr := @Instant
      SomeTablePtr := @SomeTable
      MagicPtr := @Magic
      BiggerTablePtr := @BiggerTable
      Cognew(@AssemblyCog, 0)
     
     
    DAT
    AssemblyCog
        rdlong EncoderVal, EncoderPtr
    ...
        wrlong EncoderVal, MagicPtr
    ...
     
    EncoderPtr
        long 0
    InstantPtr
        long 0
    SomeTablePtr
        long 0
    ...
    EncoderVal res 1
     
     
    

    But that depends on how much space you have in your cog.

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

    Parallax, Inc.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-02 19:07
    http://forums.parallax.com/showthread.php?p=647408

    Step 3 shows a practical example.

    Graham
  • dnaddordnaddor Posts: 26
    edited 2007-05-02 19:22
    This is exactly what I was looking for. Thanks, Paul. Thank you also, Graham.
Sign In or Register to comment.