EIP register

This is a discussion on EIP register within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I have debugged into the assembly language for the following simple sample, but I never saw any instructions ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    EIP register

    Hello everyone,


    I have debugged into the assembly language for the following simple sample, but I never saw any instructions dealing with push eip/pop eip, which dealing with saving/restoring the return address when function foo completes. Any ideas?

    Code:
    int foo (int a, int b)
    {
    	return a+b;
    }
    
    int main()
    {
    	int a1 = 100;
    	int b1 = a1 + 100;
    
    	a1 = foo (a1, b1);
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    EIP is the program counter. It changes for every instruction, and it is managed by call and ret, jmp and other "flow control" instructions, so there's really little need to push or pop it explicitly - in fact, there's no instructions to do that. In fact, plain "ret" is the same as "pop eip", and in PDP-11, although there was a "ret" instrction, it actually had the same opcode as "mov (sp)+, pc".

    You can "pop" EIP into another register:
    Code:
    intptr_t getAddressOfThisFunction()
    {
       intptr_t x;
        __asm{
            call here
    here:
            pop eax
            mov dword ptr x, eax
       }
       return ret;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    But be advised that this potentially messes up the CPU's internal procedure jump stack, thus leading to stalled pipelines on function returns.

    Search the blog The Old New Thing, where Raymond Chen warned about this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    But be advised that this potentially messes up the CPU's internal procedure jump stack, thus leading to stalled pipelines on function returns.

    Search the blog The Old New Thing, where Raymond Chen warned about this.
    Correct - not pairing call with return will mess up the "return stack optimizer".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. register variables
    By BEN10 in forum C Programming
    Replies: 9
    Last Post: 04-17-2009, 07:20 AM
  2. Return value to Register
    By Lettin03 in forum C Programming
    Replies: 20
    Last Post: 11-29-2007, 02:31 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. register file
    By axon in forum Tech Board
    Replies: 0
    Last Post: 11-20-2003, 08:07 AM
  5. difference between register int and normal int
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-25-2003, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21