GCC内联汇编“钕”约束 [英] GCC Inline Assembly 'Nd' constraint

查看:253
本文介绍了GCC内联汇编“钕”约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个小玩具内核C.我在哪里,我需要从键盘获取用户输入的地步。到目前为止,我已实施 INB 使用以下code:

 静态内嵌uint8_t有INB(uint16_t端口){
     uint8_t有RET;
     ASM挥发性(INB%1,%0:=一个(保留):ND(端口));
     返回RET;
}

我知道= A约束意味着人/ AX / EAX 将被复制到 RET 作为输出,但我仍然困惑的ND约束。任何人都可以提供关于为什么这个约束是必要的一些见解?或者为什么我不能只用一个通用寄存器的约束如RB?任何帮助将是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 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 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). 

这篇关于GCC内联汇编“钕”约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆