Shop Learn
Question about cogram — Parallax Forums

Question about cogram

Hi again all,

Today I have a question about cog ram...

Why do some of the programs I've seen declare cogid as a long? when a byte would suffice?

'instead of this
Byte Cogid

'I see this more often
Long Cogid

Comments

  • Wuerfel_21Wuerfel_21 Posts: 2,154
    edited 2022-01-10 20:10

    There's two reasons:

    • Programmers that just don't care about those 3 bytes
    • Programmers that do care but realize that the long variable actually saves space: The first 8 long-sized variables in an object have special spin opcodes that take one less byte per use. Since the cog id is usually used at least thrice, that makes up for the extra variable space if the object is only instantiated once. Additionally, the variable space allocated for an object is rounded up to the nearest long, so if there are no other word/byte sized variables, it doesn't make a difference to that size.

    tl;dr; use a long to store cogid, it's fine.

  • I've always added 1 to whatever cognew returns and stored it in a byte. I think that's pretty much standard procedure across the board.

    -Phil

  • AribaAriba Posts: 2,567

    @JaanDoh said:
    ...
    Why do some of the programs I've seen declare cogid as a long? when a byte would suffice?

    That makes the code future proof for the 4294967295 cog Propeller. ;-)

  • JaanDohJaanDoh Posts: 123
    edited 2022-01-11 14:03

    @Wuerfel_21 said:
    There's two reasons:

    • Programmers that just don't care about those 3 bytes
    • Programmers that do care but realize that the long variable actually saves space: The first 8 long-sized variables in an object have special spin opcodes that take one less byte per use. Since the cog id is usually used at least thrice, that makes up for the extra variable space if the object is only instantiated once. Additionally, the variable space allocated for an object is rounded up to the nearest long, so if there are no other word/byte sized variables, it doesn't make a difference to that size.

    tl;dr; use a long to store cogid, it's fine.

    @Wuerfel_21, Thank you, I came across mention of the first 8 longs declared in a program? (program or object I don't know), and also the first 8 parameters in a function call, and how access was faster to those.

    Right now i have a mixture of bytes and longs for storing the cogid, this difference is in the various objects, where in one it maybe long and in another it maybe byte.

    I had thought initially that it had something to do with accessing that register from within pasm programs and then I realised you can address any register via Byte[address], Word[address] and Long[address] so that made me ask here in the forum.

    I'd like to use a byte field for the cogid an wonder if the sample programs from the library would be affected if I change the field type for cogid from Long to Byte.

    Its crazy because one question leads to two or more new ones lol...

    @"Phil Pilgrim (PhiPi)" , Thank you, That sounds like how I wish to implement the cogid into my program, as byte not long, once i get it work, i'll try and look at changing the ones currently defined as long to byte. I'm a little unsure if it will break the existing code (from the samples in the library)...u

    @Ariba , Thank you, I'd best get a move on if I'm to get the hang of this, All the time I read about other members exploits, tests and research into the P2 and I'm just amazed at some of the stuff and wish desperately to master spin, and then move on pasm (I wish..) lol

Sign In or Register to comment.