1

I am trying to understand the following code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/io.h>

#define baseport 0x378

int main()
{
    int b;
    if(ioperm(baseport,3,1))
    {
        perror("ioperm");
        exit(1);
    }
    outb(0,baseport);

    usleep(1000000);
    printf("\n the status: %x,\n",inb(baseport));

    if (ioperm(baseport,3,0)) {perror("ioperm"); exit(1);}

    exit(0);
}

The output is 0xff, 255 in decimal, whether I write on Port 1 or Port 0 (using outb()). I cannot understand why it is 255 when I am writing 0 to it.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
karan421
  • 863
  • 17
  • 43

2 Answers2

5

The result of doing inb(0x378) is hardware-dependent. Some chips return the value you have written previously with outb, and some other chips just return garbage. In any case, it is not the port to read bytes from a potentially connected device.

jørgensen
  • 10,149
  • 2
  • 20
  • 27
  • if inb does not read byte on the port then for what it is used....please explain as i am a newbie....... – karan421 Mar 17 '12 at 17:13
  • 4
    @karan - Some ports are just for output, like one connected to a printer. What would you be reading from the printer? – Bo Persson Mar 17 '12 at 18:14
  • @BoPersson: its status, but then again, that is at a different, read-only, port, IIRC. – ninjalj Mar 31 '12 at 11:14
-2

First you need to see the port's capabilities, input, output or both. If it can be configured as both, you have to set it to the respective mode and only then you can expect the right behavior.

trxgnyp1
  • 317
  • 1
  • 10
RKT
  • 187
  • 1
  • 9