Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 memory and I/O map

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?

like image 299
Pyjong Avatar asked Dec 06 '25 00:12

Pyjong


1 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.

like image 72
I. J. Kennedy Avatar answered Dec 07 '25 16:12

I. J. Kennedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!