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

查看:82
本文介绍了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,其中一个或两个都乘以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

推荐答案

与该问题相反,变量 a b c 不能通过中断进行修改,因为它们是局部变量,并且没有指向它们的指针.

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.

如果将变量的指针存储在中断处理程序可以用来访问该变量的全局变量中,则内存" 破坏符将确保对变量的访问不会移到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.

需要 volatile 内存" ,则不需要两者都做.

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

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

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