简体   繁体   English

理解GCC内联汇编语法中的输入/输出操作数

[英]Understanding input/output operands in GCC inline assembly syntax

As part of writing my OS, I am implementing interrupt handling and the I/O functions inb and outb . 由于写我的操作系统的一部分,我在执行中断处理和I / O功能, inboutb

I had to learn writing inline assembly in GCC and read up a lot about it online. 我必须学习在GCC中编写内联汇编并在线阅读很多内容。 Based on my understanding, I wrote my own code. 根据我的理解,我编写了自己的代码。 Meanwhile, I looked up Linux's implementation of the functions from /usr/include/sys/io.h . 同时,我从/usr/include/sys/io.h查找了Linux的函数实现。 This is what it is like for outb : 这就是outb

static __inline void
outb (unsigned char __value, unsigned short int __port)
{
  __asm__ __volatile__ ("outb %b0,%w1": :"a" (__value), "Nd" (__port));
}

Here are my questions: 这是我的问题:

  • The GCC manual says "N" is GCC手册说“N”是

Unsigned 8-bit integer constant (for in and out instructions). 无符号8位整数常量(用于输入和输出指令)。

But here __port is unsigned short int which I believe would be 16 bits. 但是这里__portunsigned short int ,我认为它是16位。 So how is it decided which portion of the 16 bits is used in the inline assembly ? 那么如何确定16位的哪一部分用于内联汇编?

  • This is my understanding of how this works - value of __port will be used directly (because of the "N") as a constant in place of %w1. 这是我对其工作原理的理解 - __port值将直接用于(因为“N”)作为常量来代替%w1。 Value of __value is copied to eax . __value值被复制到eax %bo is replaced by %al. %bo由%al替换。 Then the instruction is executed. 然后执行指令。 Is this correct ? 它是否正确 ?

  • How is it decided which of "N" or "d" to use for the second operand ? 如何决定将哪个“N”或“d”用于第二个操作数? Is there some preference order ? 有一些偏好顺序吗?

  • What difference does it make if I don't use "N" ? 如果我不使用“N”,会有什么不同? Wouldn't simply using "d" be better, since that is 16 bits ? 不会简单地使用“d”更好,因为这是16位?

  • If I omitted the "N", then is it correct that value of __port is copied to edx and then %w1 is replaced by edx ? 如果我省略了“N”,那么将__port值复制到edx然后将%w1替换为edx是否正确?

I may be mistaken, but my understanding is that "Nd" means use either N or d , at the compiler's preference. 我可能错了,但我的理解是"Nd"意味着在编译器的偏好下使用Nd If the value is not known to be a constant that fits in 8 bits, then N is not satisfiable, so the compiler has no choice but to use d . 如果该值不知道是一个适合8位的常量,那么N是不可满足的,因此编译器别无选择,只能使用d But when the value is a compile-time constant and its value fits in 8 bits, using an 8-bit immediate is preferable to wasting a register. 但是当该值是编译时常量并且其值适合8位时,使用8位立即数比浪费寄存器更可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM