例如,C code,在拆装演示动荡? [英] Example C code that demonstrates volatile in disassembly?

查看:109
本文介绍了例如,C code,在拆装演示动荡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是演示在拆卸挥发性和非挥发性的区别很短的说明C程序?

What is a short illustrative C program that demonstrates the difference between volatile and non-volatile in the disassembly?

int main()
{
    volatile int x;

    ???
}

VS

int main()
{
    int x;

    ???
}

我们可以更换两个 ??? 这样所产生的code是不同的?

What can we replace both ??? with such that the generated code is different?

推荐答案

例如:

x = 0;

如果 X 不是挥发性,编译器会看到,它不使用,将可能消除(无论是 X = 0; 完全从产生code作为一个优化的语句,甚至变量本身)

If x is not volatile, the compiler will see that it's unused and will probably eliminate it (either the x = 0; statement or even the variable itself) completely from the generated code as an optimization.

但是,挥发性关键字正好为$ P $从这样pventing编译器。基本上,它告诉code发电机不管你认为这个变量是/不,不要再次猜测,我需要它。所以,那么编译器将把volatile变量的访问,它会发出相应的前pression实际code。

However, the volatile keyword is exactly for preventing the compiler from doing this. It basically tells the code generator "whatever you think this variable is/does, don't second guess me, I need it." So then the compiler will treat the volatile variable as accessed and it will emit the actual code corresponding to the expression.

这篇关于例如,C code,在拆装演示动荡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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