GCC内联汇编器“内存"Clobber 不会阻止重新排列 ARM 中的代码 [英] GCC Inline Assembler "memory" Clobber don't prevent from re-arrange the code in ARM

查看:24
本文介绍了GCC内联汇编器“内存"Clobber 不会阻止重新排列 ARM 中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于 GCC 内联汇编器的文章(http://www.ethernut.de/en/documents/arm-inline-asm.html).

I read article about GCC Inline Assembler (http://www.ethernut.de/en/documents/arm-inline-asm.html).

在本文中,内存"Clobber 强制编译器存储所有在执行汇编程序之前缓存值并在执行汇编程序后重新加载它们指示.并且必须保留顺序.

In this article, "memory" Clobber forces the compiler to store all cached values before and reload them after executing the assembler instructions. And it must retain the sequence.

这是一个例子.下面的代码打算将 c 与 b 相乘,其中一个或两个可以被中断程序修改.之前禁用中断访问变量并在之后重新启用它们看起来像好主意.

this is the example. The following code intends to multiply c with b, of which one or both may be modified by an interrupt routine. Disabling interrupts before accessing the variables and re-enable them afterwards looks like a good idea.

这可能会失败.因为优化器可能会决定做先乘法,然后执行两个内联汇编器指令,反之亦然.:

This may fail. Because the optimizer may decide to do the multiplication first and then execute both inline assembler instructions or vice versa. :

asm volatile("mrs r12, cpsr\n\t"
    "orr r12, r12, #0xC0\n\t"
    "msr cpsr_c, r12\n\t" ::: "r12", "cc");
c *= b; /* This may fail. */
asm volatile("mrs r12, cpsr\n"
    "bic r12, r12, #0xC0\n"
    "msr cpsr_c, r12" ::: "r12", "cc");

添加内存"Clobber 是安全的.

This is safe by adding "memory" Clobber .

asm volatile("mrs r12, cpsr\n\t"
    "orr r12, r12, #0xC0\n\t"
    "msr cpsr_c, r12\n\t" :: : "r12", "cc", "memory");
c *= b; /* This is safe. */
asm volatile("mrs r12, cpsr\n"
    "bic r12, r12, #0xC0\n"
    "msr cpsr_c, r12" ::: "r12", "cc", "memory");

但我通过 objdump -d 反汇编代码.记忆"Clobber 不起作用,代码是执行两个内联汇编指令,然后做乘法.

But I disassemble code by objdump -d . "memory" Clobber don't works, the code is to do execute both inline assembler instructions, and then do the multiplication.

mrs     ip, CPSR
orr     ip, ip, #192    ; 0xc0
msr     CPSR_c, ip
mrs     ip, CPSR
bic     ip, ip, #192    ; 0xc0
msr     CPSR_c, ip
mul     r0, r1, r0
mov     pc, lr

谁能解释为什么记忆"Clobber 不起作用?

Can anyone explain why"memory" Clobber don't works?

注意:
源代码.它可能会失败.

note:
source code.it may fail.

 #include <stdio.h>
    int main()
    {
            int a = mul(20, 10);
            printf("%d a\n", a); 
            return 0;
    };

    int mul(int b, int c)  
    {
            asm volatile("mrs r12, cpsr\n\t"
                            "orr r12, r12, #0xC0\n\t"
                            "msr cpsr_c, r12\n\t" ::: "r12", "cc");

            c *= b; /* This may fail. */

            asm volatile("mrs r12, cpsr\n"
                            "bic r12, r12, #0xC0\n"
                            "msr cpsr_c, r12" : :: "r12", "cc");

            return c;
    }
    ~   

这是安全的.

#include <stdio.h>
int main()
{
        int a = mul(20, 10);
        printf("%d a\n", a); 
        return 0;
};

int mul(int b, int c)  
{
        asm volatile("mrs r12, cpsr\n\t"
                        "orr r12, r12, #0xC0\n\t"
                        "msr cpsr_c, r12\n\t" : "=X" (b) :: "r12", "cc");
        c *= b; /* This is safe. */
        asm volatile("mrs r12, cpsr\n"
                        "bic r12, r12, #0xC0\n"
                        "msr cpsr_c, r12" :: "X" (c) : "r12", "cc");

        return c;
}

编译和反汇编命令:

lumotuwe@ubuntu:~/test_nfc$ arm-linux-gcc -O2 inline_asm.c 
lumotuwe@ubuntu:~/test_nfc$ arm-linux-objdump -d a.out

推荐答案

与问题相反,变量 abc不能被中断修改,因为它们是局部变量,没有指向它们的指针.

Contrary to the question, the variables a, b and c can not be modified by an interrupt as they are local variables and there is no pointer to them.

如果指向变量的指针存储在中断处理程序可以使用访问该变量的全局变量中,内存" clobber 将确保对变量的访问不会移动到 asm 语句.

If a pointer to the variables is stored in a global variable which an interrupt handler could use access the variable, the "memory" clobber will ensure access to variables is not moved past the asm statement.

需要 volatilememory" ,不需要两者都做.

Either volatile or a "memory" is required, ther is no need to do both.

这篇关于GCC内联汇编器“内存"Clobber 不会阻止重新排列 ARM 中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆