The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Citric on May 12, 2005, 05:19:00 AM

Title: The Address of the Next Instruction
Post by: Citric on May 12, 2005, 05:19:00 AM
Hi All

Is it possible to move the address of the next into a variable ie

    mov eax,"next instruction address"
    mov [spotToJumpBackToo],eax


Cheer Adam
Title: Re: The Address of the Next Instruction
Post by: Petroizki on May 12, 2005, 05:26:40 AM
Maybe you could do something like?
mov eax, OFFSET @F
@@:
Title: Re: The Address of the Next Instruction
Post by: roticv on May 12, 2005, 06:43:28 AM

call @F
@@:
pop eax ;eax = address of next instruction - 1
;next instruction
Title: Re: The Address of the Next Instruction
Post by: AeroASM on May 12, 2005, 07:24:14 AM

call @F
@@:
pop eax
add eax, offset NextInstruction - offset @B
NextInstruction:


Petroizki's way is simplest, quickest and smallest though.
Title: Re: The Address of the Next Instruction
Post by: Citric on May 12, 2005, 07:34:54 AM
isnt there a register with the current instruction pointer?

Adam.
Title: Re: The Address of the Next Instruction
Post by: Citric on May 12, 2005, 08:15:50 AM
Should "mov [istrPointer], eip + 4" work?

Adam
Title: Re: The Address of the Next Instruction
Post by: Petroizki on May 12, 2005, 08:39:20 AM
You can't use eip register directly. You have to do some tricks to get it.
Title: Re: The Address of the Next Instruction
Post by: AeroASM on May 12, 2005, 10:00:31 AM
JUst thought of another idea:


int 3
jmp @F
dd 0
@@:


Then, make another app which debugs the first one and when it gets the int3, it uses GetThreadContext to get the eip, then stores it in the spare dd.
Title: Re: The Address of the Next Instruction
Post by: MichaelW on May 12, 2005, 10:16:44 AM
Quote
Should "mov [istrPointer], eip + 4" work?

The programmer does not have direct access to the instruction pointer, so EIP is not a valid symbol.

These should work:

mov [istrPointer], $ + 10
mov eax, $ + 5

Where the number at the end is the length of the assembled instruction in bytes.
Title: Re: The Address of the Next Instruction
Post by: Mark Jones on May 12, 2005, 12:32:22 PM
This may be a dumb question... but there's no EIP macro?
Title: Re: The Address of the Next Instruction
Post by: Farabi on May 14, 2005, 03:47:50 AM
GetEIP proc
pop eax
push eax

ret

GetEip endp
Title: Re: The Address of the Next Instruction
Post by: BogdanOntanu on May 14, 2005, 04:02:58 AM
Take care because this code:

mov eax, OFFSET @F
@@:


Is calculated at compile time not at runtime.
Title: Re: The Address of the Next Instruction
Post by: MazeGen on May 17, 2005, 08:29:20 AM
Mark, what do you mean by "EPI macro"?

Bogdan, the offset is not relocated at run-time?
Title: Re: The Address of the Next Instruction
Post by: AeroASM on May 17, 2005, 09:51:50 AM
EXEs generally do not have a relocation table, so they have to be loaded at the correct base otherwise they are screwed.
Title: Re: The Address of the Next Instruction
Post by: Randall Hyde on May 17, 2005, 11:55:13 PM
Quote from: AeroASM on May 17, 2005, 09:51:50 AM
EXEs generally do not have a relocation table, so they have to be loaded at the correct base otherwise they are screwed.

Sure they do!  It's possible to strip the relocation entries (the .reloc section) from an EXE file, in which case what you claim would be true, but standard EXEs are certain relocatable.
Cheers,
Randy Hyde
Title: Re: The Address of the Next Instruction
Post by: AeroASM on May 18, 2005, 07:14:31 AM
I never found one when I was messing around with exes. When I changed the image base of my own exe to 500000 it screwed up because the data offsets were from 400000