ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
My first contact with linux was inspired by it's popen source implementation. I was excited by reading the C programming language (the original book) and also the Operating Systems Design and Implementation, which included Minix. There are some source files in minix which contain pure assembly. I haven't studied yet the hole linux code, but I think there are similarities. Now, my question is can an os be written completely in C or C++?
No, this is absolutely impossible. C does not offer the direct hardware access provided in assembler.
Remember that when you compile the following happens: c code >>> assembler code >>> machine instructions
So anything done in c can be done in assembler, but the inverse is not true.
How could you ever do I/O in pure c??? c offers nothing in terms of I/O. Remember that those glibc functions you use in c to do I/O like printf were actually written in assembler or link to something written in assembler.
Linux kernel contains an estimated 15% assembler code witch is considered very low and very good since assembler code is not portable, but c is.
Actually, most of the I/O code in Linux is written in C. The only thing that needs assembler by in large is maybe a little in the raw disk access. In general, you will need assembler for the bootloader, and bits and pieces of the memory management and interrupt handling systems. For example, code that switches the CPU from ring 0 to rings 1 or 2 or that manipulates the MMU must be done in assembler. What you also need is a good understanding of protected mode x86 programming (assuming you're developing for x86).
Originally posted by btmiller Actually, most of the I/O code in Linux is written in C. The only thing that needs assembler by in large is maybe a little in the raw disk access.
Please, enlighten me on how anything involving raw disk access can be done in c. I've being told that even printf was written in assembler. I tryed to find printf code, but could not find it.
I never actually got involved in coding the linux kernel, but I'm a reasonably experienced Intel Assember coder.
Even the most simple c tutorials will say that c does not provide I/O access and you must use glibc for such things.
First off, understand that the kernel does not use glibc, all of the the internal library functions are provided in the kernel. Even the glibc printf is probably not written in assembler, although to be honest I don't know 100% for sure. In any case, I'll tell you where you can find a C version of printf -- download the GRUB source code (I used 0.96 but probably any recent version will do) and looks in the docs/ directory. There is a little example kernel that shows how to write a simple OS that uses GRUB and in the kernel.c source file is a simple printf implementation written in C. Basically, it just declares a pointer into video RAM and manipulates it.
Granted, this is not the slightest bit portable to non-x86 hardware. What the C tutorials say is that ANSI/ISO standard C provides no portable I/O routines beyond the stdio library, which is correct.
[edit to add]
Of course, the Linux glibc does not use raw access to video hardware as the example I gave does. It uses the open, read, write, etc. system calls in Linux, read the man pages for these calls for more info.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.