Shop OBEX P1 Docs P2 Docs Learn Events
Memory map - Constants — Parallax Forums

Memory map - Constants

RickHRickH Posts: 40
edited 2008-05-20 03:04 in BASIC Stamp
Why do I not see constants in memory map?
I keep adding constants and nothing changes in memory map. I am asuming they are stord in the EPROM but like I said I see no changes as I add them. I have added every type %11111111, $DD, "ABC" and 128. None of them individualy or all at the same time change my memory map.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-05-18 09:02
    Constants don't get compiled into memory unless you use them in your program. When you do, their values get embedded in the code where they occur, just like they would if you used a literal number instead.

    -Phil
  • RickHRickH Posts: 40
    edited 2008-05-18 11:13
    Ahh, I see. So when thier used what size are they in memory? would it still break down into bit, nib, byte, word? or would %111 be 3 bits?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-18 15:15
    What size is 5? What size is 136? It depends on how it's used. What about zero? What about expressions like: 3178 - 3003 ?
  • RickHRickH Posts: 40
    edited 2008-05-19 03:04
    I'm just asking if I declare %111 if it alocates a nible or 3 bits. Why are you asking me questions to answer mine. No wher in the documentation have I found Memory allocations for constants. Please be cleareer in replying to my posts.

    Post Edited (RickH) : 5/19/2008 3:09:34 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-19 04:08
    I'm asking you questions to get you to think.

    If you declare a constant like %111 like "something CON %111", it doesn't allocate anything. The name "something" just becomes another way of writing "%111", a way that may have more meaning for you than just writing "%111".
  • RickHRickH Posts: 40
    edited 2008-05-19 05:38
    OK so then I'm assuming that it is what the name is. If I declare a constant of $11 then the Amount of memory used is only that of the value, I didn't think that would be the case. A constant in VB is a Pointer to a memory location but the alocation is the question. I asumed if I used 7 bits of data say like %1100000 in and integer format = 96 that it would take up a byte and not 7 bits.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-05-19 14:14
    Because a "CON" entry is like a compiler directive -- the CON entry itself makes an entry into the IDE symbol table, but doesn't actually 'take up space' in the eeprom.

    Now, when you USE the CON, THAT takes up space. An example:

    MyBaud CON 16468 ' CON 'entry', takes up no space in eeprom

    SEROUT 16, MyBaud, [noparse][[/noparse]"Hi", CR] ' Okay, HERE some space is used to 'instantiate' the CON values. The 'Word' value 16468 is plugged in where "MyBaud" is.

    That being the case, I'm pretty sure most "CON" instantiatiations take up a BYTE (if 0 to 255) or a WORD (if 256 or above) in the eeprom.

    Post Edited (allanlane5) : 5/19/2008 2:19:18 PM GMT
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-05-19 16:43
    The PBASIC tokenizer operates bit by bit, so in fact the CONstant %11, when compiled into code, will take up less space than %111, %1111, etc.

    You can use the memory map to look explore the effect using a simple program:
    x VAR Word   ' try different sizes, bit, nib, byte word
    x = %11   ' try different values
    



    for example, if you try x=%11, then x=%111 and x=%1111, and look at the memory map, you will see the program grown by one bit at each step. Bring up the memory map (CTRL-M), and remember that the size of the program grows from high memory downward:

    %1100 1101   $CD   for x=%11
    %0110 0110 1000 0000     $66 $80   for x=%111        
    %0011 0011 0100 0000    $33 $40    for x=%1111
    



    The program size is indicated by the position of the final 1 on the right, although a couple of zeros after that are significant and indicate end of statement and end of program.

    If you change x VAR WORD to a smaller chunk, i.e., BYTE, NIB or BIT, the program will become longer. That is because the pointer to select the chunk becomes longer, one of 13 words, one of 26 bytes, one of 52 nibs, one of 208 bits.

    PBASIC treats powers of two as an exceptional case. If you try x=%1 and then x=%1000000000000000, you will find that the space required is the same (for all powers of two) and it is as compact as it can get. I quite often take advantage of that in my programs. For example, if I need a timeout of about 1 second, I will use 1024 instead of 1000. Saves seven bits.

    1001 1010   $9A      x=1024
    1110 1100 1100 1100  $EC $CD    x=1000
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 5/19/2008 4:49:32 PM GMT
  • RickHRickH Posts: 40
    edited 2008-05-20 03:04
    ahh, verry cool. This is a verry good demonstaration. Thank you, I am working on my code optimization as we speek. This power of two concept may come in handy for me at some point.
Sign In or Register to comment.