POINTER() and MWHEEL Questions

All aspects of programming on the Commander X16.
Post Reply
mortarm
Posts: 320
Joined: Tue May 16, 2023 6:21 pm

POINTER() and MWHEEL Questions

Post by mortarm »

MWHEEL: Can anyone give an example on the usage of this variable? The docs don't have one.

POINTER(): The docs say, "Returns the memory address of the internal structure representing a BASIC variable." Can someone define "internal structure", or tell me where I can find info on this?

Thanks.
Stefan
Posts: 465
Joined: Thu Aug 20, 2020 8:59 am

Re: POINTER() and MWHEEL Questions

Post by Stefan »

MWHEEL contains the mouse wheel movement +127/-128 since it was last retrieved. Mouse wheel support is not available in the last release; it’s currently only in the Github master branch
DragWx
Posts: 362
Joined: Tue Mar 07, 2023 9:07 pm

Re: POINTER() and MWHEEL Questions

Post by DragWx »

POINTER() returns the memory address where a BASIC variable's value is stored. The way a variable's value is stored in memory is different from what you get when you PRINT it, which is what the docs mean when it says "internal structure".

For precise information on variables, you can check C64-Wiki, which I'll summarize here:
  • All variable values are 5 bytes.
  • Floating point numbers: 1 byte exponent, 4 bytes (MSB first) mantissa.
  • Integers: 2 bytes (MSB first) for signed 16-bit integer, 3 zero bytes.
  • Strings: 1 byte for length, 2 bytes (LSB first) pointer to characters, 2 zero bytes.
  • Arrays: Structure is the same as non-array variables, except each value in the array is stored without needing the padding bytes. (For example, an array of 5 integers will consume only 10 bytes of memory.)
    • If you DIM AY(5), remember to use POINTER(AY(0)) to get the starting address of the array. If you accidentally use POINTER(AY) instead, you'll get the address for a plain AY variable and not the AY array.
mortarm
Posts: 320
Joined: Tue May 16, 2023 6:21 pm

Re: POINTER() and MWHEEL Questions

Post by mortarm »

DragWx wrote: Wed Oct 04, 2023 6:51 pm POINTER() returns the memory address where a BASIC variable's value is stored.
That was my initial thought as well, but when I tried to look up the address using Codex, the string I used could not be found. Does Codex clear out RAM when started?
DragWx
Posts: 362
Joined: Tue Mar 07, 2023 9:07 pm

Re: POINTER() and MWHEEL Questions

Post by DragWx »

That's because, as outlined in my previous post, the characters of the string are not stored at the address you get from POINTER(). Instead, a string variable contains one byte for the string length, and then a 16-bit pointer to another memory address which contains the actual characters within the string:
x16_pointer_string_memory.png
x16_pointer_string_memory.png (32.46 KiB) Viewed 2701 times
mortarm
Posts: 320
Joined: Tue May 16, 2023 6:21 pm

Re: POINTER() and MWHEEL Questions

Post by mortarm »

DragWx wrote: Thu Oct 05, 2023 5:44 pm That's because, as outlined in my previous post, the characters of the string are not stored at the address you get from POINTER().
Yep, you sure did. Don't know why it didn't click the first time. Thanks for breaking it down.

Couple things I noticed is that the pointer address changes depending on the length of the string, or whether you do it as a program or immediate mode. Examples:

PROGRAM IMMEDIATE A$="MORTAR" = $0825 $0805 A$="MORTAR TIME" = $082A $0805

Curious that the address doesn't change in Immediate mode.
DragWx
Posts: 362
Joined: Tue Mar 07, 2023 9:07 pm

Re: POINTER() and MWHEEL Questions

Post by DragWx »

BASIC stores all of the variables in memory after the tokenized program, so for example:
10 A$ = "IT'S MORBIN' TIME!"
20 PRINT HEX$(POINTER(A$))
READY.
RUN
0834

READY.
Look what happens when a space in line 10 is removed:
10 A$= "IT'S MORBIN' TIME!"
20 PRINT HEX$(POINTER(A$))
READY.
RUN
0833

READY.
A$'s location moved by one byte, because we changed the size of the program by one byte.
And now if a new variable is added in direct mode after running this program:
B$ = "BAYBEEEEEE!"

READY.
PRINT HEX$(POINTER(B$))
083A

READY.
B$ is placed in the next available space in variable memory, which is right after A$.
(Please note, I did say all named variables have values that are 5 bytes, but that value will be preceeded by two bytes for the variable's name (two characters) and type (bit 7 from both characters), so that's why B$'s value is 7 bytes after A$'s value.)

If you clear the program, then create a new variable in direct mode:
NEW

READY.
A$ = "THEY SEE ME MORBIN'"

READY.
PRINT HEX$(POINTER(A$))
0805

READY.
...now you get address $0805, since now the current BASIC program is just a blank program, and A$ is a new variable created after our tokenized blank BASIC program.
mortarm
Posts: 320
Joined: Tue May 16, 2023 6:21 pm

Re: POINTER() and MWHEEL Questions

Post by mortarm »

Gotcha. But, what if the program size doesn't change, just the string size, as noted previously?
DragWx
Posts: 362
Joined: Tue Mar 07, 2023 9:07 pm

Re: POINTER() and MWHEEL Questions

Post by DragWx »

If you start with this:
10 A$ = "CHEESEBURGER"
...and you later change that line to this:
10 A$ = "BACON CHEESEBURGER"
...then that still counts as changing the program size, because line 10 now has six more characters in it, which means your entire program is now six bytes larger than before.
mortarm
Posts: 320
Joined: Tue May 16, 2023 6:21 pm

Re: POINTER() and MWHEEL Questions

Post by mortarm »

Doh! It takes a bit, but I finally got it. Thanks again.
Post Reply