Question

for example, we know that printf() function displays text in the console screen. But how are functions like printf() defined. Is it possible to write code to display text without the use of any library files? is assembly code used in defining these functions?

Was it helpful?

Solution

We can talk about C more easily, because it's a very basic language and really a little more higher over asm.
The answer is: system calls.
You could wonder: why? There are things that a language cannot do. And I/O is one of those. I/O streams are "owned" by the Operating System. It handles them.
The OS allows you to use them, but you must always rely on it before.

System calls are very basic: there are no format strings or whatever, for example.
Also you need to consider that system calls are OS-dependent. Windows' ones are different from Linux's ones.

puts implementation in the glibc

OTHER TIPS

Since every library/code internally use the operating system calls provided by kernel. So, It is possible to write your own printf like function without using c library.

If you want to know how these functions internally works, you can go for assembly language programming.

Is it possible to write code to display text without the use of any library files?

Yes of course it is. You might directly drive your display device, without any use of the standard functions.

is assembly code used in defining these functions?

Not necessarily, it can be completely accomplished in c or c++, without a single line of assembler code.

In the end it depends on the actual toolchain you are using to compile your programs, and the standard libraries that come with it, how these functions are defined. There are certain low level functions, you can 'override' for your concrete environment.

A common binding is to map the standard output interface (as used by printf()) to one of the UART interfaces of a MCU.

E.g for the commonly used newlib(c) coming with GCC toolchains here's some reference what's necessarily has to, and optionally can be ported to any environment: 'What steps do I need to do to port newlib to a new platform?'

Is it possible to write code to display text without the use of any library files?

Absolutely, since they have also done it, haven't they?

You would need to interface your kernel for writing low-level libraries like that, albeit it is not strictly necessary for each single case.

is assembly code used in defining these functions?

Yes, partially, using SIMD alike and other clever tricks for performance critical parts, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top