Page 1 of 1

C for the assembly developer?

Posted: Tue Sep 02, 2008 12:58 pm
by 512dev
Let me start by saying that most of my programming experience is with assembly language. I've written a few simple OS kernels with it in the past and a few apps and such, but I've been pretty much out of the whole OSDEV scene for a couple years. Anyway, I've taken an interest in trying my hand at it again, only this time I'd like to properly learn C and write a kernel in it, rather than the entire thing in ASM. There's a few reasons why I want to make the move on to C, mainly that I've learned that writing anything large in ASM is tedious at best and mind-numbing at worst, but that's not the point.

Most books/tutorials/ebooks that I've picked up on C focus on teaching the reader C for purposes of application development, usually under Windows and sometimes Linux. That's all well and good, but obviously not what I'm going for. I've learned how to write applications in C on Linux before but my roadblock seems to be that I'm a bit confused as to how to apply a knowledge of C to writing an OS. Can anyone recommend a book/tutorial on C that focuses on how to apply it to writing a kernel rather than an application?

Thanks,
512

Re: C for the assembly developer?

Posted: Tue Sep 02, 2008 1:43 pm
by bewing
Back in the early '80s, I learned C and ASM shorly afterward. I am currently hacking an OS in ASM.
So, to directly answer your question for my case -- I don't know any books.
However, I am surprised that you think you need one ... unless you are trying to write an object-oriented C++ style OS in C (which is even more mind-numbing and tedious than coding in pure ASM!). The only things you can't do in C are a few specific opcodes, and you can do those with inline ASM code -- which you wouldn't have any trouble with.
Otherwise, it's just a matter of declaring some variables, and writing some loops, and using pointers -- it's a perfectly direct mental translation of what you would write in asm -- you just eliminate the need to keep track of the values in all the (pitifully few) CPU registers, by writing in C.

And, you know, if you want to join someone else's OS coding project, you could get a good head start -- even on an ASM OS.

Re: C for the assembly developer?

Posted: Tue Sep 02, 2008 1:54 pm
by Combuster
OS development is just C programming with the additional challenge of there being no libraries to use, so you are stuck with the basics that assembly has: computations in the processor, and communicating the results between memory and IO.

In C you will use pointers for memory, so that's the key thing to understand. You will still need assembly for the IO part.

As for a book, any good book that teaches pointer math thorougly will do, after that the only construct you need is the casting of ints to pointers. (Something you rarely see in app development)

In essence, it boils down to knowing what assembly is going to come out of the back of GCC when you feed it some code. As an exercise, try constructing a piece of assembly in your head then write the C code to get that. Once you get a hang of that you'll see why C codes much faster :)