What is the correct Rust inline assembly format?

Viewed 434

How do you get the new inline assembly format to work with rust? I would like to have a function that calls outb.

unsafe fn outb(value: u8, port: u16) {
    asm!(
        "outb {0}, {1}",
        in(reg_byte) value,
        in(reg_abcd) port,
     );
 }

This was the only version that almost produced code. There error message was:

error: invalid operand for instruction

   |
29 |         "outb {0}, {1}",
   |          ^
   |
note: instantiated into assembly here
--> <inline asm>:2:2
   |
2  |     outb dil, rax
   |     ^
1 Answers

First of all, you are using AT&T syntax, but the new asm! defaults to Intel's. You can still use AT&T but you have to add options(att_syntax).

Then, take a look at how this instruction can be used. What you need is to resolve to OUT DX, AL (outb %al, %dx in AT&T), no other registers of these sizes are allowed.

So you cannot use register classes at all:

    asm!(
        "outb %al, %dx",
        in("dx") port,
        in("al") value,
        options(att_syntax)
     );

Or in Intel syntax:

    asm!(
        "out dx, al",
        in("dx") port,
        in("al") value,
     );
Related