EIP

Programming, for all ages and all languages.
Post Reply
sweetgum
Member
Member
Posts: 37
Joined: Thu Sep 18, 2008 11:17 pm

EIP

Post by sweetgum »

Im reading the intel manuals and it talks about how the only way to access the EIP is by executing a CALL instruction then reading the value of the return instruction pointer from the procedure stack. Can someone explain to me how this is done with a small example? Thanks!
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: EIP

Post by Firestryke31 »

For mov EAX, EIP:

Code: Select all

codeWantingEIP:
 call movEAXEIP
 ; do stuff with EIP

movEAXEIP:
 mov eax, [esp]
 ret
It's movEAXEIP that I believe they're talking about. To do 'mov EIP, EAX' you'd just do 'jmp eax'.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: EIP

Post by CodeCat »

No need for a RET, this works too, and is simpler. ;)

Code: Select all

codeWantingEIP:
call next
next:
pop eax
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: EIP

Post by Firestryke31 »

I was just remembering a thread where there was all of this talk about some sort of return address stack cache and doing that messes it up and slows everything down for a while. I don't know if that's really the case, but better safe than sorry.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: EIP

Post by Love4Boobies »

Lol. No, that's not what he's asking. After doing the call, you get EIP from the stack. Check the Intel manuals to see exactly what CALL pushes on the stack (sizes and order) and then read memory relative to SP :wink:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
sweetgum
Member
Member
Posts: 37
Joined: Thu Sep 18, 2008 11:17 pm

Re: EIP

Post by sweetgum »

If I pop the value of EIP, does it affect the functionality if i use ret? ret is supposed to pop the top of the stack to eip
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: EIP

Post by JohnnyTheDon »

If you pop EIP, then ret won't work. Instead use someting like

Code: Select all

mov eax, dword [esp]
Which will keep ret working and give you the value of the pushed EIP.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: EIP

Post by thepowersgang »

Or just use

Code: Select all

getEip:
    pop eax
    jmp eax
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: EIP

Post by JAAman »

thepowersgang wrote:Or just use

Code: Select all

getEip:
    pop eax
    jmp eax
not really a good idea, in general, because it will trash the CPUs CALL stack...
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: EIP

Post by Firestryke31 »

Aha! I knew there was something like that!
That's why I used the code I did.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: EIP

Post by Combuster »

callstack-friendly:

Code: Select all

pop eax
push eax
ret
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: EIP

Post by Firestryke31 »

Isn't that almost what I had in the second post in the thread? Or is this some sort of "come up with as many ways to do the exact same thing" contest? Image
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply