【问题标题】:Inline assembly in C: INT command and C variablesC 中的内联汇编:INT 命令和 C 变量
【发布时间】:2009-10-01 14:38:55
【问题描述】:

我正在尝试使用 C 变量在 C 代码中使用汇编。 我的代码如下所示:

__asm { INT interruptValue };

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

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

我不知道 interruptValue 的正确类型是什么。我尝试了 long\int\short\char\char* 但没有一个起作用。

【问题讨论】:

    标签: c assembly inline-assembly


    【解决方案1】:

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

    如果您真的想调用可变中断(我无法想象这样做的任何情况),请使用类似 switch 语句来决定使用哪个中断。

    类似这样的:

    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:我该如何修改代码?我能想到的就是将指令放在不同的函数中,然后用函数地址的偏移量更改一些字节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2018-09-15
    • 2010-11-26
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多