I want to call the bios inline my c code. I tried asm("int %%al"::"a" (interrupt));
but gcc write Error: operand size mismatch for 'int'
. I wonder that code work.

- 328,167
- 45
- 605
- 847

- 87
- 6
-
Please note that assembly language questions should always include a tag for the relevant architecture, here apparently [tag:x86]. – Nate Eldredge Feb 13 '21 at 16:30
1 Answers
The int
instruction must take its vector as an immediate; it has no form that takes the number from a register. See the instruction description; note that the second form is INT imm8
and there is nothing like INT r8
or INT r/m8
that would allow a register or memory operand.
If interrupt
can be evaluated as a compile-time constant then you may be able to do
asm volatile("int %0" : : "i" (interrupt));
Note that in order for the interrupt to do something useful, you probably have to load various values into registers beforehand, and retrieve the values returned. Those will need to be done as part of the same asm
block, requiring more operands and constraints. You cannot put something like asm("mov $0x1a, %%ah");
in a preceding block; the compiler need not preserve register contents between blocks.
If you truly don't know the interrupt number until runtime, your options are either to assemble all 256 possible int
instructions and jump to the right one, or else use self-modifying code.

- 48,811
- 6
- 54
- 82
-
I have a new error : `../project/kernel/bootloader/boot.c:20:6: error: ‘asm’ operand 0 probably does not match constraints [-Werror] 20 | asm volatile("int %0" : : "i" (interrupt)); | ^~~ ../project/kernel/bootloader/boot.c:20:6: error: impossible constraint in ‘asm’` – Konect Team Feb 13 '21 at 16:31
-
3@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. – Nate Eldredge Feb 13 '21 at 16:33
-
-
In the simplest case, just hardcode `asm("int $0x10");` or whatever. What I mean by "determine at compile time" is if you write something like `int interrupt = 0x10; asm("int %0" : : "i" (interrupt));` the compiler (if optimizing) may be able to tell that `interrupt` will always have the value `0x10` and can insert that value into the instruction. But if it's something like a global variable or a non-inlined function parameter then there is no hope of this. – Nate Eldredge Feb 13 '21 at 16:49
-
I have a hawkish version for a version that doesn't use self modifying code in attorney syntax here: https://forum.osdev.org/viewtopic.php?p=315435#p315435 and an Intel syntax version here on SO https://stackoverflow.com/a/48410286/3857942 both are very hawkish and such run time usage is uncommon – Michael Petch Feb 13 '21 at 17:21