the semester is over so I sank a bit into assembly again. I have read some articles and parts of x86 family user's manual concerning the memory map and I/Os and I still haven't figured out how does it work.. As I understand it now, I can access the I/Os with IN and OUT instructions, in that case is it like the port number I use as an argument is actually relative address to some predefined area? or what do these two instructions do when being executed? or are the I/Os addressed completely diferent way from the RAM?
-
Which operating system you intend to use? – GJ. Jul 07 '10 at 17:27
-
1I don't intend to use any, since this is not dependent from operating system, but microprocessor's architecture.. – Pyjong Jul 07 '10 at 17:54
-
You are wrong! Under IA32 protected mode the I/O access normaly isn't allowed and will couse I/O exception so you need to write I/O kernel driver. – GJ. Jul 07 '10 at 18:32
-
ahhhpfff... ok assume i'm working in real mode – Pyjong Jul 07 '10 at 18:42
-
1it is still true that the way the thing is done in hardware is not O.S. dependent. the fact that O.S. takes control does not mean that hardware changes. it can't – ShinTakezou Jul 07 '10 at 19:13
-
Voting to close as unclear / too broad. See also: http://stackoverflow.com/questions/3215878/what-are-in-out-instructions-in-x86-used-for – Ciro Santilli OurBigBook.com Oct 30 '15 at 20:19
3 Answers
I/O ports are sort of like memory addresses, but are accessed differently, using IN and OUT instructions. The full story on modern hardware is very complicated, but accessing legacy devices in real mode is straightforward. Here's an example of how to read a scan code from the keyboard (technically, the keyboard controller).
Wait:
IN AL, 64H ; read keyboard status port
AND AL, 1 ; a key is ready when bit 0 is set
JZ Wait
IN AL, 60H ; read scan code
The port numbers 60H and 64H were established by IBM sometime before you were born, but every PC since then has mimicked this behavior in the name of backwards compatibility. Other legacy devices have fixed port numbers as well. Here's a fun one, if you've got a floppy drive:
MOV DX, 3F2H ; 3F2 is the floppy controller's control port
MOV AL, 10H ; turn on bit 4
OUT DX, AL ; start the floppy motor!
For port numbers bigger than 8-bits (e.g. 3F2), you have to put the port number in DX first (just a quirk of the instruction set). Again, the 3F2 assignment was fixed a long time ago with the introduction of the IBM PC.
Accessing today's devices on a modern bus is much, much trickier.

- 24,725
- 16
- 62
- 87
-
hmm thank you.. so what's the deal with accessing I/O with other instructions than IN and OUT? – Pyjong Jul 08 '10 at 07:15
-
Memory space and I/O space are independent. Some devices use memory-mapped I/O, which means they are accessed using normal memory reads and writes. There are pros and cons to both schemes, which is why some hardware makers use I/O addresses, and some use memory-mapped I/O. – I. J. Kennedy Oct 06 '10 at 04:26
-
when accessing pci device, how do I know if I should access using port or i/o memory ? Does it depends on cpu or on pci device ? – ransh Jan 06 '19 at 08:50
It is not possible to address the I/O the same way as memory.
I/O and memory have a common address bus. But, whether you access memory or I/O depends on the instructions you use. There is M/IO pin that determines one or the other. Nevertheless, the whole mechanism is a lot more complicated and somewhat dependent on design of your system. For my favorite design - PC if you want to programm devices with mov instruction you need to programm that devices address decoder so device starts responding to those address ranges.

- 3,095
- 4
- 32
- 50
Hardwareports are interfaces to hardware. Each port number is mapped to a specific device. Under protected mode, you cannot access ports, this must be done in kernel mode.
In DOS mode you are allowed to do it.

- 28,510
- 21
- 92
- 151