Writing to ports using C

Kevin Buettner plug-discuss@lists.plug.phoenix.az.us
Fri, 1 Mar 2002 21:38:53 -0700


On Mar 1,  4:53pm, Benjamin Bostow wrote:

> I am trying to write to a port using C. So far I can only send over one char
> at a time. The following test code is below.
> 
> #include <asm/io.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <string.h>
> #include <errno.h>
> 
> int main()
> {
>   ioperm(0x3f0, 15, 1);
>   outw(0x56, 0x3F8);
> }
> 
> 
> Is there a way to print a string to the port, or is the only way to write an
> entire string to convert every character to HEX and print that to the port?

I fail to see why you need to convert anything to hex.  (In fact, it'll
likely be wrong if you do so.)

It seems to me that you could simply do

    void
    my_outsb (unsigned short port, const char *s)
    {
      while (*s)
	{
	  outb (*s, port);	/* or maybe outw (*s, port) instead. */
	  s++;
	}
    }

    ...

    my_outsb (0x3f8, "Hello, world!");

But it's probably even simpler to do the following instead...

    char *hello = "Hello, world!";
    outsb (0x3f8, hello, strlen (hello));

(Hint: Look for the definition of outsb() in <asm/io.h>.)

Kevin