Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, Anyone there

I recently wrote a trial program in C++ that is to get the value in the eip register to be assign back to one function pointer variable, that function pointer var upon receiving an address from the eip register is expected to be callable again using that address from the eip.

The following is the complete listing of that kind program I just been working on, it had compiled ok but not run so smoother as i expected before.
#include "<cstdlib>"
#include "<iostream>"
#include "<cstdio>"

using namespace std;

typedef int (*fmult)(int,int);

int cmult(int,int);

int main(int argc, char *argv[])
{
    
    fmult funcp = NULL;
    
    cmult(10,20);
    
     asm (
        "movl %%esp,%%eax;"
        "movl %%eax,%0;"
        :"=a"(funcp)
        :
    );
    
    cout << "Function cmult(10,20) has called." << endl;
    
    cout << "This would try calls the cmult again.." << endl;
    
    funcp(20,10);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int cmult(int x, int y)
{
    cout << "cmult() get called.." << endl;
    return x * y;
}


I compiled it Ok using DevCpp 4.9.9.2 but still had problems when run it.

Anybody there can help please? ..
Posted
Updated 10-Jun-16 9:43am
v3
Comments
Sergey Alexandrovich Kryukov 22-Aug-13 12:50pm    
Why?
—SA

EIP is the instruction pointer, and it cannot me a subject of MOV instructions, because its purpose is very special. For example, if your execution moves from one instruction to another, EIP is modified accordingly. By this reason, the whole problem of "reading" EIP is not really well-defined. If you write some code to determine EIP, which is the EIP values would you need to remember: before calling this code, after, before return, or what? Any code execution modifies this register. Usually, this register is stored on the stack and is retrieved from the stack by instructions like CALL, RET or IRET instructions. The fact that it's stored on stack can help you to find out the EIP at the point of the call of some method which could retrieve that value from stack. It would be the offset in the code segment representing return address from such function.

So, you can store this register in another register indirectly. For more detail, please see, for example, http://stackoverflow.com/questions/4062403/how-to-check-the-eip-value-with-assembly-language[^].

I would wonder why would you need it. I'm not sure you that mean some good programming technique. :-)

—SA
 
Share this answer
 
v2
Comments
pasztorpisti 22-Aug-13 19:24pm    
+5, in this case getting IP isn't really useful...
Sergey Alexandrovich Kryukov 22-Aug-13 19:41pm    
Thank you. I agree with you, but who knows? What if OP invented some great revolutionary technology based on x86 instruction-set architecture? :-)
—SA
In your case getting IP isn't useful. You should work with a function or interface pointer to achieve what you want.

Getting the IP in assembly is useful when you are writing offset independent code in assembly: This is a piece of code that can loaded to an arbitrary offset in memory and can be executed without relocation. In that case getting the IP is usually used at least once in the offset independent code to calculate the offset of a label inside the assembly and then addressing data is done by relative to the label. As others have already mentioned (in the stackoverflow link) you can use the call instruction for this and in some circumstances an interrupt instruction. The most useful is a call instruction, it does the following: pushes the offset of the instruction that follows the currently executing call instruction and then jumps to a specified label. (This label is often the next instruction...).

My offset independent assembly often looked like the following:
start:
        call after_data ; the call instruction pushes the offset of data label to stack and then jumps to the after_data label
data:
        ; Blah blah balah: declaring all my data here with various labels pointing to different parts of it

after_data:
        pop eax ; no eax contains the offset of the data label
        ; My code comes here
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Aug-13 19:42pm    
5ed. I agree with you, but who knows? What if OP invented some great revolutionary technology based on x86 instruction-set architecture? :-)
—SA
pasztorpisti 22-Aug-13 19:47pm    
:-) :-) :-)
I used to get the EIP like this in C compiler inline asm in Intel syntax, but I don't know enough about AT&T asm syntax to help there.

For Intel asm syntax,
C#
_asm    
{ 
 // whatever code here    

 mov eax, TEST1_EIP    // Grab eip from label 
 // whatever code

TEST1_EIP: // whatever code
 
 // whatever code
}   

If you can reproduce the above in AT&T it should work, besides I'd like to see it in AT&T myself. I used to use the above to capture the offended exception addr.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900