views:

88

answers:

3

Hi,

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?

A: 

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.

codymanix
+1  A: 

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.

I. J. Kennedy
hmm thank you.. so what's the deal with accessing I/O with other instructions than IN and OUT?
stupid_idiot
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
A: 

hey hey hey!!! i finally know it!! it is not possible to address the I/O the same way as memory. the thing is I/O and memory have common address bus but dependent from instructions you are using, it uses memory or I/O.

stupid_idiot