It is currently Sun Jan 24, 2021 2:40 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Gets the size function
PostPosted: Wed Feb 24, 2016 3:12 pm 
Offline
User
User

Joined: Sun Jun 28, 2015 9:04 am
Posts: 29
Code:
Procedure MY()
 
  i=1
 
  b=2
 
EndProcedure

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




Gets the size Proced re code


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 1:58 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 543
Code:
FnLambdaStart:
Procedure FnLambda()
;
EndProcedure
FnLambdaEnd:

FnLambdaSize = ?FnLambdaEnd - ?FnLambdaEnd


Fn = FuNction
"FnLambda" = Function name example
Function = Same as "procedure" (convention)


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 2:07 am 
Offline
User
User

Joined: Sun Jun 28, 2015 9:04 am
Posts: 29
Olliv wrote:
Code:
FnLambdaStart:
Procedure FnLambda()
;
EndProcedure
FnLambdaEnd:

FnLambdaSize = ?FnLambdaEnd - ?FnLambdaEnd


Fn = FuNction
"FnLambda" = Function name example
Function = Same as "procedure" (convention)



debug FnLambdaSize =0 ?????


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 2:48 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Feb 04, 2013 5:28 pm
Posts: 334
come on...

end - end = 0

Just correct the typo. :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 3:13 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8149
Location: Fort Nelson, BC, Canada
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.

_________________
BERISHEET


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 11:35 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10589
Location: Beyond the pale...
The following works on Winx86. Not sure I'd rely on it though.

Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 11:52 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Feb 04, 2013 5:28 pm
Posts: 334
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:
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).


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Thu Feb 25, 2016 7:58 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Sep 12, 2015 6:55 pm
Posts: 105
Nice :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Sat Feb 27, 2016 6:51 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 543
Please apologize my problem of parallelism between my both eyes.

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

Code:
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:
McStart:
Macro McTest()
;McCode
EndMacro
McEnd:

Here "?McEnd - ?McStart" = 0


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Sat Feb 27, 2016 11:22 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10589
Location: Beyond the pale...
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Gets the size function
PostPosted: Sat Feb 27, 2016 3:55 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 543
+1 (if no function argument only)
+3 (if one or any arguments are present)
+x (in the way I m wrong)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 52 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye