GCC内联汇编“钕”约束 [英] GCC Inline Assembly 'Nd' constraint
问题描述
我开发一个小玩具内核C.我在哪里,我需要从键盘获取用户输入的地步。到目前为止,我已实施 INB
使用以下code:
静态内嵌uint8_t有INB(uint16_t端口){
uint8_t有RET;
ASM挥发性(INB%1,%0:=一个(保留):ND(端口));
返回RET;
}
我知道= A
约束意味着人/ AX / EAX
将被复制到 RET
作为输出,但我仍然困惑的ND
约束。任何人都可以提供关于为什么这个约束是必要的一些见解?或者为什么我不能只用一个通用寄存器的约束如R
或B
?任何帮助将是AP preciated。
在
指令(返回一个字节),可以采取直接的8位值作为一个端口号,或在指定端口DX
注册。更多关于在
指令可以在找到指令参考(Intel语法)。正在使用的机器约束可以在 GCC文档。如果您向下滚动到 x86系列
你会看到:
D
D寄存器
N
无符号的8位整数常数(IN和OUT指令)。
块引用>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 thatal/ax/eax
will be copied toret
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:d
The d register
N
Unsigned 8-bit integer constant (for in and out instructions).
这篇关于GCC内联汇编“钕”约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!