Page 1 of 1

_ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 1:15 am
by CodeCave
How can i do that in Pure Basic ?
-> The _ReturnAddress intrinsic provides the address of the instruction in the calling function that will be executed after control returns to the caller.

C++:

Code: Select all

_ReturnAddress()
msdn:
http://msdn.microsoft.com/en-us/library ... s.80).aspx

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 9:42 am
by Fred
There is no such command in PB, but if you mess a bit with ASM, you should be able to get it.

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 9:47 am
by Thorium
Here is an example:

Code: Select all

Procedure Test(Param1.i, Param2.i)
  
  Protected ReturnAddr.i
  
  !mov eax, [esp+4]
  !mov [p.v_ReturnAddr], eax
  
  MessageRequester("return addr", Hex(ReturnAddr))

EndProcedure

Test(15, 17)
For your procedure you need to change the value thats added to ESP. +4 per localy declared variable.

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 9:53 am
by buddymatkona
Instead of an Inline Function, how about a Macro?

Code: Select all

Macro MyTest
  Debug MacroExpandedCount
  x = ?MyLabel#MacroExpandedCount
  MyLabel#MacroExpandedCount:  
EndMacro
  
 MyTest 
  Debug x ; address beyond last  macro instruction for 1st use
 MyTest 
  Debug x ; address beyond last  macro instruction for 2nd use
 MyTest 
  Debug x ; address beyond last  macro instruction for 3rd use

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 9:56 am
by Thorium
buddymatkona wrote:Instead of an Inline Function, how about a Macro?
He wants the return address, i guess without knowing the caller, so he cant place a macro in the caller.

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 12:03 pm
by buddymatkona
@Thorium
True, the macro only works if he is compiling all of the code. Then the unique labels can double as CallerID.

Re: _ReturnAddress() in Pure Basic

Posted: Sat Jun 29, 2013 4:07 pm
by CodeCave
Hi thx for the replies :)

After reading them i played around a bit and ended up with these 2 codes...
Both of them get an "address" (at least is seems so) and that confuses me :?:

I would be happy if someone could explain me what esp exactly holds after the function is called and what im doing wrong/right :D

Here are my 2 codes:

Code1:

Code: Select all

Procedure TEST0(xyz,a,b,c,d,e,f)
  x = 0
  MOV eax, [esp+36]
  MOV x, eax
  Debug (x)
EndProcedure

Procedure TEST1()
    TEST0(1,2,3,4,5,6,7)
EndProcedure

Procedure TEST2()
    V = 1000
    V = V - 200
    TEST0(1,2,3,4,5,6,7)
EndProcedure

Procedure TEST3()
    TEST1()
EndProcedure

TEST0(1,2,3,4,5,6,7)
TEST2()
TEST3()
TEST3()
TEST2()
TEST2()
TEST0(1,2,3,4,5,6,7)
TEST1()
TEST1()
Code2:

Code: Select all

Procedure TEST0(xyz,a,b,c,d,e,f)
  x = 0
  MOV eax, esp
  MOV x, eax
  Debug (x)
EndProcedure

Procedure TEST1()
    TEST0(1,2,3,4,5,6,7)
EndProcedure

Procedure TEST2()
    V = 1000
    V = V - 200
    TEST0(1,2,3,4,5,6,7)
EndProcedure

Procedure TEST3()
    TEST1()
EndProcedure

TEST0(1,2,3,4,5,6,7)
TEST2()
TEST3()
TEST3()
TEST2()
TEST2()
TEST0(1,2,3,4,5,6,7)
TEST1()
TEST1()
What exactly stored in esp :?:

So far i got this info by testing:

esp 0 -> return address ?
esp 4 -> ?
esp 8 -> xyz
esp 12 -> a
esp 16 -> b
esp 20 -> c
esp 24 -> d
esp 28 -> e
esp 32 -> f
esp 36 -> return address ?

When i use the second code the "calling" addresses are closer together which makes more sense to me...
What should i use/do ?

(sorry my english is not the best :S)

Re: _ReturnAddress() in Pure Basic

Posted: Sun Jun 30, 2013 1:18 am
by idle
not sure if this is right but if esp holds the return address before Ret is called
you could trap RET instruction and move esp into a global var

Code: Select all

Global ReturnAddress

!Macro RET val {
  !mov ecx ,[esp]
  !mov [v_ReturnAddress], ecx
  !ret val   
!} 


Procedure Foo(a.i,b.i) 
  Protected c ,d 
  ProcedureReturn a * b 
EndProcedure 


Debug foo(3,4) 
Debug @foo() 
Debug ReturnAddress 


Re: _ReturnAddress() in Pure Basic

Posted: Sun Jun 30, 2013 10:14 am
by buddymatkona
@ idle
Thanks. That looks like a good way to insert a bit of procedure exit code.

Re: _ReturnAddress() in Pure Basic

Posted: Sun Jun 30, 2013 10:19 am
by Thorium
ESP is the stack pointer, it holds the stack address.
If a call is invoked the return address is pushed on the stack, so after a call ESP points to the stack position the return address is stored.

[esp+36] in TEST0 is wrong. Only add localy declared variables, not parameter variables.

I dont get what you want to accomplish with TEST1, TEST2 and TEST3.

Re: _ReturnAddress() in Pure Basic

Posted: Sun Jun 30, 2013 10:32 am
by CodeCave
Thx idle ill test that :)
Thorium wrote:ESP is the stack pointer, it holds the stack address.
If a call is invoked the return address is pushed on the stack, so after a call ESP points to the stack position the return address is stored.
[esp+36] in TEST0 is wrong. Only add localy declared variables, not parameter variables.
I dont get what you want to accomplish with TEST1, TEST2 and TEST3.
Thx Thorium,
that would mean the 1st code i posted is correct :>
i used TEST1/2/3 to get different callers and check the return address :S