Shop OBEX P1 Docs P2 Docs Learn Events
byte pointer memory access — Parallax Forums

byte pointer memory access

David BetzDavid Betz Posts: 14,516
edited 2007-12-24 19:12 in Propeller 1
I've been trying to write some simple SPIN code to access an array of bytes through a pointer and am having some trouble understanding the difference between the two methods of doing this. I've been trying to use the following code. It seems that test2 does what I expect but test1 does not. Shouldn't they be equivalent?

Thanks!
David

VAR

byte data[noparse][[/noparse]10]
long ptr

PUB init
ptr := @data
test1(2)
test2(3)

PUB test1(i)
ptr.byte := 1

PUB test2(i)
byte[noparse][[/noparse]ptr] := 1

Comments

  • CardboardGuruCardboardGuru Posts: 443
    edited 2007-12-24 15:07
    David Betz said...
    Shouldn't they be equivalent?

    No they shouldn't.

    ptr.byte is accessing the least significant byte of the actual pointer. Same as ptr.byte[noparse][[/noparse]0] would do.

    byte[noparse][[/noparse]ptr] is accessing the byte that ptr is pointing to.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Help to build the Propeller wiki - propeller.wikispaces.com
    Play Defender - Propeller version of the classic game
    Prop Room Robotics - my web store for Roomba spare parts in the UK
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2007-12-24 15:19
    David,
    Is it not this simple ?

    var

    byte data[noparse][[/noparse]10]
    byte x

    pub read_em_all | ptr
    repeat 10
    x := byte [noparse][[/noparse] @data ] [noparse][[/noparse] ptr++ ]
    'do something with x

    I can't even imagine what is going on here........."ptr.byte" , I will leave that to others

    Ron
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-24 15:40
    Ron,
    I have not the slightest idea what you are on to... You use an over-complex construction; and please note that ptr is NOT initialized to zero!!!!


    X.BYTE[noparse][[/noparse]n] is someting you do with the "special registers" as
    OUTA[noparse][[/noparse]8*n .. 8*n+7]

    Post Edited (deSilva) : 12/24/2007 6:34:39 PM GMT
  • David BetzDavid Betz Posts: 14,516
    edited 2007-12-24 18:20
    Okay, thanks for your help. I see now that they aren't the same and that the one I chose to use for memory access (the foo.byte[noparse]/noparse syntax) was exactly the wrong choice. My program is working now thanks to the help I've gotten here!

    Another probably dumb question:

    How do I continue a statement on a second line. For instance, I'd like to split up some of my method calls over several lines if they have a lot of parameters.

    foo(a,b,c,d)

    becomes

    foo(
    a,
    b,
    c,
    d)

    or something like that. Is this possible?
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-24 18:35
    David Betz said...
    ...

    foo(
    a,
    b,
    c,
    d)

    or something like that. Is this possible?
    With difficulties smile.gif
    foo(       {
    }  a,      {
    }  b,      { 
    }  c,      {
    }  d)
    


    Not really aesthetic... But it comes handy for commenting things on that opportunity smile.gif
  • David BetzDavid Betz Posts: 14,516
    edited 2007-12-24 18:49
    Ugh. maybe we can convince them to allow lines ending with backslash to be continued on the next line or something like that.
  • Nick MuellerNick Mueller Posts: 815
    edited 2007-12-24 19:12
    > Ugh. maybe we can convince them to allow lines ending with backslash to be continued on the next line or something like that.

    Welcome to the land of indention-sensitive syntax!


    Nick, not a friend of the above! No, really not!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
Sign In or Register to comment.