Peek functions, what do they look like?, writing my own

Just starting out? Need help? Post your questions and find answers here.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Peek functions, what do they look like?, writing my own

Post by superadnim »

Hi :)

I'm wondering what do the Peek functions actually do?, how do they work?, what do they look like in assembly?!

I figured a PeekL would be about a MOV and the like, but what about the PeekS?, does PB handle Strings in an enclosed fashion?.

I'd like to write my own peek functions for PB just for fun (learning purposes, new to ASM)

so far I've got a PeekL and PokeL working flawlessly (although they mess with the original pointer, I don't know how to solve this other than copy the original memory address to later on use it on the freememory() proc...

I had an idea, which is to do an ADD of 4 to the given memory address, do this on both peek and poke, that way I'd avoid messing with the original pointer, right?, would this be a good idea?.

Feedback please!!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Look in the PB docs under the topic "On Error" and inside there you will find DisASMCommand() and a nice little example of how to use it to make your own mini-disassembler. You can type any PB code between two labels and see how the compiler creates the ASM code for it. Also, for a listing of the entire ASM code of a program, you can look at the commandline compiler option /COMMENTED. Have fun!
BERESHEIT
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Thanks, but all I see is that it makes a few push call and pop - meaning it's using an external function and not in-place code (I understand this is for size purposes, but what if I want speed?)
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Hi,

3 little examples.

Code: Select all

; Peek/Poke Examples
; Purebasic-Lounge.de
; By Hroudtwolf
Procedure MyPokeL (*Ref , Value.l)
  !PUSH   dword [esp+8]
  !MOV    ebp,dword [esp+8]
  !POP    dword [ebp]
EndProcedure

Procedure.l MyPeekL (*Ref)
  !PUSH   ebp
  !MOV    ebp,dword [esp + 8]
  !MOV    eax,dword [ebp]
  !POP    ebp
  !RET    4
EndProcedure

*Buffer = AllocateMemory (SizeOf(LONG))
MyPokeL (*Buffer , 1234567890)
Debug MyPeekL (*Buffer)
A little bit easier but not slower....

Code: Select all

; Poke-Macro Examples
; Purebasic-Lounge.de
; By Hroudtwolf

Macro MyPokeL (_ADDRESS_ , _VALUE_)
   *Dummy_LONG.LONG = _ADDRESS_   
   *Dummy_LONG\l    = _VALUE_
EndMacro

num.l = 100
MyPokeL (@num , 200)
Debug num
Best regards

Wolf
Post Reply