Results 1 to 7 of 7

Thread: outp() and inp() in C

  1. #1
    Junior Member
    Join Date
    Jul 2002
    Posts
    8

    outp() and inp() in C

    Hi,

    Do any of you know what outp() and inp() are for in C? The piece of code that I am reading contained these library funtions from either dos.h or conio.h. This is the first time that I have bumped into these libraries. So I did a search on dos.h and I found this library discription: "These are interfaces to the DOS operating system. They generally correspond directly to DOS API calls. They are not portable to operating systems other than DOS." As for conio.h, I found: "Console versions of common I/O functions. They bypass the stdin and stdout buffers and access the console directly." Anyhow, I looked into both library and I still can't find where outp() and inp() are coming from. I have also looked into stdio.h; not there. BTW, I have thought about compiling the code. But since the code has to do with the BIOS, I am a little afriad inp() might be a writing function. It would be disastrous the next time I boot up the machine and it's not there anymore. I would appreciate any type feedback or help. Thanx.

  2. #2
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    Do any of you know what outp() and inp() are for in C?
    As far as I can remember they were used to read/write bytes to/from ports (not TCP/IP ports). I believe that they were removed from the implementation of the C library by Borland because they were not compatible with Windows NT. They have since been redefined and are no longer used much since the era of 32 bit operating systems. I could be wrong but that's what I remember.
    OpenBSD - The proactively secure operating system.

  3. #3
    Junior Member
    Join Date
    Jul 2002
    Posts
    8
    To be more precise, this is the piece of code I am looking at:

    for(i=0;i<128;i++)
    {
    outp(0x70,i);
    if(i%8==0)printf("\n");
    printf(" [%2x=%2x] ",i,inp(0x71));
    }

    It looks to me like it's dumping the content of certain memory addresses. It would be nice if someone can explain what is "0X##" in the code? That looks like a memory address to me; not sure. If it is an address, what's located in "0X70" and "0X71"? A more general question to that is, what does that area of memory represent? And on the top of that, why the loop from 0 -127? I hope the question are all related in someways. Also, if outp() is a library function that outputs bytes, then, why not use printf()? Sorry about the hairy questions. It's just that it's 4:11AM here, and I am having problem sleeping with the stuff unexplained. BTW, thanx for the valuable info. smirc. Appreciate it.

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    420
    The following code will enable the interrupts:

    outp(0x70, 0x0B);

    outp(0x71, inp(0x71

    The above is from http://www.geocities.com/SiliconValley/2151/cmos.html found using http://google.com searching on outp(0x70,i); Second link first was in German.

    Cheers,
    -D

    PS I did the search 'cause I was curious -I was think something hexideciamal
    If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
    -- former White House cybersecurity adviser Richard Clarke

  5. #5
    Junior Member
    Join Date
    Jul 2002
    Posts
    8
    ----------------------------------------------------------------------
    These are some of the useful information I found from dspeidel's link. Thanx.
    ----------------------------------------------------------------------
    utputpw
    % MSC: int outp( unsigned port, int datab )
    % MSC: unsigned outpw(unsigned port, unsigned dataw )


    - prototype in conio.h

    - write data value to port specified in port
    - port = hardware I/O port
    - datab = byte value to write
    - dataw = word value to write

    - Turbo C uses ~outportb~() and ~outport~()

    ----------------------------------------------------------------------

    :inp:inpw
    % MSC: int inp( unsigned port )
    % MSC: unsigned inpw( unsigned port )


    - prototype in conio.h

    - port = hardware I/O port 0-3FFh
    - returns byte or word data read from port
    ----------------------------------------------------------------------

    ^Interrupt Table as Implemented by System BIOS/DOS
    ...

    70 IRQ8 real time clock (AT,XT286,PS50+, see ~INT 15~)
    71 IRQ9 software redirected to IRQ2 (AT,XT286,PS50+)
    ...

    ----------------------------------------------------------------------

    :int 70
    ^INT 70 - Real Time Clock Interrupt (XT 286,AT,PS/2)


    - called 1024 times per second for periodic and alarm functions
    - decrements a DWORD counter by 976æ sec (1/1024)
    - when DWORD reaches zero, bit 7 of designated wait flag is set
    - if alarm was enabled by ~INT 1A,6~, ~INT 4A~ is called when
    counter reaches zero to activate alarm handler
    - not available in model 30 PS/2

    ----------------------------------------------------------------------
    ----------------------------------------------------------------------
    Looks like I have made an error when I wrote outp() and inp() are not found in conio.h, because that's where they can be found. Now, can someone help me explain what happened when port 0x70 is written and rewritten with numbers from 0 to 127? (please see the loop in my second post for this discussion)(I think that's what outp() is doing; not sure) What are the numbers (0 - 127) overriding? Are they useful or garbage? And would I damage my computer if I compile the codes and run it? I am currently reading some of manuals that can found on the link provided by dspeidel. If I ever figure out what I'm looking at, I'll post them. Thanx for the feedbacks.

  6. #6
    Junior Member
    Join Date
    Jul 2002
    Posts
    1
    The code reads data from the HDD (primary interface, slave drive). 0x170 is the port address
    of this drive. "Data" reffers not to data stored on HDD, but info such as HDD manufacturer, revision
    and the like.
    crc

  7. #7
    Junior Member
    Join Date
    Jul 2002
    Posts
    8
    Thanx for the feedback, crobert. If we are looking at some type of manufacturer information that has been imprinted onto the HDD, then would it harm the HDD if that part of the HDD was overriden? I am not seeing the importance of the need to must-have manufacturer info., maybe someone can explain that to me. BTW, a friend of mine has suggested that I might be looking at the master boot record (MBR) if I am indeed looking at the first 128 bytes. This prompted me to break out my UNIX System Admin. handbook. I am still talking about PC and Windows, so bear with me for a minute. This is an excerpt from the text describing the PC boot process: "...once your machine has figured out what device to boot from, it will try to load the first 512 bytes of the disk. This 512-byte segment is known as the Master Boot Record or MBR. The MBR contains a program that tells the computer from which disk partition to load a secondary boot program (the "boot loader")." And so forth. This tells me a little bit about what I am looking at; assuming I am working with the first 512 bytes. But what is contained in the first 128 bytes of the MBR? I would appreciate if anybody can tell me that I am looking at the wrong area all together. And even if I am looking at the wrong place, I would still appreciate any type of documentation on MBR; preferably with regard to Windows 2000. What does the OS do to it, or put in it. thanx.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •