Page 1 of 1

Gets the size function

Posted: Wed Feb 24, 2016 3:12 pm
by zgneng

Code: Select all

Procedure MY()
 
  i=1
  
  b=2
 
EndProcedure

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


Gets the size Proced re code

Re: Gets the size function

Posted: Thu Feb 25, 2016 1:58 am
by Olliv

Code: Select all

FnLambdaStart:
Procedure FnLambda()
;
EndProcedure
FnLambdaEnd:

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

Re: Gets the size function

Posted: Thu Feb 25, 2016 2:07 am
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 ?????

Re: Gets the size function

Posted: Thu Feb 25, 2016 2:48 am
by DontTalkToMe
come on...

end - end = 0

Just correct the typo. :wink:

Re: Gets the size function

Posted: Thu Feb 25, 2016 3:13 am
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.

Re: Gets the size function

Posted: Thu Feb 25, 2016 11:35 am
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

Re: Gets the size function

Posted: Thu Feb 25, 2016 11:52 am
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).

Re: Gets the size function

Posted: Thu Feb 25, 2016 7:58 pm
by sys64802
Nice :)

Re: Gets the size function

Posted: Sat Feb 27, 2016 6:51 am
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

Re: Gets the size function

Posted: Sat Feb 27, 2016 11:22 am
by srod
Funstart: and Funend: do not capture the entire functions' contents if you take a look at the generated ASM.

Re: Gets the size function

Posted: Sat Feb 27, 2016 3:55 pm
by Olliv
+1 (if no function argument only)
+3 (if one or any arguments are present)
+x (in the way I m wrong)