Shop OBEX P1 Docs P2 Docs Learn Events
BS2 Fixed variables — Parallax Forums

BS2 Fixed variables

Joseph OsborneJoseph Osborne Posts: 13
edited 2010-07-31 19:28 in BASIC Stamp
I am trying to modify the Scribbler include program. It uses all fixed variables and seems to use all the variable memory. I want to change some variables, to do a DO WHILE loop, for instance, but find the counter gets overwritten. Obviously I don't understand something important.

I am looking for a technical discussion of how the BS2 stores fixed variables. I can't find anything in the manual. Can anyone point me to a technical manual or something similar?

Thanks,

Joseph Osborne

PS- my hat is off to Phil Pilgrim who wrote this amazing program.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-29 22:07
    The BS2 (and other Stamps) has 26 bytes of variable storage usable as 16-bit words, 8-bit bytes, 4-bit nibbles, and 1-bit values. There are predefined names for all of these, but the compiler also will allocate variables from among these and match similar storage units together so 8 1-bit values fit in one byte and 2 4-bit nibbles fit in one byte and 2 8-bit bytes fit in one 16-bit word. If you have more than 26 bytes of variables, you have to reuse some of them since most programs use many of their variables in only a small portion of the program and don't need to keep values in most of them. The compiler has aliases for this where you can define a new variable using the same storage as some other variable and you can define a new variable so it uses all or part of an existing variable of a larger size. See the help files (or the Reference Manual) for details on memory allocation.
  • Martin_HMartin_H Posts: 4,051
    edited 2010-07-30 01:09
    That looks like code produced by the Scribbler GUI. When I was actively programming the Scribbler I found that code a useful starting point to learn PBASIC. But since it is machine generated it can be hard to follow. I found the Scribbler demo program a little better for a starting point. Eventually I found it easier to start over and write my own functions to control each Scribbler feature and I fully understood the memory map of the program.

    Generally having an allocation scheme to help you track variables is a good idea. In my own program is use ascending variable names ACC, X, Y, Z. ACC is the most volatile and Z is the least volatile. In this code it looks like it is using numbered counter variables for similar purposes.

    Mike, somebody slipped carbon paper under your keyboard as you're in triplicate!
  • Joseph OsborneJoseph Osborne Posts: 13
    edited 2010-07-31 00:55
    Thank you to both of you. Is there a technical discussion somewhere of exactly how the compiler goes about allocating variable space if W0, W1, B0, B1, etc are used? If I declare a variable as WORD or BYTE rather than W3 or B22, then declare another variable as part of the larger one, e.g.

    Example VAR W0
    Example2 VAR Example.HIGHBYTE

    Vs

    Example VAR WORD
    Example2 VAR Example.HIGHBYTE

    Will it allocate the same space for Example2?

    Joseph Osborne
  • Martin_HMartin_H Posts: 4,051
    edited 2010-07-31 02:12
    I'm sure there's a discussion in the PBASIC manual www.parallax.com/dl/docs/prod/stamps/web-BSM-v2.2.pdf somewhere.

    Here's a quick answer to your question. B0 and B1 are contained within W0's low byte and high byte respectively. In your first example you are creating an alias to W0 and then creating an alias to its high byte. This explicitly uses memory location W0 for storage. In your second example you are relying on PBASIC to choose which word memory location to allocate for Example and then creating an alias for Example2 in the high byte. Which location PBASIC chooses depends upon the other variables defined by the program and their order.

    I prefer the second style of memory allocation because it lets PBASIC do all the jiggery-pokery so I can concentrate on program logic.
  • Joseph OsborneJoseph Osborne Posts: 13
    edited 2010-07-31 18:04
    "mixing fixed variables with automatically allocated variables (discussed in the next section) is an invitation to bugs. A fixed variable can overlap an allocated variable, causing data meant for one variable to show up in another! "

    "...for all BS2 models, fixed variables like B3 and W1 and any aliases do not show up on the memory map as memory used. The editor ignores fixed variables when it arranges automatically allocated variables in memory. Remember, fixed and allocated variables can overlap."

    This is what concerns me. If I use even one fixed variable, do I need to allocate the whole memory map to avoid unpleasant surprises? I am thinking I need to convert the whole list of declarations away from fixed unless I understand exactly how the compiler works.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-31 19:28
    Yes, you need to either use fixed variables and their aliases or compiler allocated variables and their aliases in any given program. Mixing the two schemes is an invitation for subtle errors. It's possible to mix the two schemes if you know what you're doing and you're very careful. I don't suggest it. A little bit of one-time work and you've eliminated that as a source of problems.
Sign In or Register to comment.