Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to get the I/O port addresses assignment for hardware?

I have been learning assembly language recently, and I already know how to use the in/out command. But I have some questions:

  • Where did the port addresses come from, like 0x70?
  • How do I know how many ports my computer has and the addresses of them?

I've checked the Intel developer's manual, but I can't find the answer.

like image 653
donald jiang Avatar asked Jun 05 '20 10:06

donald jiang


1 Answers

There's a maximum of 65536 IO ports, and most aren't used.

The IO ports that are used on a modern PC can be split into 2 categories: PCI devices and legacy devices.

For PCI devices; each device ("function" in the terminology used by PCI specs) has some configuration space which contains "BARs" (Base Address Register/s). Each BAR says if the area used by the device is in the physical address space or the IO port space; and if it's in the IO port space the BAR will tell you how many IO ports at which base IO port. These can be set to anything (and software can change the setting if it has a reason to - normally firmware sets them up before the OS starts).

Legacy devices (things that were built into the motherboard or connected via. ISA bus before PCI was adopted but may still exist now) use fixed IO ports. For example, the master PIC controller (if it exists) will always use IO ports 0x20 and 0x21. Ideally; you'd decide to support a legacy device and check its documentation, and its documentation will tell you which IO port/s to use and what they do. Note that (because of ISA bus restrictions) all of these IO ports will be less than or equal to 0x3FF. For modern computers, you should also check (using ACPI tables) that the device exists before attempting to use it. Of course if you're not writing a device driver for a legacy device you don't really need to care what its IO port/s might be.

Note 1: For a computer with ISA slots (e.g. older than mid 1990s) you could have any kind of ISA card plugged in; and the IO ports used by each card was often controlled by little "DIP swiches" on the card itself. There was no way for software (an OS) to auto-detect a device's IO ports (and no way to auto-detect that a device was present). The only way around this problem is to ask the user to tell you and hope they remember and get it right (and storing the details in a configuration file). This was horrible for everyone, and fortunately later buses (PCI) avoided the problem.

Note 2: There actually was a "Plug and Play ISA" specification (from Microsoft) to add a means of auto-detecting ISA devices and their resources (e.g. IO ports). For better or worse ISA cards that support this specification are extremely rare; mostly because PCI arrived before most manufacturers tried to support it. Now that ISA slots are obsolete (and we're left with a few legacy pieces built into the motherboard with fixed IO ports) there's no point supporting this (or ISA slots in general).

Note 3: I'm ignoring other bus types (EISA, MCA) that existed (briefly) on some PCs. These were uncommon when they were being used and extremely rare now (e.g. you might find one that works in a museum somewhere, but they won't let you take it home to test your software on it).

Note 4: Some CPUs (mostly from Cryix and IBM in the late 1980s and early 1990s) used a few IO ports for the CPU itself and used them as configuration registers for various things (CPU features and cache control). Again; it's all too old to matter now.

like image 78
Brendan Avatar answered Nov 15 '22 07:11

Brendan