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开发者_JS百科 when being executed? or are the I/Os addressed completely diferent way from the RAM?
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.
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.
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.
精彩评论