Jona
Jona

Reputation: 1825

C++ inline assembly error

I'm trying to insert a JMP instruction using inline assembly but i get an error saying :

"Expected expression"

// Allocate a place in memory for the bytes 
BYTE *jmp = (BYTE*)malloc(len + 5);

// Copy the bytes of original + length to the allocated memory place:
memcpy(jmp, orig, len);

// Next we want to insert a jump back to the original + length 
jmp += len; // increment to the end of the copied bytes
jmp[0] = _asm JMP   // this is where i get the error

*(DWORD*)(jmp + 1) = (DWORD)(orig + len - jmp) - 5;

I am new to assembly and would like to know a way of achieving this goal in another way.

Upvotes: 0

Views: 123

Answers (1)

user786653
user786653

Reputation: 30450

jmp[0] = _asm JMP

Can never work as the opcode (byte(s) representing the instruction) for JMP depends on the operand (argument to the instruction). See Vol. 2A 3-433 in Intel® 64 and IA-32 Architectures Software Developer’s Manual.

It looks like you're after JMP rel32 in which case you should substitute 0xE9 for _asm JMP.

For more information I recommend the linked Intel documents or one of the other many sources on x86 instruction encoding on the net. E.g. this one from osdev wiki.

Upvotes: 1

Related Questions