admin管理员组

文章数量:1356753

Does this:

mov al, 0ffh
in al, dx

always do the same as this:

in al, dx

So, is the mov al, 0ffh redundant, and does an in al, dx instruction always set the value of AL, never keeping the previous value?

If there are differences within the CPU product line (8086, 186, 286, 386, 486, Pentium and beyond), please highlight them.

Does this:

mov al, 0ffh
in al, dx

always do the same as this:

in al, dx

So, is the mov al, 0ffh redundant, and does an in al, dx instruction always set the value of AL, never keeping the previous value?

If there are differences within the CPU product line (8086, 186, 286, 386, 486, Pentium and beyond), please highlight them.

Share Improve this question edited Mar 29 at 17:31 Sep Roland 39.9k10 gold badges48 silver badges88 bronze badges asked Mar 28 at 1:18 ptspts 87.7k23 gold badges115 silver badges198 bronze badges 2
  • 3 Yes, but you might get an exception and the handler could decide to skip the instruction in which case al would be a default value. Far fetched, but possible. – Jester Commented Mar 28 at 1:38
  • @Jester: You may want to copy your comment to an answer, and I'll accept it. – pts Commented Mar 28 at 1:46
Add a comment  | 

1 Answer 1

Reset to default 4

in al, dx always over writes AL, same as mov al, src.

Unless it faults, in which case AL has its previous value when the CPU jumps to the fault handler.

In protected mode, user-space can't run I/O instructions like in unless the IO privilege level is elevated to 0, or if specific ports are allowed in an I/O bitmap. (Linux ioperm(2) to allow specific ports in a limited range, iopl(2) to change IO privilege level). IO privilege level is separate from the current privilege level in the low bits of the CS selector, so e.g. you can let user-space run cli on this core (potentially getting it stuck other than NMIs) without letting it run invd and break cache coherency system-wide.

So anyway, faulting with #GP(0) due to privilege level is one way for in to fault. Apparently (https://www.felixcloutier/x86/in#64-bit-mode-exceptions) it can also fault with #PF in protected or 64-bit mode. The description section doesn't say how; the port number is in I/O address-space so isn't translated. I wonder if maybe this entry used to also document (rep) ins which writes to ES:rDI and thus can #PF on the memory destination, and maybe they fot to take out #PF when splitting ins to its own entry.

Protected mode was new in 286, and 386 changed it a bit.
In real mode, it can't fault other than with #UD (if lock prefix is used).

本文标签: assemblyDoes reading from an IO port on x86 always set ALStack Overflow