PDA

View Full Version : Peek & Poke...



ReneMiner
19-11-2015, 13:05
Everyone knows them.
For backward-compatibility we need them but they read so 1982 and they are developed in times there were only Byte's to Peek & Poke. (not to mention DPeek & DPoke :unsure: )


Currently we do the following:



Uses "console"

String test = "This be a test" ' some data anywhere
Long i

' usually we peek single elements like this:

For i = 0 To Len(test) - 1
Print Chr$( Peek( Byte, StrPtr(test) + i ) ) & "_"
Next
PrintL

' and poke elements of some Type (here Byte) as this:

Poke(Byte, StrPtr(test) + 5, &H69) ' "i" , 105
Poke(Byte, StrPtr(test) + 6, &H73) ' "s" , 115

PrintL "after changing some data:"

For i = 0 To Len(test) - 1
Print Chr$( Peek( Byte, StrPtr(test) + i ) ) & "_"
Next
PrintL

PrintL Repeat$(3, $CRLF)
PrintL "press a key"
WaitKey
PrintL

PrintL "In thinBasic we can use virtual variables At Ptr..."

' -------------------------------------
Dim vByte As Byte At 0
' -------------------------------------


test = "This be a test again"

For i = 0 To Len(test) - 1
' we place the virtual Byte onto position:
SetAt(vByte, StrPtr(test) + i )
' and request it's value - because we don't assign any:
Print Chr$( vByte ) & "_"
Next
PrintL

' now we place the virtual Byte onto position
SetAt( vByte, StrPtr(test) + 5)
' and assign some value
vByte = &H69
' once again, place vByte onto position:
SetAt( vByte, StrPtr(test) + 6)
' and assign a value
vByte = &H73

PrintL "after changing some data:"

For i = 0 To Len(test) - 1
' place the virtual Byte onto position
SetAt(vByte, StrPtr(test) + i )

' we obviously request the value - because we dont assign any
Print Chr$( vByte ) & "_"
Next

PrintL Repeat$(3, $CRLF)
PrintL "press a key to end"
WaitKey



Now what, if we omit to give that virtual variable a name because we dont need it any more after we used it once?
Simply treat data that is supported by Peek & Poke at some memory-position as desired typename (Byte, Long, Word etc.)


<typename> At <pMem>
interpret Memory At pMem As typename Once


As this:



' example syntax, do not run this !

String test = "This be a test"
Long i

For i = 0 To Len(test) - 1

' request the Byte At position:
Print Chr$( Byte At StrPtr(test) + i ) & "_"

' -----------------------------
<Type> At <Ptr>

' -no asignement- means

Peek(<Type>, <Ptr>)
' -----------------------------


Next


Byte At StrPtr(test) + 5 = &H69 ' see lines 15 & 16 in example above
Byte At StrPtr(test) + 6 = &H73
' would equal
' Word At StrPtr(test) + 5 = &H7369

' ...


' so
' -----------------------------
<Type> At <Ptr> = <NumericExpr>

' -assigning a Value- means

Poke(<Type>, <Ptr>, <NumericExpr>)

' for +=, -=, *=, /=, \= which are assignements too,
'like
<Type> At <Ptr> { += | -= | *= | /= | \= } <NumericExpr>


' it means
Poke(<Type>, <Ptr>, Peek(<Type>, <Ptr>) { + | - | * | / | \ } <NumericExpr>))
' -----------------------------

' example

' imaginary preparation of the readers mind:
' >>> Dword pMem = BRAIN_Alloc(SizeOf(Long))
' >>> pMem now should point some valid memory position in your brain ;)
' >>> not to explode on Invalid Memory Access as Petr's did below :D :D :D

' assign:
Long At pMem = 123
' equals
Poke(Long, pMem, 123)
' or
Memory_Set(pMem, MKL$(123))

' request
PrintL Str$( Long At pMem )
' same as current
PrintL Str$( Peek( Long, pMem ) )
' or
PrintL Str$(CVL(Memory_Get(pMem, SizeOf(Long)) ) )


' assign +
Long At pMem += 234
'equals then

Poke(Long, pMem, Peek( Long, pMem ) + 234 )

' ...


As mentioned, only for primitive numeric types that work with Peek & Poke
(NO Peek$/Poke$ !!! - instead of String At we have Memory_Get & Memory_Set)

Petr Schreiber
19-11-2015, 13:56
At first, my brain exploded, but when I managed to collect the pieces back together, it makes sense!


Petr

ErosOlmi
19-11-2015, 21:15
Interesting.

Quite easy to develop the SET: <type> At <pMem> = something
Quite difficult to develop GET: <type> At <pMem> in Math expressions

I will think about it.

ReneMiner
20-11-2015, 13:17
Interesting.

Quite easy to develop the SET: <type> At <pMem> = something
Quite difficult to develop GET: <type> At <pMem> in Math expressions

I will think about it.

perhaps in math-expressions we could use parenthesis as



Long At pResult = ( (Long At p1) + (Long At p2) ) * 12345...