Marcin K.
Marcin K.

Reputation: 843

How to issue 0x2D interrupt in C code on 64-bit Windows?

I'm looking for a way to issue interrupt 0x2D from C code just like you can issue int 3 using __debugbreak()

On x86 I can use inline assembly

__asm
{
    int 2dh
}

but I need a solution that will work for:

Upvotes: 0

Views: 958

Answers (1)

zwol
zwol

Reputation: 140796

Since x64 Visual Studio doesn't support inline assembly at all (last I heard, anyway), your best compiler-independent solution is to write an .asm file that defines a function, let's call it int_2d, that just issues this interrupt and then returns. You compile the .asm file to an object file, link that object file with the rest of your application, and you can then write extern void int_2d(void); in a header file and call it normally from C when you need to issue the interrupt.

For the assembler that ships with x64 Visual Studio, the .asm file should look something like this:

_TEXT segment 'CODE'

int_2d proc 
    xor eax,eax 
    int 2dh 
    nop
    ret 
int_2d endp 

_TEXT ENDS 
END

The nop after the int 2dh is necessary because, depending on the opcode used and whether or not the process has a debugger attached, the INT 2D handler may or may not cause one byte of code (not one instruction) immediately after the int instruction to be skipped. So you need to put a one-byte NOP there.

INT 2D also expects an opcode in the A register; known values of this opcode range from 0 to 5, and I'm not having any luck finding documentation on what they actually do. The example code arbitrarily chooses to set A to 0. (You don't need to save and restore it, because A is call-clobbered in all of the Windows calling conventions.) If you want to pass the opcode as an argument, you can do that like this:

_TEXT segment 'CODE'

int_2d proc 
    mov eax, ecx
    int 2dh 
    nop
    ret 
int_2d endp 

_TEXT ENDS 
END

but you must then declare the function on the C side as __fastcall void int_2d(uint32_t op); (The __fastcall is unnecessary, but harmless, when compiling in 64-bit mode, I think.)

Unfortunately I do not know how to write the "boilerplate" (all of the stuff other than the actual assembly instructions) for an .asm file that needs to work with as many different Windows toolchains as possible. The above probably won't be acceptable to NASM, for instance.

Upvotes: 3

Related Questions