是否有办法将汇编代码插入到C中?

时间:2021-12-04 12:43:32

I remember back in the day with the old borland DOS compiler you could do something like this:

我记得在那一天,有一个老的borland DOS编译器你可以做这样的事情:

asm {
 mov ax,ex
 etc etc...
}

Is there a semi-platform independent way to do this now? I have a need to make a BIOS call, so if there was a way to do this without asm code, that would be equally useful to me.

现在是否有一种半平*立的方式来做这件事?我需要进行一次BIOS调用,所以如果有一种方法可以在没有asm代码的情况下进行此操作,那么这对我也同样有用。

4 个解决方案

#1


55  

Using GCC

使用GCC

__asm__("movl %edx, %eax\n\t"
        "addl $2, %eax\n\t");

Using VC++

使用VC + +

__asm {
  mov eax, edx
  add eax, 2
}

#2


9  

In GCC, there's more to it than that. In the instruction, you have to tell the compiler what changed, so that its optimizer doesn't screw up. I'm no expert, but sometimes it looks something like this:

在GCC中,还有比这更多的东西。在指令中,您必须告诉编译器发生了什么更改,以便它的优化器不会出错。我不是专家,但有时看起来是这样的:

    asm ("lock; xaddl %0,%2" : "=r" (result) : "0" (1), "m" (*atom) : "memory");

It's a good idea to write some sample code in C, then ask GCC to produce an assembly listing, then modify that code.

用C编写一些示例代码是一个好主意,然后请GCC生成一个程序集清单,然后修改该代码。

#3


4  

A good start would be reading this article which talk about inline assembly in C/C++:

一个好的开始是阅读这篇文章,它讨论了C/ c++中的内联程序集:

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

Example from the article:

从本文的例子:

#include <stdio.h>


int main() {
    /* Add 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                "movl $20, %ebx;"
                "addl %ebx, %eax;"
    );

    /* Subtract 20 from 10 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "subl %ebx, %eax;"
    );

    /* Multiply 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "imull %ebx, %eax;"
    );

    return 0 ;
}

#4


2  

Non-x86 Microsoft compilers do not support in-line assembly. You have to define the whole function in a separate assembly source file and pass it to an assembler.

非x86的Microsoft编译器不支持联机汇编。您必须在单独的汇编源文件中定义整个函数,并将其传递给汇编程序。

You're highly unlikely to be able to call into the BIOS under a protected-mode operating system and should use whatever facilities are available on that system. Even if you're in kernel mode it's probably unsafe - the BIOS may not be correctly synchronized with respect to OS state if you do so.

在受保护模式的操作系统下,您几乎不可能调用BIOS,并且应该使用该系统上可用的任何工具。即使您处于内核模式,这也可能是不安全的——如果您这样做的话,BIOS可能不会正确地与OS状态同步。

#1


55  

Using GCC

使用GCC

__asm__("movl %edx, %eax\n\t"
        "addl $2, %eax\n\t");

Using VC++

使用VC + +

__asm {
  mov eax, edx
  add eax, 2
}

#2


9  

In GCC, there's more to it than that. In the instruction, you have to tell the compiler what changed, so that its optimizer doesn't screw up. I'm no expert, but sometimes it looks something like this:

在GCC中,还有比这更多的东西。在指令中,您必须告诉编译器发生了什么更改,以便它的优化器不会出错。我不是专家,但有时看起来是这样的:

    asm ("lock; xaddl %0,%2" : "=r" (result) : "0" (1), "m" (*atom) : "memory");

It's a good idea to write some sample code in C, then ask GCC to produce an assembly listing, then modify that code.

用C编写一些示例代码是一个好主意,然后请GCC生成一个程序集清单,然后修改该代码。

#3


4  

A good start would be reading this article which talk about inline assembly in C/C++:

一个好的开始是阅读这篇文章,它讨论了C/ c++中的内联程序集:

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

Example from the article:

从本文的例子:

#include <stdio.h>


int main() {
    /* Add 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                "movl $20, %ebx;"
                "addl %ebx, %eax;"
    );

    /* Subtract 20 from 10 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "subl %ebx, %eax;"
    );

    /* Multiply 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "imull %ebx, %eax;"
    );

    return 0 ;
}

#4


2  

Non-x86 Microsoft compilers do not support in-line assembly. You have to define the whole function in a separate assembly source file and pass it to an assembler.

非x86的Microsoft编译器不支持联机汇编。您必须在单独的汇编源文件中定义整个函数,并将其传递给汇编程序。

You're highly unlikely to be able to call into the BIOS under a protected-mode operating system and should use whatever facilities are available on that system. Even if you're in kernel mode it's probably unsafe - the BIOS may not be correctly synchronized with respect to OS state if you do so.

在受保护模式的操作系统下,您几乎不可能调用BIOS,并且应该使用该系统上可用的任何工具。即使您处于内核模式,这也可能是不安全的——如果您这样做的话,BIOS可能不会正确地与OS状态同步。