简体   繁体   English

内联汇编程序:可以使用哪些临时寄存器?

[英]Inline Assembler: What scratch registers can be used?

When inserting inline assembler into a function in a C-like language, what is the convention about what registers you're allowed to use for scratch? 将内联汇编程序插入到类似C语言的函数中时,关于允许哪些寄存器用于临时的约定是什么? Is it the compiler's responsibility to save the values of all the registers it needs to save before entering the asm block? 在进入asm块之前,编译器是否有责任保存需要保存的所有寄存器的值? Is it the responsibility of the programmer to store the values in these registers somewhere and restore them before exiting the asm block? 程序员是否有责任将值存储在这些寄存器中并在退出asm块之前恢复它们? Is there a typical convention, or is this very implementation-specific? 是否有典型的约定,或者这是特定于实现的?

Inline assembly is, by definition, compiler-specific. 根据定义,内联汇编是特定于编译器的。

Most compilers that support inline assembly have a syntax that allows you to indicate which registers are modified by the assembly. 大多数支持内联汇编的编译器都有一个语法,允许您指示程序集修改哪些寄存器。 The compiler can then save and restore those registers as needed. 然后,编译器可以根据需要保存和恢复这些寄存器。

This is very compiler specific. 这是特定于编译器的。 However, for a realistic example let's take gcc on x86. 但是,对于一个现实的例子,让我们在x86上使用gcc。 The format is: 格式为:

asm ( assembler template
    : output operands               (optional)
    : input operands                (optional)
    : list of clobbered registers       (optional)
    );  

Where the "list of clobbered registers" is you telling the compiler which registers your code is using. 在“命令寄存器列表”中,您告诉编译器您的代码正在使用哪些寄存器。

Here's a simple memory copy code: 这是一个简单的内存复制代码:

asm ("movl $count, %%ecx;
      up: lodsl;    
      stosl;
      loop up;"
    :           /* no output */
    :"S"(src), "D"(dst) /* input */
    :"%ecx", "%eax" );  /* clobbered list */    

Given these directions, gcc won't be using eax and ecx for other things in the block. 鉴于这些方向,gcc将不会将eax和ecx用于块中的其他内容。

More info here . 更多信息在这里

您可以在此处阅读某些调用约定中的寄存器用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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