4 ms·
I could read and write commodore 128 assembler before I started to learn C, so I knew about adresses, adressing modes and how to use them. Despite that, I still
by Corazoor 6y ago
I could read and write commodore 128 assembler before I started to learn C, so I knew about adresses, adressing modes and how to use them. Despite that, I still had trouble wrapping my head around the concept of pointers at first.
There are multiple things that can make it confusing, and they kinda collude to make it harder to get.
In order to understand them at all, you have to know
- what a variable represents (a region of memory)
- what an adress/reference is (the index of said region in memory)
- how a value is stored in and read from said variable (by putting it into or reading from the memory beginnen at this adress)
- that you actually can treat an adress as a value, i.e store it in another variable
Thats already four deeply related, but still different things you have to wrap your head around.
To make matters worse, the way pointers often are used (and most of the time introduced), is to point to another variable. Now you have two adresses, and two values, one of wich is one of the adresses. If that sentence confuses you, that is how beginners feel about it as well!
Dereferencing is not straightforward either:
It makes a pointer behave as if it were the pointed to variable, which in the usual introductory toy examples makes it look like a totally unnecessary and useless operation.
Also the size of the pointed to variable is encoded via the pointers type, and does not have to match the size of the actually pointed to thing. Another mindblower that one.
Lastly there is the linguistic problem that we tend to say a variable IS its value. Which is actually misleading in multiple ways if a variable contains the adress of another one.
Ultimately I had to understand what pointers are NOT, in order to finally grasp what they are.
- pjmlp 6y agoDid the 128 Assembler not support declaring variables? I was already using variables, arrays and structs on MS-DOS/Amiga macro Assemblers.
- Corazoor 6y agoWhile there were macro assemblers, all I did use was the built-in machine monitor, which was basically a system provided debugger. But you could assemble single mnemonics into a memory location of your choosing, so I would put programs together directly in memory, one instruction at a time. That required counting their length as well, and doesn't support any constructs that span multiple instructions, like macros or variables... I knew variables already from commodore Basic, which i learned first. But there was no concept of pointers, direct interaction with memory happened via peek and poke, and the adress space was so small, you just chose an adress where to put your stuff and worked with offsets. It is worth remembering that on these old machines there was no OS managed memory allocation... So there was much less need to work with adresses unknown at "compile time".
- pjmlp 6y agoAh ok, I only got into Assembly when moving into 16 bit computers, on my Speccy I hardly did any Z80 coding, just a couple of Timex Basic stuff and mostly playing. So I never got to play with 8 bit Assemblers.