Read/Write to ports, how many bytes?
Read/Write to ports, how many bytes?
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?
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.
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: Read/Write to ports, how many bytes?
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?
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)
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?
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?
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.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Read/Write to ports, how many bytes?
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.
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.