AT&T汇编语法中inl指令不同写法差异及执行时序咨询
inl Questions Let's tackle your two questions one by one—AT&T syntax can feel counterintuitive at first, so it's great to dig into these details!
Question 1: What's the difference between inl (%dx),%eax and inl %dx,%eax in AT&T syntax?
First, let's ground this in what the inl instruction actually does: it reads a 32-bit value from an I/O port into the %eax register. Here's the breakdown of the two syntaxes:
inl %dx, %eaxis the valid, correct AT&T syntax for indirect port addressing. This tells the CPU to use the value stored in the%dxregister as the I/O port number, then read a 32-bit value from that port into%eax. This maps directly to the Intel-syntaxIN EAX, DXinstruction, which is a standard supported x86 operation.inl (%dx), %eaxis invalid or misleading. The(%dx)syntax in AT&T refers to memory addressing—it means "read the value from the memory location pointed to by%dx". But theininstruction doesn't support using a memory address as a port source: x86 hardware only allows immediate port numbers (e.g.,inl $0x3F8, %eax) or the%dxregister for indirect port access. This syntax will either throw an assembly error, or if an assembler tries to interpret it, it won't perform a valid port read.
Question 2: Is there a difference between inl %%dx and inl (%%dx)? Also, is ebx set to 0 before inl executes?
Let's unpack this into two parts:
Difference between inl %%dx and inl (%%dx)
The double %% is specific to GCC inline assembly or macros—single % characters are reserved for placeholder operands in those contexts, so we use %% to refer to actual registers.
inl %%dx, %%eax(you'll always need the destination register%%eaxhere) is the valid way to write the indirect port read in inline asm. It behaves exactly likeinl %dx, %eaxin regular AT&T syntax.inl (%%dx), %%eaxis again invalid, just like the(%dx)case above.(%%dx)still denotes memory addressing, not port addressing. x86 hardware doesn't support fetching a port number from memory for anininstruction, so this is not a valid operation.
Does ebx get set to 0 before inl executes?
Nope, never automatically. The inl instruction only modifies its destination register (%eax in your examples). It has no effect on any other general-purpose registers like %ebx, and there's no requirement for %ebx to be zero to run inl. The only way %ebx would be zero is if your code explicitly sets it to zero (with an instruction like xorl %%ebx, %%ebx) before calling inl.
内容的提问来源于stack exchange,提问作者tomer.z

