2

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?

4 Answers4

2

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

edmz
  • 8,220
  • 2
  • 26
  • 45
  • 1
    What are **system** calls? I have no _system_ on my target! All I have is a firmware and a `main()` program + some bound interrupt routines :-/ ... – πάντα ῥεῖ May 04 '14 at 19:49
  • It's too broad. You might have some routines for I/O or you might not. – edmz May 04 '14 at 19:52
  • @πάνταῥεῖ: obviously, then you cannot reimplement userspace functions if you do not have kernel and userspace separation. – László Papp May 04 '14 at 21:29
  • @LaszloPapp That's not really true, we're running bindings for e.g. FreeRTOS at work. And of course a bare metal system would be able to implement some basic I/O as well ... – πάντα ῥεῖ May 04 '14 at 21:32
  • See the question: `How are all the c++ functions finally defined?` Note the word _all_, not "some basic I/O". Moreover, these are userspace functions, if you only have kernel space, you are not a position where you can talk about userspace, so that is needed, and then you have syscalls. – László Papp May 04 '14 at 21:33
  • Was the title changed to C++? I'm pretty sure I saw C and therefore replied for it. However, C++ (itself and its stdlib) is much more complex than C. @LaszloPapp I considered the I/O because it's one of the things which relies on system calls. Of course you don't need those to get a string length. – edmz May 05 '14 at 12:36
  • @black: it has always been C++. – László Papp May 05 '14 at 12:38
  • @LaszloPapp My bad. Probably I got confused because of `printf`. Shall I edit my answer and cover C++ too? – edmz May 05 '14 at 12:42
  • 1
    I do not think so. The answer remains valid regardless it is C or C++ imho, but yes, printf was a bad example from the OP for C++. He should have mentioned C or cout. – László Papp May 05 '14 at 12:42
1

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.

rajenpandit
  • 1,265
  • 1
  • 15
  • 21
  • 3
    oh, it's not difficult to write e.g. `printf` for an ordinary desktop OS, withut any assembly language programming. – Cheers and hth. - Alf May 04 '14 at 19:33
  • I have target's without OS at all, how does your answer apply for such? – πάντα ῥεῖ May 04 '14 at 19:38
  • @πάνταῥεῖ: Direct access to the hardware resources. – rodrigo May 04 '14 at 19:40
  • @πάνταῥεῖ: That depends on your architecture. For example, on an old real-mode PC, you might have just set up a pointer to the video memory and write your output there with ordinary C code. – celtschk May 04 '14 at 19:40
  • @rodrigo & celtschk Thank's guys, you don't have to tell **me** really ;) ... And I'm using customized newlibc bindings for most of these cases BTW. – πάντα ῥεῖ May 04 '14 at 19:42
  • yes but but ... how about the **[Kim 1](http://en.wikipedia.org/wiki/KIM-1)**, how to write i/o in C++11 for the Kim 1? – Cheers and hth. - Alf May 04 '14 at 19:44
  • @Cheersandhth.-Alf: I'm not sure that has enough memory to hold a complete C++11 implementation, no matter whether you write it in C++ or assembly. – celtschk May 04 '14 at 19:54
  • @celtschk: with the 4K memory extension there should be more than enough memory, yes? remember, all we need to is implement a universal Turing machine and give it enough tape to use. – Cheers and hth. - Alf May 04 '14 at 19:55
  • I don't have an idea how you'd output on that platform even in assembly, but I guess it's either writing to memory and letting a ROM routine handle the actual output, or writing to an I/O port. The first one can certainly be handled with C++ code (you'd write the compiler, therefore you could define the calling conventions to be compatible to the ROM code), and the second one can be if you add special support to the compiler to access the I/O ports, or if you already have C++-callable assembly code for general I/O port access (which would not be part of the printf routine, only used). – celtschk May 04 '14 at 20:04
  • @celtschk It's usually boiling down to interact with MCU intrinsic or connected peripherals. Yes, this is to write command's to certain register addresses and provide e.g. data to exchange via DMA. But above this level, you can easyily bind to the functions, coming with the c-standard library used by the toolchain. There's not really need to _'write the compiler'_ to accomplish this ;) ... – πάντα ῥεῖ May 04 '14 at 20:31
  • @πάνταῥεῖ: I strongly doubt there's already a C++11 compiler for the Kim 1 (a board from 1976!). Also this question is about writing standard library functions, which of course cannot assume the presence of standard library functions except those you've already written yourself. – celtschk May 04 '14 at 20:37
  • @celtschk I wasn't aware you referred to Alf's comment ... – πάντα ῥεῖ May 04 '14 at 20:39
1

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?'

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • `Not necessarily, it can be completely accomplished in c or c++, without a single line of assembler code.` -> how? Some optimization tricks are either not exposed to the compilers or the compilers do bad job which can potentially render functions unacceptably useless. – László Papp May 04 '14 at 21:27
  • @LaszloPapp I did't said this would lead to a good implementation, but can be accomplished though ... – πάντα ῥεῖ May 04 '14 at 21:29
0

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135