Debugging not exactly an interesting thing for most programmer! And this is one of the reason for not many people are ready to debug kernel code written by others. But it is an excellent trade to have!
There a few different ways one can debug kernel code -
-- Passive debugging using message printing.
-- Active debugging under a debugger.
Examples of extended inline assembly -
void mycfunc ( )
int a =10, b = 0;
asm ("movl %1, %%eax; \
movl %%eax, %0;"
:"=r" (b) /*output constraint, first mention of a variable b so b is %0, = sign mean target var */
: "r" (a) /*Input constraint, a var is now %1, no = sign means it is not output or target */
: "%eax" /* we clobbering eax register, so gcc will preserve eax's previous content */
-- Watch that we need to refer to registers using %%, instead of %.
-- If we use registers, we should make GCC aware about them, so it preserves the values before using them.
Compiler toolchains are smart these days, so depending on the analysis, GCC could delete the whole code section or a part or it can move code around for optimization. There may be situation where we want the compiler to stop doing those optimization, and place the code as is ( i.e. in place ). For that in systems code we almost always uses __volatile__ keyword, like the following example.
From <asm-i386/atomic.h> of Linux source -
statoc __inline__ void atomic_inc(atomic_t *v)
__asm__ __volatile__ (
LOCK "incl %0"
: "m" (v->counter)
For the detail syntax, and semantics, please use the GNU compiler connection manual. It is available on line.
Remember the dialog of this excellent and funny movie !
When working with proprietory code, some time it is absolutely necessary to disassemble pieces of code to understand some funcky stuff. On example is working with Windows internals. One can't get far without disassembling some parts of kernel implementations. Windows debugger is good at that, and the micorsoft team are not afraid to encourage us to do that, well of course if we know what we are doing!
So for some one not too familiar about x86 family of code, it is a good starting point, just in case you might need it sometime. I heard way too many times that - "Hey we are engineers, we make things !". But hardly every you will hear that - "Hey we break things too !" Sometime breaking little stuff gives you the confidence, along with frustration and craving for wanting to know more.
Before we go ahead, it is good to mention that Windows also support other architectures. And in case you are completely new at delving deep into assembly language while you are quite familiar with higher level language like C/C++ then first thing to follow is to read - "Just enough assembly assembly language to get by ( http://www.microsoft.com/msj/0298/hood0298.aspx )". It is two part article, and enough to get one become curious. It did to me, though I was not new to assembly language.
On the other hand, you might learn directly using assembly language to build little applications. So my suggestion is to use both methods.
Here our discussion is bit different. It is about using gnu assembler (as), and it is bit more arcane than what normal assembly language programming. It is about embedded ( inline ) assembly feature of gnu assembler, named GAS.
First compatibility wise, some of the inline assembly does not quite work from one platform to another. Particularly be watchful, if you happen to test some of them in freebsd 7.2 or earlier. Remember that we are discussing inline assembly in C file. GCC, the complier does not have any notion of assemby syntax, so it does not try to parse anything inside such instruction, it passes it to assembler who knows what to do with them.
Next, we might ask why do we need it. There are times when you need to access the register transfer level instructions for various reasons like: Kernel programming; Interrupt handling; fast performance; etc.
is the general structure of a simple inline assembly statement.
asm("nop") ; asm("cli"); asm("sti"); are some of the simple statements.
In case you have any named variable asm in global scope, you may want to use __asm__ instead to avoid name conflict.
Extended inline assembly is where this feature really shine !. If you happen to have a look at Linux or XNU kernel, you will find quite a bit of example use and their power.
Form of an extended inline assembly is -
asm ("assembly statement" : "output constraint(s)" : "input constraint(s)" : "clobbered constraint(s)" );
In a simple inline assembly, none of the constraint(s) are needed. That tells that they are optional. So any of the above constraints could be absent in an extended inline assembly instruction. Also within any constraint clause we can have more than one constraints.
Most important constraint is the clobbered one. GCC does not want to know or care about what registers are being used as target of operations in your statement, so if we want to perserve consistent machine state we need to tell the compiler what register sets are being clobberred so gcc can produce code that would save the registers before being used in the inline statment.