汇编语言是如何融入到程序中的?

发布于 2024-07-25 20:26:46 字数 307 浏览 8 评论 0原文

我在很多地方都读到过,汇编语言通常不用于创建完整的程序,而是被其他程序用来提高某些程序的效率,特别是那些每秒调用数千次的程序。 我想知道如何将一小段汇编代码合并到更大的程序中。

  • 我认为可以制作一个小型可执行文件,然后从另一个程序运行,但这似乎效率低下。

  • 然后我想到了 Visual Studio 的内联汇编,但那是 Microsoft 特有的,似乎会有更好的方法。

那么,如何在不创建单独的程序或使用 Visual Studio 内联汇编的情况下在较大的程序中使用少量汇编代码呢?

I have read in a lot of places that assembly language is not usually used to create complete programs, but is used by other programs to make certain procedures more efficient, particularly the ones that are called a couple thousand times a second. I am wondering how small bits of assembly code are incorporated into larger programs.

  • I thought that a small executable could be made and then run from another program, but that seems inefficient.

  • Then I thought about the inline assembly for Visual Studio, but that's specific to Microsoft, and it seems like there would be a better way.

So how can small bits of assembly code be used within a larger program without creating separate programs or using the Visual Studio inline assembly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮年 2024-08-01 20:26:47

下面是一个示例(GCC):

__asm__ ("movl %eax, %ebx\n\t"
"movl $56, %esi\n\t"
"movl %ecx, $label(%edx,%ebx,$4)\n\t"
"movb %ah, (%ebx)");

对于 Microsoft 内联汇编,关键字是 __asm,汇编代码用大括号括起来,它不是字符串,并且目标寄存器现在位于左侧。

Microsoft 示例:

int foo(void) {
  __asm{
   mov eax,100 ; Moves 100 into eax!
   leave
   ret
  };
}

更多信息链接

内联汇编指令和参考:http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

IBM 教程,特定于 x86:http://www.ibm.com/developerworks/linux/library/l-ia.html

Here's an example (GCC):

__asm__ ("movl %eax, %ebx\n\t"
"movl $56, %esi\n\t"
"movl %ecx, $label(%edx,%ebx,$4)\n\t"
"movb %ah, (%ebx)");

For Microsoft Inline Assembly, the keyword is __asm, the assembly code is wrapped in curly braces, it's not a string, and the destination register is now on the left.

Microsoft example:

int foo(void) {
  __asm{
   mov eax,100 ; Moves 100 into eax!
   leave
   ret
  };
}

Links for more information

Inline Assembly Instructions and Reference: http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

IBM tutorial, specific to x86: http://www.ibm.com/developerworks/linux/library/l-ia.html

似梦非梦 2024-08-01 20:26:47

然后我想到了 Visual Studio 的内联汇编,但那是 Microsoft 特有的,似乎会有更好的方法。

每个编译器都支持某种形式的内联汇编,尽管通常方式略有不同。

因此,如果您需要使用内联汇编,请将每个片段封装在一个小函数中。 然后,在移植到另一个编译器时,您可以毫不费力地重新实现这些函数。

Then I thought about the inline assembly for Visual Studio, but that's specific to Microsoft, and it seems like there would be a better way.

Every compiler supports some form of inline assembly, though usually in slightly different ways.

So if you need to use inline assembly, encapsulate each snippet in a small function. Then you can reimplement these functions without much effort when porting to another compiler.

迟到的我 2024-08-01 20:26:47

对于像内部循环这样的小代码,通常通过内联汇编来完成。 这绝对不是微软特有的! (GCC 和几乎所有现代编译器也支持它,尽管语法上有不同的变化)。

对于较大的代码块(例如,操作系统的引导加载程序),通常会将其放入单独的源文件中(就像每种语言使用多个源文件一样),然后与程序的其余部分链接使用链接器。

请记住,汇编现在主要仅用于操作系统,因为编译器现在已经足够好了,将其用于游戏等内部循环并没有多大帮助(而且它根本不能移植到不同的体系结构)。 它主要用于真正低级的东西,例如硬件中断和设置分页(虚拟内存)。

For small bits of code like inner loops, it would usually be done with inline assembly. And that is definitely not Microsoft specific! (GCC and pretty much every modern compiler supports it too, although with different variations in syntax).

For larger chunks of code (for example, a bootloader for an operating system), it would usually be put in a separate source file (just like you use multiple source files with every language), and then linked in with the rest of the program using the linker.

Keep in mind that assembly is mostly just used for OSs nowadays, because compilers are now good enough that it doesn't help much to use it for inner loops in things like games (and it is not at all portable to different architectures). It is mainly used for really low level stuff like hardware interrupts and setting up paging (virtual memory).

江南月 2024-08-01 20:26:46

内联汇编并不是 Visual Studio 特有的 - GCC 也支持它。 内联汇编通常是将汇编合并到程序中的最简单方法。 然而,有几个问题 - 它会极大地影响代码的可移植性,并且某些编译器不支持它 - 例如,Microsoft 的 x64 编译器不支持它。

对于不支持内联汇编的编译器或者当您想要包含所有机器特定代码时,通常将汇编语言特定部分分离到它们自己的文件中,将汇编代码公开为 C++ 代码可以调用的函数。 然后,您可以将它们作为构建的一部分进行组装和链接。 您的链接器并不关心使用什么语言来生成目标文件 - 它可以将多种语言的目标文件组装到单个程序中。

Inline assembly is not specific to Visual Studio - GCC supports it as well. Inline assembly is typically the easiest way to incorporate assembly into a program. There are a couple issues however - it can greatly affect the portability of your code, and some compilers do not support it - for instance, Microsoft's x64 compiler does not support it.

For compilers that do not support inline assembly or when you want to contain all machine specific code, you typically separate the assembly language specific portions into their own files, exposing the assembly code as functions that your C++ code can call. You then assemble and link them as part of the build. Your linker doesn't care what language was used to generate the object files - it can assemble object files from multiple languages into a single program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文