I'm developing a small toy kernel in C. I'm at the point where I need to get user input from the keyboard. So far, I have implemented inb
using the following code:
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
I know that the "=a"
constraint means that al/ax/eax
will be copied to ret
as output, but I'm still confused about the "Nd"
constraint. Can anyone provide some insight on why this constraint is necessary? Or why I can't just use a general purpose register constraint like "r"
or "b"
? Any help would be appreciated.
The
in
instruction (returning a byte) can either take an immediate 8 bit value as a port number, or a port specified in thedx
register. More on thein
instruction can be found in the instruction reference (Intel syntax) . The machine constraints being used can be found in the GCC docs . If you scroll down tox86 family
you'll see: