Read/Write to ports, how many bytes?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Read/Write to ports, how many bytes?

Post 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?
Hery
Member
Member
Posts: 65
Joined: Sat Dec 04, 2004 12:00 am

Re: Read/Write to ports, how many bytes?

Post 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.
User avatar
xenos
Member
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?

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Read/Write to ports, how many bytes?

Post 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)
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Read/Write to ports, how many bytes?

Post 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?
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Read/Write to ports, how many bytes?

Post 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.
User avatar
Combuster
Member
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?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply