Can't get bptr to work

LEH

Member
I have tried something like the following:
let bptr = $55
let blu = $F0
let red = $A0
poke @bptrinc, blu
poke @bptrinc, red
let bptr = $55
peek @bptrinc, blu
peek @bptrinc, red
If I run this both blu and red = 240 and the memory does not contain $F0 or $A0 at addr $55.
picaxe 20X2
 

AllyCat

Senior Member
Hi,

@bptr is a direct variable, you don't need to poke or peek it; the correct syntax is @bptr = blu and (e.g.) sertxd(#@bptr). Don't forget that the (optional) inc or dec occurs after the expression is evaluated.

You can use poke bptr, blu if you wish (but not with inc or dec added), however the advantage of the @ format is that it can be included directly in other expressions or commands. You can even poke @bptr,... (which is why it's not flagged as a syntax error) but as you've discovered this "double indirection" probably won't do what you expect.

Cheers, Alan.
 
Last edited:

lbenson

Senior Member
Furthermore, if the value at address $55 is 0, as it would be when a program is first run, then after "POKE @BPTRINC, blu", the value of byte 0 in RAM, B0, will be $F0, and the value of bptr will be $56. The value at address $56 will also be zero, so after "POKE @BPTRINC, red", the value of byte 0 will be changed to $A0.

Then you PEEK the value at B0 back into both blu and red, leaving them both at $A0.

You can see this by running your program in the simulator, single stepping, and looking at ram.

(But double indirection is a neat idea--perhaps someone will come up with a use for it--stepping through an array of pointers.)
 

hippy

Technical Support
Staff member
(But double indirection is a neat idea--perhaps someone will come up with a use for it--stepping through an array of pointers.)
That's about the only use I could think of. This example allows a list to be created in RAM from address 16 upwards which is a list of other RAM addresses to set to a value. Simulate the code and, when finished, RAM will show as addresses 96, 97 and 104 having been set to $FF ...

Code:
#Picaxe 20M2

bPtr     = 16   ; Start address of list
@bPtrInc = $FF  ; Level
@bPtrInc = 96   ; Set location 96
@bPtrInc = 97   ; Set location 97
@bPtrInc = 104  ; Set location 104
@bPtr    = 0    ; End of list

bPtr = 16       ; Start address of List
b0   = @bPtrInc ; Level to set
Do Until @bPtr = 0
  Poke @bPtrInc, b0
Loop
 
Top