简体   繁体   中英

How to add assembly code in Linux

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

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.

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

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM