Results 1 to 4 of 4

Thread: Peek & Poke...

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170

    Peek & Poke could become <typename> At <position> ?

    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 )


    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)
    Last edited by ReneMiner; 19-11-2015 at 16:05.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    At first, my brain exploded, but when I managed to collect the pieces back together, it makes sense!


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,529
    Rep Power
    170
    Quote Originally Posted by ErosOlmi View Post
    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...
    
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Brief peek at VB.NET development
    By Petr Schreiber in forum Other languages
    Replies: 0
    Last Post: 14-05-2014, 21:38
  2. Replies: 2
    Last Post: 09-04-2013, 16:48
  3. cast convert / varptr / peek
    By largo_winch in forum thinBasic General
    Replies: 3
    Last Post: 28-09-2012, 11:39

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •