C:INT命令和C变量中的内联汇编

Eld*_*dad 1 c assembly inline-assembly

我正在尝试使用C变量在C代码中使用汇编.我的代码看起来像这样:

__asm { INT interruptValue };
Run Code Online (Sandbox Code Playgroud)

其中'interruptValue'是我从用户那里获得的变量(例如15或15h).当我尝试编译时,我得到:

汇编程序错误:'无效的指令操作数'

我不知道interruptValue的正确类型是什么.我尝试了长\ int\short\char\char*,但没有一个工作.

Fra*_*ack 8

INT操作码不允许将变量(寄存器或内存)指定为参数.你必须使用像这样的常量表达式INT 13h

如果你真的想调用变量中断(我无法想象这样做),可以使用switch语句来决定使用哪个中断.

像这样的东西:

switch (interruptValue)
{
   case 3:
       __asm { INT 3 };
       break;
   case 4:
       __asm { INT 4 };
       break;
...
}
Run Code Online (Sandbox Code Playgroud)

编辑:

这是一个简单的动态方法:

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); 
}
Run Code Online (Sandbox Code Playgroud)

  • 作为一个有趣的练习,您可以使用自修改代码编写变量中断,您只需将INT指令的第二个字节的值更改为您想要的任何中断. (2认同)