Page 1 of 1
How to implement LibC
Posted: Wed Mar 19, 2008 6:19 am
by AlfaOmega08
How does libc (for example) print a string?
It cannot access directly to 0xB8000. Because it's executed in user mode, and it would cause a page fault. Does libc use system calls?
Posted: Wed Mar 19, 2008 6:27 am
by JamesM
Yes, libc normally uses the read() and write() system calls.
On a *NIX system, it uses write() on the file descriptor "stdout" (normally FD 1, iirc).
Posted: Wed Mar 19, 2008 6:35 am
by jgraef
Hi,
The printf function writes to the stdout, what normally is redirected in the parent process' stdin. When the string you want to print reaches your init process you can pass it to a screen driver. I do this over a device file. The screen driver uses a special flag for malloc() to allocate 0xB8000. So the screen driver can put your string on screen.
Posted: Wed Mar 19, 2008 4:20 pm
by lukem95
It depends if you want your OS to conform to a standard (e.g. POSIX) or not.
i have a putch() system call, to make my life easier

This detects what graphical mode your in (through a external volatile int) and then calls the required putch (VGA, console etc)
Posted: Wed Mar 19, 2008 4:48 pm
by piranha
Well, I implemented IO channels, you can call read, write, open, close, or custom through syscalls. It then calls a handler function (in kernel mode) which does what is requested.
Channel 0 is kernel (reset, asm("hlt")), Channel 1 is Terminal (getchar, puts, clear, setXY).
-JL