I wrote a small program to test GCC’s options.
int main()
{
int a=0;
__asm__("movl %0,%%ecx\n"
"jmp jmpsection\n"
"inc %%ecx\n"
"jmpsection: movl $1,%%eax\n"
"movl $0,%%ebx\n"
"int $0x80\n"::"a"(a):"ecx","ebx");
}
In order to keep var a equal to 1, skip the inc instruction.
I want to force GCC to generate the jmp instruction using IP relative addressing method.
I have searched the GCC manual to find a solution, but I feiled.
Thanks for replay.
For the x86, IP relative addressing is only possible in long mode (64 bit) and you seem to be writing 32 bit code.
Edit: Actually, in 32 bit mode it is possible to make jumps relative to the current IP and I think the compiler actually generates the correct code in your case. Let’s take a look at the relevant part of the generated assembly:
So, although it looks like the instruction at address 13 makes a direct jump to address 16, it actually is an IP relative jump. Take a look at the machine code:
ebis the opcode for a shortjmpwith an 8-bit displacement.01is this displacement. The result of this instruction is a jump to%eip + 0x01and, as the IP points to the next instruction to be executed, this will jump to address 16.