C Programming

Outb 2 C Function

“The CPU has several connections to external hardware, including LCDs and hard drives. The CPUs are utilized to transfer data into and out of the peripherals. This total transaction data transfer rate could range from bits to terabytes. When data is sent between a CPU and a peripheral, the CPU provides numerous ports, often known as input and output, or IO. These ports are used by the CPU to manage various smart devices. While working in any programming language, you may have used many functions and variables utilizing some port numbers or information regarding ports. Just like that, the Linux POSIX C library came up with such functions as well. Some of the C functions function belong to the low-level input and output family of a POSIX to perform low-level input and output operations. The Outb() 2 C function of POSIX is one of them.

Outb seems to be a function encapsulation for just an assembly command of the very same designation being used to communicate with port-mapped peripherals, such as the typewriter on IBM PC compatible systems, which use the I/O address range 0x60-0x6F. Within this article, we will be discussing the use of the outb() C function while using the Ubuntu 20.04 Linux operating system. To understand the working of the outb() function, you need to have a look at the Post-mapped I/O first.”

What is Port-mapped I/O?

Memory-mapped and port-mapped I/O are the two common ways that computer chips link to external devices. Nevertheless, all approaches seem to be equivalent when it comes to the periphery. Port mapped I/O is accessible via a specific set of CPU commands and a distinct, specialized address space. The IN and OUT commands can be seen on Intel computer processors.

To give I/O devices a different address space from ordinary storage, either an additional “I/O” pin was added to the access point of the CPU, or a whole bus was set aside for I/O. It is a CPU-to-peripheral communication technique that is employed when unique commands are required because the processor must communicate with the ram and peripherals. This is frequently referred to as isolated I/O since the address for I/O is separate from that for primary storage. The alternative and most popular way presently are memory-mapped I/O, in which no extra commands are required since everything shares the very same address space. Mapped to memory I/O is accessible in almost the same manner as program memory and/or user memory since it is drawn into the very same address space.

Low-level port input and output operations are handled by this group of POSIX methods. The b-suffix methods are byte-width procedures, the w-suffix methods are word-width procedures, and the _p-suffix methods delay till the I/O is finished. The out* operations perform port outputting, while the in* processes do port inputting. Although they can still be used in user mode, they are generally designed for inner core use.

#include <sys/io.h>

void outb(unsigned char value, unsigned short port);

We need to use O, O2, or an equivalent compiler. Because the methods are specified as inline macros, connection time problematic references may result if optimization is not activated. To instruct the kernel to let the user-mode program use the concerned I/O ports, you can use the ioperm (2) or, optionally, the iopl (2) commands. If you don’t perform this, the program will get a segmentation fault. Hardware-specific functions like outb() and companions In contrast to how most DOS systems work, the value parameter is provided first, and the port parameter follows.


Let’s take a look at the usage of the outb() function in the C program. This program will be a little demonstration of how we can use it within any C program code without looking at its output, i.e., most of the time, it won’t output the way we want. So, we need to create a C file in our Linux operating system first. Open the shell terminal via the use of “Ctrl+Alt+T”.

After that, make use of the Linux’s touch instruction along with the name of a file “outb.c” to be created with the “.c” extension at its end. This file can be found in the current working directory of your Linux system, i.e., “home”. Open the file explorer and double tap on the file to open it.

The empty file would be opened on your screen. Write out the shown-below C code of the outb() function in it and press the Save button to save it. We have started this C code with the use of some main headers of C. Firstly, we have added the “config.h” header to configure the C code. The “sys/io.h” is the main header required for the use and execution of the outb() function in the C program. So, we have added it as well. After that, the sys/ioctl.h and Linux/parport.h header have been utilized for the use of port-related information. The standard stdio.h and stdlib.h headers are used to make use of standard input and output within the program.

The initialization of the addr variable has launched the main() function. The ioperm() function is used to set the port access permission bits and pass the result to the variable result. The outb() function is here to perform port outputting. You can run this program on your system with the GCC compiler.


As for the benefits and drawbacks, exchanging information and address buses might make memory access sluggish as peripheral devices operate more slowly than ram. The I/O flexibility offered by memory-mapped platforms, on the other hand, reduces the amount of internal logic needed by the processor, allowing for the implementation of quicker, less expensive, and power-efficient CPUs. In parallel to RISC systems, the rationale is to decrease intricacy to obtain a more focused and reliable system, which is useful for embedded devices, for instance. This guide has one of the basic examples to demonstrate the idea of outb function in C language.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.