C:INT命令和C变量中的内联程序集
我正在尝试使用C变量在C代码中使用汇编。 我的代码如下所示:C:INT命令和C变量中的内联程序集,c,assembly,inline-assembly,C,Assembly,Inline Assembly,我正在尝试使用C变量在C代码中使用汇编。 我的代码如下所示: __asm { INT interruptValue }; switch (interruptValue) { case 3: __asm { INT 3 }; break; case 4: __asm { INT 4 }; break; ... } 其中“interruptValue”是我从用户处获得的变量,例如15或15h。 当我尝试编译时,我得到: 汇编程序
__asm { INT interruptValue };
switch (interruptValue)
{
case 3:
__asm { INT 3 };
break;
case 4:
__asm { INT 4 };
break;
...
}
其中“interruptValue”是我从用户处获得的变量,例如15或15h。
当我尝试编译时,我得到:
汇编程序错误:“无效指令”
操作数
我不知道中断值的正确类型是什么。我尝试了long\int\short\char\char*但没有一个有效。int操作码不允许将变量寄存器或内存指定为参数。必须使用INT 13h之类的常量表达式 如果您真的想调用变量中断,而我无法想象这样做的任何情况,请使用switch语句之类的语句来决定使用哪个中断 大概是这样的:
__asm { INT interruptValue };
switch (interruptValue)
{
case 3:
__asm { INT 3 };
break;
case 4:
__asm { INT 4 };
break;
...
}
编辑:
这是一个简单的动态过程:
void call_interrupt_vector(unsigned char interruptValue)
{
//the dynamic code to call a specific interrupt vector
unsigned char* assembly = (unsigned char*)malloc(5 * sizeof(unsigned char));
assembly[0] = 0xCC; //INT 3
assembly[1] = 0x90; //NOP
assembly[2] = 0xC2; //RET
assembly[3] = 0x00;
assembly[4] = 0x00;
//if it is not the INT 3 (debug break)
//change the opcode accordingly
if (interruptValue != 3)
{
assembly[0] = 0xCD; //default INT opcode
assembly[1] = interruptValue; //second byte is actual interrupt vector
}
//call the "dynamic" code
__asm
{
call [assembly]
}
free(assembly);
}
INT操作码不允许将变量寄存器或内存指定为参数。必须使用INT 13h之类的常量表达式 如果您真的想调用变量中断,而我无法想象这样做的任何情况,请使用switch语句之类的语句来决定使用哪个中断 大概是这样的:
__asm { INT interruptValue };
switch (interruptValue)
{
case 3:
__asm { INT 3 };
break;
case 4:
__asm { INT 4 };
break;
...
}
编辑:
这是一个简单的动态过程:
void call_interrupt_vector(unsigned char interruptValue)
{
//the dynamic code to call a specific interrupt vector
unsigned char* assembly = (unsigned char*)malloc(5 * sizeof(unsigned char));
assembly[0] = 0xCC; //INT 3
assembly[1] = 0x90; //NOP
assembly[2] = 0xC2; //RET
assembly[3] = 0x00;
assembly[4] = 0x00;
//if it is not the INT 3 (debug break)
//change the opcode accordingly
if (interruptValue != 3)
{
assembly[0] = 0xCD; //default INT opcode
assembly[1] = interruptValue; //second byte is actual interrupt vector
}
//call the "dynamic" code
__asm
{
call [assembly]
}
free(assembly);
}
作为一个有趣的练习,您可能可以使用自修改代码对变量中断进行编码,您只需将INT指令的第2字节的值更改为您想要的任何中断。@Falaina:您需要注意INT 3,因为它有不同的操作码,但在其他方面听起来是个好主意。@Falina:我该如何修改代码呢?我所能想到的就是将指令放在不同的函数中,然后用函数地址的偏移量更改一些字节。作为一个有趣的练习,您可能可以使用自修改代码编写一个变量中断,您只需将INT指令的第二个字节的值更改为您想要的任何中断。@Falaina:您需要注意INT 3,因为它有不同的操作码,但听起来是个好主意。@Falina:我怎样才能修改代码?我所能想到的就是将指令放在不同的函数中,然后用函数地址的偏移量更改一些字节。