Shop OBEX P1 Docs P2 Docs Learn Events
I need PBASIC 2.5 help. — Parallax Forums

I need PBASIC 2.5 help.

nightridernightrider Posts: 9
edited 2007-03-04 21:31 in BASIC Stamp
Hello all,
I'm learning PBASIC 2.5 from a book called PROGRAMMING AND CUSTOMIZING THE BASIC STAMP COMPUTER and I ran into this situation.· The following code produces the output under it:

code
' {$STAMP BS2}
' {$PBASIC 2.5}


_x· VAR Byte
_y· VAR Byte
_z· VAR Byte
_area VAR Word
_x = 20
_y = 30
B0 = 155
B1 = 220
W2 = B0 + B1

_area = _x*_y· 'solve the area

DEBUG "the Area is ", DEC _area, "sq ft."
DEBUG CR, ? B0, ? B1, ? B2, ? W2
END
======================================
OUTPUT:
the Area is 600sq ft.
B0 = 88
B1 = 2
B2 = 20
W2 = 375
=====================================
My questions are:

-why is B1 = 2 when I declared it '220'?
-why is B2 = 20 when I didn't load it with any values?
-W2 is giving me the correct sum, but why are the B variables displaying strange results?



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you,


Alex Cordero
San Diego, Ca.
bender.gif
"what?"

robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.

"Fill your boots, man!"
-Irish saying

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-04 14:58
    Basically, you can't combine the VAR method of allocating variables with explicit reference to the specific variables like B0, B1, W2 because the compiler may use the variables you're referencing. In other words, when you write "_x VAR byte", the compiler chooses a byte variable (from B0-B25) and associates the name "_x" with it. You don't know which variable it uses, but it probably allocates word variables first starting at W0, then allocates byte variables starting at the next available byte, so "_area" is probably assigned to W0, "_x" to B2, "_y" to B3, and "_z" to B4.
  • ratronicratronic Posts: 1,451
    edited 2007-03-04 15:08
    As recently pointed out to me(thank god) you can use the get and put commands to store 126 bytes of scratchpad ram built into chip and they dont effect the system variabls (26 of them).
    Good Luck , Dave.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Fix it if ain't broke said...
    (replace this text with what was said)
    D Rat
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-04 16:23
    ratonic,
    The scratchpad ram does not exist on the BS2 and is only 64 bytes on the BS2e and BS2sx.
  • nightridernightrider Posts: 9
    edited 2007-03-04 17:36
    Mike. So what is the point of having the option to initialize customized variables when BS2 has predefined memory allocations?


    NOTE:
    it may help to know that my programming experience is HTML/Javascript and Perl. I have no experience is writing code to access hardware. So I don't know how Pbasic communicates with BS2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thank you,


    Alex Cordero
    San Diego, Ca.
    bender.gif
    "what?"

    robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.

    "Fill your boots, man!"
    -Irish saying
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-03-04 18:04
    Hi Alex, you can actually get a visual of what Mike is saying .If you enter your code one line at a time and each time you write a line look at the memory map ( Ctrl & M ). You should see the memory fill from Reg0: (W0) upward. After declaring your first 4 variables (3 bytes and a word) you have already used the memory locations B0 B1 B2 B3 B4. Also as you enter each variable the memory map rearanges itself so that the largest variables are in the lowest address and the smallest variables are at the highest eg. word-byte-nibble-bit. Therefore the variable _x may have initially been B0 but when you add the variable word _area then byte _x becomes B2. It is much simpler to keep track of variables by name rather than by address.

    Jeff T.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-04 21:31
    nightrider,
    The reason for both is that the VAR declaration came later in the development of the PBasic editor/compiler than the simple predefined names and, when the VAR declaration was added, the other was left in for compatibility reasons.
Sign In or Register to comment.