开发者

Understanding the hardware of printf

开发者 https://www.devze.com 2022-12-22 22:49 出处:网络
I was wondering if there was any r开发者_StackOverflowesources available online that explains what happens with something, like printf of C, that explains what\'s going on in the very low level (BIOS/

I was wondering if there was any r开发者_StackOverflowesources available online that explains what happens with something, like printf of C, that explains what's going on in the very low level (BIOS/kernel calls)


Linux:

printf() ---> printf() in the C library ---> write() in C library ---> write() system call in kernel.

To understand the interface between user space and kernel space, you will need to have some knowledge of how system calls work.

To understand what is going on at the lowest levels, you will need to analyze the source code in the kernel.

The Linux system call quick reference (pdf link) may be useful as it identifies where in the kernel source you might begin looking.


Something like printf, or printf specifically? That is somewhat vague.

printf outputs to the stdout FILE* stream; what that is associated with is system dependent and can moreover be redirected to any other stream device for which the OS provides a suitable device driver. I work in embedded systems, and most often stdout is by default directed to a UART for serial I/O - often that is the only stream I/O device supported, and cannot be redirected. In a GUI OS for console mode applications, the output is 'drawn' graphically in the system defined terminal font to a window, in Windows for example this may involve GDI or DirectDraw calls, which in turn access the video hardware's device driver. On a modern desktop OS, console character output does not involve the BIOS at all other than perhaps initial bootstrapping.

So in short, there typically is a huge amount of software between a printf() call and the hardware upon which it is output.


This is very platform-specific. From a hardware perspective, the back-end implementation of printf() could be directed to a serial port, a non-serial LCD, etc. You're really asking two questions:

  1. How does printf() interpret arguments and format string to generate correct output?

  2. How does output get from printf() to your target device?

You must remember that an OS, kernel, and BIOS are not required for an application to function. Embedded apps typically have printf() and other IO routines write to a character ring buffer. An interrupt may then poll that buffer and manipulate output hardware (LCD, serial port, laser show, etc) to send the buffered output to the correct destination.


By definition, BIOS and kernel calls are platform-specific. What platform are you interested in? Several links to Linux-related information have already been posted.

Also note that printf may not even result in any BIOS or kernel calls, as your platform may not have a kernel or BIOS present (embedded systems are a good example of this).


The printf() takes multiple arguments (variable length arguments function). The user supplies a string and input arguments.

The printf() function creates an internal buffer for constructing output string. Now, printf() iterates through each character of user string and copies the character to the output string. Printf() only stops at "%".

"%" means there is an argument to convert(Arguments are in the form of char, int, long, float, double or string). It converts it to string and appends to the output buffer. If the argument is a string then it does a string copy.

Finally, printf() may reach at the end of user sting and it copies the entire buffer to the stdout file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号