Setting and getting the Data Pointer...

Share your advanced PureBasic knowledge/code with the community.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Setting and getting the Data Pointer...

Post by DoubleDutch »

This routine allows you to save/restore the data pointer. A useful function of it is that you can set the data pointer to an area of allocated memory and use the read commands rather than the peek commands (no need to increment a pointer). This is really useful if reading in a file that has a header with data in it. Another advantage when using this over the traditional "Restore" command is that you can use an expression to set the address.

If you use the commands to read from an area of allocated memory you need to disable and then enable the debugger as it has a slight conflict.

The syntax is:
OriginalPointer=RestorePointer([new address])

Code: Select all

Procedure RestorePointer(position=0)
	!MOV eax,dword [PB_DataPointer]
	!MOV edx,dword [p.v_position]
	!CMP edx,0
	!JE	@f
	!MOV dword [PB_DataPointer],edx
!@@:
	ProcedureReturn
EndProcedure

Debug("Uninitialised restore pointer: "+Hex(RestorePointer()))

Restore MyData

Debug("MyData address: "+Hex(?MyData))
Debug("Current restore pointer: "+Hex(RestorePointer()))

Read var
Debug("Restore pointer after read: "+Hex(RestorePointer(?MyData)))
Debug("Restore pointer after optional setting: "+Hex(RestorePointer()))

Read var2
Debug("Result from 2nd read: "+Str(var2))


memtest=AllocateMemory(100)
PokeL(memtest,456)
l1=PokeS(memtest+4,"blah blah blah")+1
l2=PokeS(memtest+4+l1,"second string")+1
PokeL(memtest+4+l1+l2,789)

RestorePointer(memtest)
DisableDebugger
Read var3
Read string$
Read string2$
Read var4
EnableDebugger
Debug("Result from read from memory: "+Str(var3)+"  "+string$+"   "+string2$+"   "+Str(var4))

DataSection
MyData:
	Data.l	123
EndDataSection
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Very good, and useful of course.
I hope it is implemented as native. I mean the posibility of put an expression and not a constant value :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Yep - This should keep us going until it's native, I did some tests and it seems to cope ok. :)

The best side effect was that you can now use the READ commands to do a psuedo auto-incrementing PEEK. It's great for reading preloaded bin files.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Thank You Double Dutch, I think I can put this to use in a project. :)
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Setting and getting the Data Pointer...

Post by skywalk »

Hi DoubleDutch.
Do you mind moving this to Feature Requests?
It has worked great but now we have the C backend.
Here is my post on RestoreX(?mylabel) workaround..
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Setting and getting the Data Pointer...

Post by DoubleDutch »

I would do, but i I don't know how to get it moved to feature requests? Maybe an admin can do this?

Shame it hasn't been made native yet, coded it almost 15 years ago...
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Setting and getting the Data Pointer...

Post by skywalk »

Statute of limitation has long expired :lol:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply