Inline assembly compiles when an input is constant, but not when variable

Hello, community.
I want to write x64 inb instruction using Zig inline assembly.
My code is below:

pub fn main() !void {
    _ = inb(3);
}

pub fn inb(port: u8) u8 {
    return asm volatile (
        \\inb %[port], %[ret]
        : [ret] "={al}" (-> u8),
        : [port] "N{dx}" (port),
    );
}

The build fails saying that:

error: error: <inline asm>:1:6: invalid operand for instruction
        inb %dl, %al
            ^~~

It works when an input for port is a constant value such as 3:

    return asm volatile (
        \\inb %[port], %[ret]
        : [ret] "={al}" (-> u8),
        : [port] "N{dx}" (3),
    );

Zig version is 0.12.0. Can anybody explain why the first example cannot compile?

Welcome @smallkirby :slight_smile:

It is correct that there is no instruction encoding for inb %dl, %al, but there is for inb %dx, %al.
Perhaps changing (port: u8) to (port: u16) is going to help.

Oops! You are completely right! It works.
Thanks for your quick and kind help :slight_smile:

2 Likes