FindMemoryByte, FindMemoryWord, etc...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

FindMemoryByte, FindMemoryWord, etc...

Post by Olliv »

Here is a copy of this topic

Code: Select all

;******************************************************************************************************************************************
;{ FindMemory Lib }

Macro _FindUnit_(UnitFunction, UnitSymbol, UnitSize, UnitSize_, StopConditionSymbol, NonStopConditionSymbol, RegisterPrefix)
        Procedure FindMemory#UnitFunction(*ResearchStart, WantedValue, MaximumQuantityToBeScanned = -1)
                                
                ! mov                           RegisterPrefix#ax,      [p.v_WantedValue]
                ! mov                           RegisterPrefix#cx,      [p.v_MaximumQuantityToBeScanned]
                ! mov                           RegisterPrefix#di,      [p.p_ResearchStart]
                ! xor                           RegisterPrefix#bx,      RegisterPrefix#bx
                ! shr                           RegisterPrefix#cx,      UnitSize_
                
                ! rep#StopConditionSymbol                               scas#UnitSymbol
                ! lea                           RegisterPrefix#di,      [RegisterPrefix#di - UnitSize]
                ! lea                           RegisterPrefix#ax,      [RegisterPrefix#bx]
                ! cmov#NonStopConditionSymbol   RegisterPrefix#ax,      RegisterPrefix#di
                ProcedureReturn
                
        EndProcedure
EndMacro

Macro _FindMemory_(UnitName, UnitSymbol, UnitSize, UnitSize_)
        CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
                CompilerIf UnitSize < 8
                        _FindUnit_(UnitName, UnitSymbol, UnitSize, UnitSize_, ne, e, e)
                        _FindUnit_(Non#UnitName, UnitSymbol, UnitSize, UnitSize_, e, ne, e)
                CompilerEndIf
        CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
                _FindUnit_(UnitName, UnitSymbol, UnitSize, UnitSize_, ne, e, r)
                _FindUnit_(Non#UnitName, UnitSymbol, UnitSize, UnitSize_, e, ne, r)
        CompilerEndIf
EndMacro

_FindMemory_(Byte, b, 1, 0)
_FindMemory_(Unicode, w, 2, 1)
_FindMemory_(Long, d, 4, 2)
_FindMemory_(Quad, q, 8, 3)

;}
example :

Code: Select all

DataSection
Here:
Data.A 65,33,48,0
EndDataSection

Debug "Start address :"
Debug ?Here
Debug "Character address 0:"
Debug FindMemoryByte(?Here, 0)
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: FindMemoryByte, FindMemoryWord, etc...

Post by Olli »

Message to the PB team : do not hesitate to include this in the next version of PB (if it is not already done).
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: FindMemoryByte, FindMemoryWord, etc...

Post by Mijikai »

Nice, it would be even better if it would know where and what it scans
(sections, access rights & boundaries) so it wont crash or lockup.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: FindMemoryByte, FindMemoryWord, etc...

Post by Olli »

Mijikai wrote: Sat Dec 19, 2020 9:09 pm Nice, it would be even better if it would know where and what it scans
(sections, access rights & boundaries) so it wont crash or lockup.
It is already coded in the main source :

Code: Select all

Procedure FindMemory#UnitFunction(*ResearchStart, WantedValue, MaximumQuantityToBeScanned = -1)
You have a third facultative argument : MaximumQuantityToBeScanned.

But I agree I am not sure the translating of the expression is so easy if the spaces are removed.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: FindMemoryByte, FindMemoryWord, etc...

Post by Mijikai »

Olli wrote: Mon Oct 31, 2022 1:44 pm It is already coded in the main source...
It does not check if the section/memory page you try to scan is even readable nor what dimensions it has.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: FindMemoryByte, FindMemoryWord, etc...

Post by Olli »

Show me where is your memory protection here...

Code: Select all

For i = 0 to 500
 If peekA(*address + i) = 53
  found53 = 1
  break
 endIf
Next
... and I will easily tell where is the memory protection in the CPU statement on the head.

For the number of dimensions, I would say << yes >> and << no >>.

Quicker than an 1-dim array.
Quicker than a [2;x] 2-dims longs array.
Quicker than a [4;x] 2-dims words array.
Quicker than a [8;x] 2-dims bytes array.
Quicker than a [2;2;x] 3-dims words array.
Quicker than a [2;4;x] 3-dims bytes array.
Quicker than a [2;2;2;x] 4-dims bytes arrays.

Quicker than a map.

I think you should allocate a gigabyte memory area, loose a 13 coded quad i.e. on the end of the area, and test the speed of the research (and the CPU ressources allocated), and compare between ASM backend and C backend.

Recall of the several functions:
findMemoryByte(*Address, value, onHowmanyCells)
findMemoryNonByte(*Address, value, onHowmanyCells)
findMemoryUnicode(*Address, value, onHowmanyCells)
findMemoryNonUnicode(*Address, value, onHowmanyCells)
findMemoryLong(*Address, value, onHowmanyCells)
findMemoryNonLong(*Address, value, onHowmanyCells)
findMemoryQuad(*Address, value, onHowmanyCells)
findMemoryNonQuad(*Address, value, onHowmanyCells)
Post Reply