-
Notifications
You must be signed in to change notification settings - Fork 2
Pointers
To understand pointers, you have to understand how memory works. First, every byte of memory has what is called an address. An address, is a pointer to that byte. For example, the first byte of memory is at address 0, the second byte is at address 1, et cetera. On these calcs, there are 65536 bytes of memory addressed at a time. The last 32768 bytes are RAM. This is where your program and everything in it is stored. This has some powerful implications. If you have a pointer to a section of code and you tell Grammer to start executing there, it can jump there immediately. If you have a pointer to a string, you can use that pointer to draw the string or use it. This means you don't have to create any external variables for your strings or sprites. If you want to create an appvar for save data, having the pointer to that lets you edit the data, save to it, or read from it. Pointers are powerful. As such, you will probably be using them frequently. It is not coincidence that Grammer vars hold 16 bits and pointers are 16-bit.
Vars are two byte values. These can hold a 16-bit number, so these are well suited to holding pointers. These are all the letters A
to Z
and θ
and A'
to Z'
and θ'
. For readability, you can use the lowercase letters instead of A'
, for example.
How do you store to these? Like all BASIC programmers know, use →
. For example: 3→A
Likewise, for a string: "Hello World→A
Don't be fooled, that does not store the string anywhere. It just stores where the string is located in A.
So if you change the byte at A, you will change the "H" in your program. If you want to try it, run this program and then check the source again.
:.0:
:"HELLO WORLD→A
:int(A,47
:Stop
You will see that after running the program, it now says "QuartReg ELLO WORLD"
! That is because we changed the first byte of the string to the value 47
, which corresponds to the token QuartReg
. Be careful! Not all tokens are one byte-- lowercase letters are a notorious example of two-byte tokens.
Pointers are also useful with labels or finding programs. Anything that requires searching, actually. For example, to goto a label, you would do something like Goto Lbl "HI
and that would jump to the label named .HI
. You can also get a pointer to this label. If you need to jump to that label often, this saves lots of time by not having to search for the label every time. Remember, everything is math in Grammer:
:.0:Return
:Lbl "HI→A ;Locates the label .HI and stores its pointer to A.
:Repeat getKey(15 ;`getKey(15` returns 1 if clear is being pressed
:prgmA ;This calls the subroutine pointed to by A
:End
:Stop
:.HI
:B+1→B
:Text('0,0,B ;Displays the number B at (0,0)
:DispGraph
:End ;End the subroutine
Remember to always End
your subroutines! Now I want to take time to finally start explaining some code. Labels can be a bit more descriptive than in BASIC. You can use up to 42 bytes for a name, technically, but try to maintain readability. You can also use pretty much whatever tokens you want in a label name. For example, I had a label named ICircle(
that I had draw inverted filled circles. Lbl
takes a string argument where the string is the name of a label. It returns the pointer to the line after the label.