views:

662

answers:

4

I am writing a Linux kernel module on Fedora core 6 and I am wondering if anyone could tell me how to add the assembly code shown below to my program. The assembly code was written for Windows and I have no idea how to convert to Linux kernel program.

#ifdef _MSC_VER

unsigned char lookKbits(char k)
{
    _asm {
        mov dl, k
        mov cl, 16
        sub cl, dl
        mov eax, [wordval]
        shr eax, cl
    }
}

unsigned char WORD_hi_lo(char byte_high, char byte_low)
{
    _asm {
        mov ah,byte_high
        mov al,byte_low
    }
}

#endif
+5  A: 

GCC Inline Assembly Howto

If you're just looking for syntax:

The format of basic inline assembly is very much straight forward. Its basic form is

asm("assembly code");

Example: asm("movl %ecx %eax"); /* moves the contents of ecx to eax */

Specifically, look at section 3, which compares Intel to AT&T syntax.

Stephen Pape
A: 

What you're asking is how to write inline assembly in gcc. Section 5.35 of the gcc manual has extensive information about that. However, for this particular example, you're almost certainly much better off rewriting these functions is C, as you'll probably get better code out of the compiler than what the asm statements produce...

Chris Dodd
+1  A: 

In the kernel, you can also use the __asm__ macro, for an example see include/asm/atomic.h. LXR is a very good site for browsing and searching through the Linux sourcecode, you will find many examples there.

VolkA
+4  A: 

Have you tried writing it in C? To my naive eye it doesn't look like it needs to be in assembler.

starblue
The main idea is that these functions will be called too many times in my program and the program should excute as fast as possible.
Writing it in C really is a good idea - then you can profile your code and determine whether an assembly version is REALLY required.
MarkR
No program needs execute "as fast as possible" - most only need to execute finitely fast - figure out how fast that is and optimise it until you reach it, then stop.
MarkR