Gets the size function

Just starting out? Need help? Post your questions and find answers here.
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Gets the size function

Post by zgneng »

Code: Select all

Procedure MY()
 
  i=1
  
  b=2
 
EndProcedure

GetprocodeSIZE(@my());??vHow To write


Gets the size Proced re code
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Gets the size function

Post by Olliv »

Code: Select all

FnLambdaStart:
Procedure FnLambda()
;
EndProcedure
FnLambdaEnd:

FnLambdaSize = ?FnLambdaEnd - ?FnLambdaEnd
Fn = FuNction
"FnLambda" = Function name example
Function = Same as "procedure" (convention)
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

Re: Gets the size function

Post by zgneng »

Olliv wrote:

Code: Select all

FnLambdaStart:
Procedure FnLambda()
;
EndProcedure
FnLambdaEnd:

FnLambdaSize = ?FnLambdaEnd - ?FnLambdaEnd
Fn = FuNction
"FnLambda" = Function name example
Function = Same as "procedure" (convention)

debug FnLambdaSize =0 ?????
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Gets the size function

Post by DontTalkToMe »

come on...

end - end = 0

Just correct the typo. :wink:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Gets the size function

Post by netmaestro »

You can't do that anyway, the procedure's size isn't contained between the two labels. Correct the typo and it's still 0.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Gets the size function

Post by srod »

The following works on Winx86. Not sure I'd rely on it though.

Code: Select all

Procedure MY1()
  i=1
  b=2
EndProcedure

Procedure MY2()
EndProcedure

sizeMY1 = @MY2() - @MY1()

Debug sizeMY1
I may look like a mule, but I'm not a complete ass.
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Gets the size function

Post by DontTalkToMe »

netmaestro wrote:Correct the typo and it's still 0.
Unusual way to generate the code but true, but now it's at least the right 0 :mrgreen:

Maybe it's possible to get the address of the desired procedure and then move upwards along the code until you reach one of the possible opcodes for the RET instruction (and consider its optional parameter).

Then subtract the two addresses.

But since the same number can be found in other instructions or operands along the way, you may have to use a runtime disassembler, like the one coming with pb, or Bea Engine. Then you can get the "real" RET instruction.

The returned size would change between debug and release builds I imagine.


Just a quick try to show the idea, I've not verified if the results are right.

Code: Select all

DisableDebugger 

Procedure fun1()
a = a + 1
EndProcedure

Procedure.s fun2(a, b, c)
 a$ = "hello"
 For k = 1 To 10
    a$ + Str(k) 
 Next
 ProcedureReturn a$
EndProcedure

Procedure.i GetFunSize(*entry)
 If ExamineAssembly(*entry)
    While NextInstruction()      
        i$ = Trim(InstructionString())
        *addr = InstructionAddress()
                     
        If Left(i$,3) = "ret"
            NextInstruction() 
            *addr = InstructionAddress()            
            Break
        EndIf
    Wend
    ProcedureReturn *addr - *entry
 EndIf

 ProcedureReturn 0
EndProcedure

MessageRequester("fun1()", Str(GetFunSize(@fun1())))
MessageRequester("fun2()", Str(GetFunSize(@fun2())))
This supposing the bundled disassembler works (I don't think it does it reliably enough and I would use another one).
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: Gets the size function

Post by sys64802 »

Nice :)
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Gets the size function

Post by Olliv »

Please apologize my problem of parallelism between my both eyes.

Just swap the four lines, two as two, near like that:

Code: Select all

Procedure FunTest()
FunStart:
; Fun code
FunEnd:
EndProcedure
Normally, should be lightly better than first suggesting.

Also... A little bit late to wish you a good year?

Edit: A reason of null value when label names are outside the function could be the same effect as a macro.

Code: Select all

McStart:
Macro McTest()
;McCode
EndMacro
McEnd:
Here "?McEnd - ?McStart" = 0
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Gets the size function

Post by srod »

Funstart: and Funend: do not capture the entire functions' contents if you take a look at the generated ASM.
I may look like a mule, but I'm not a complete ass.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Gets the size function

Post by Olliv »

+1 (if no function argument only)
+3 (if one or any arguments are present)
+x (in the way I m wrong)
Post Reply