Page 1 of 1
Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 8:59 am
by mangaluve
I have a question about ports (that you can write/read from). Do you always read 2 bytes from a port, and write 1 byte? Because now I have two functions, inb och outb, that seems to write/read one byte. But I've seen examples where people use inw to read 2 bytes. What are the rules?
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 10:39 am
by Hery
It depends on what you want to achieve. You can access a 8-bit, 16-bit or 32-bit port. However writing to or reading from 16-bit port is in fact accessing two 8-bit ports. There are no rules when use which size of port defined by x86 specification, although it is important for the devices you want to access.
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 10:42 am
by xenos
There are instructions for reading / writing 1 byte, 2 bytes or 4 bytes from / to a port. Refer to
the Intel docs, Volume 2A and 2B, for detailed information about the "in" and "out" instructions.
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 11:40 am
by JAAman
hery is correct, it depends on which device you are accessing
some devices use only 1 port, some use more than one port -- for a specific device however, you should only use the size accesses that particular device expects
there is a big difference between 2 (or 4) 8bit accessses to a port and a 16- (or 32-) bit access, since, just like memory, larger accesses address multiple ports (rather than more bits from a single port)
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 12:05 pm
by mangaluve
but if I look at a certain port, that port expects a certain number of bytes, or does it depends on the context? For instance, I can read from the hard drive on port 0x1F0. Does this allways give 2 bytes?
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 3:46 pm
by bewing
The hard drive data ports are special, and must always be accessed using 16bit accesses (for both read and write). Almost every other I/O port on the system should be accessed one byte at a time, only.
Re: Read/Write to ports, how many bytes?
Posted: Thu Feb 26, 2009 4:01 pm
by Combuster
For integrated peripherals, its indeed one byte a time. The IDE controller is 16 bits exclusively, the VGA can do either 8 or 16 bit writes (in the latter case, its decoded as two writes). The video card I'm working with ATM is almost exclusively using 32-bit ports
So its a matter of the device you're talking to and what it expects from you. If you do something it does not expect you can't be sure what the consequence is.