Shop OBEX P1 Docs P2 Docs Learn Events
How to declare variable at specific ram position in VAR block — Parallax Forums

How to declare variable at specific ram position in VAR block

Christofer AChristofer A Posts: 9
edited 2008-05-14 21:14 in Propeller 1
Hi all,

I am looking for a way to declare variable at specific ram position, to be able to read and wright to variable without knowing its name. maybe using word[noparse][[/noparse]$1234] if 1234 is the ram position.

Or is there a way in spin to find out a variables ram position?

Any suggestions?

Thanks in advance.
Christofer

Comments

  • tpw_mantpw_man Posts: 276
    edited 2008-05-14 20:46
    To find out a variable's position in RAM, you can use the @ operator. It returns the address of the variable in hub RAM. If you want to 'declare' a variable somewhere in RAM at runtime, you can just use a constant with the address, but be very sure to check if the address is occupied or could be occupied. If so, it will cause huge problems!
    Examples:


    'to get the address of a variable and assign 1 to it
    
    PUB main | var, addr_var
    addr_var := @var
    long[noparse][[/noparse]addr_var] := 1
    'or
    long[noparse][[/noparse]@var] := 1
    'for a locally declared variable (not using the VAR section), it is always a long
    
    'to get the address of a global (in VAR section) variable and assign 1 to it
    VAR
    word var 'can be word, long, or byte. just make sure you address it as so
    
    PUB main | var_addr
    var_addr := @var
    word[noparse][[/noparse]var_addr] := 1
    
    
    'to just "assign" a variable in ram and assign that position a 1
    CON
    var_location = $7000 
    
    PUB main
    long[noparse][[/noparse]var_location] := 1
    'you can use byte, word, or long as well
    
    
    
    



    You MUST use either a word or a long to hold the address as it is 16 bit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I am 1011, so be surprised!


    Advertisement sponsored by dfletch:
    Come and join us on the Propeller IRC channel for fast and easy help!
    Channel: #propeller
    Server: irc.freenode.net or freenode.net
    If you don't want to bother installing an IRC client, use Mibbit. www.mibbit.com
    tongue.gif

    Post Edited (tpw_man) : 5/14/2008 8:51:24 PM GMT
  • Christofer AChristofer A Posts: 9
    edited 2008-05-14 21:14
    brilliant, thank you very much.
    Also thanks for the quick reply [noparse]:)[/noparse]

    //Christofer
Sign In or Register to comment.