Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Inline Assembly 'Nd' constraint

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.

like image 657
Michael Morrow Avatar asked Sep 25 '15 23:09

Michael Morrow


1 Answers

The in instruction (returning a byte) can either take an immediate 8 bit value as a port number, or a port specified in the dx register. More on the in 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 to x86 family you'll see:

d

The d register

N

Unsigned 8-bit integer constant (for in and out instructions). 
like image 157
Michael Petch Avatar answered Nov 09 '22 11:11

Michael Petch