Page 1 of 1

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

Posted: Mon Dec 03, 2007 4:57 am
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!!

Posted: Mon Dec 03, 2007 5:10 am
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!

Posted: Mon Dec 03, 2007 5:19 am
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?)

Posted: Mon Dec 03, 2007 10:21 am
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