_ReturnAddress() in Pure Basic

Just starting out? Need help? Post your questions and find answers here.
CodeCave
User
User
Posts: 16
Joined: Wed Jun 19, 2013 10:50 pm

_ReturnAddress() in Pure Basic

Post 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
Fred
Administrator
Administrator
Posts: 18161
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: _ReturnAddress() in Pure Basic

Post by Fred »

There is no such command in PB, but if you mess a bit with ASM, you should be able to get it.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: _ReturnAddress() in Pure Basic

Post 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.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: _ReturnAddress() in Pure Basic

Post 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
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: _ReturnAddress() in Pure Basic

Post 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.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: _ReturnAddress() in Pure Basic

Post by buddymatkona »

@Thorium
True, the macro only works if he is compiling all of the code. Then the unique labels can double as CallerID.
CodeCave
User
User
Posts: 16
Joined: Wed Jun 19, 2013 10:50 pm

Re: _ReturnAddress() in Pure Basic

Post 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)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: _ReturnAddress() in Pure Basic

Post 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 

Windows 11, Manjaro, Raspberry Pi OS
Image
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: _ReturnAddress() in Pure Basic

Post by buddymatkona »

@ idle
Thanks. That looks like a good way to insert a bit of procedure exit code.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: _ReturnAddress() in Pure Basic

Post 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.
CodeCave
User
User
Posts: 16
Joined: Wed Jun 19, 2013 10:50 pm

Re: _ReturnAddress() in Pure Basic

Post 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
Post Reply