Lets say I want to write an inline assembly function in a c++ code that returns its return address.
So if I'm calling the function returnAddress() from some address and it needs to return to the address X after the function is done, I want returnAddress() to return the value X.
example of code for returnAddress():
void* getAddress()
{
__asm {
pop ebx; // moving return offset to ebx?
push ebx; // restoring stack state
xor eax, eax;
mov ax, cs; // ax <- code segment
mov ecx, 16;
mul ecx; // multiplying the code segment by 16
add eax, ebx;// adding offset
}
}
The previous code doesn't work correctly, since when I press alt+8 I can clearly see my code's address is completely different from the return value of this function.
The reason I want to find my code's address in the memory is because I want to try and change it while the code itself is running. If there is any other way to find the address of my code without using inline assembly (maybe using windows API?) let me know please.
Also I'm pretty sure I can't even use CS's (code segment) value using visual studio 2010, so maybe that's what causing me problems... CS always equals to 35. Does the assembly view (alt+8) show incorrect addresses because VS2010 runs a virtual machine?
This is my first post here so maybe I didn't make my point very clear. Please let me know if I can explain myself to make it any clearer.
_ReturnAddress
intrinsic. - Raymond Chen