如何在内联汇编中使用INT%0和来自C变量的中断号?

我想调用 bios 内联我的 c 代码。我试过了,asm("int %%al"::"a" (interrupt));但 gcc 写Error: operand size mismatch for 'int'。我想知道代码是否有效。

回答

int指令必须采取其作为立即向量; 它没有从寄存器中获取数字的形式。见指令说明;请注意,第二种形式是INT imm8并且没有类似INT r8INT r/m8允许寄存器或内存操作数的东西。

如果interrupt可以评估为编译时常量,那么您可能能够做到

asm volatile("int %0" : : "i" (interrupt));

请注意,为了让中断做一些有用的事情,您可能必须事先将各种值加载到寄存器中,然后检索返回的值。这些将需要作为同asm一块的一部分来完成,需要更多的操作数和约束。你不能把类似的东西asm("mov $0x1a, %%ah");放在前面的块中;编译器不需要在块之间保留寄存器内容。

如果直到运行时您才真正知道中断号,您的选择是组合所有 256 条可能的int指令并跳转到正确的指令,或者使用自修改代码。

  • @KonectTeam: Which means that the "If` in my second sentence is not satisfied - the compiler cannot determine the value of `interrupt` at compile time. Enabling optimizations might help, if you are not doing that already. Otherwise, rewrite your code so that it is a constant, or else see my last sentence.

以上是如何在内联汇编中使用INT%0和来自C变量的中断号?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>