深入理解计算机系统:1.3 了解编译系统是如何工作是很有必要的(It Pays to Understand HowCompilation Systems Work)

1.3 了解编译系统是如何工作是很有必要的(It Pays to Understand HowCompilation Systems Work)

例如以上举例的 “Hello.c”简单C语言程序,我们需要依赖编译系统生成正确而且有效的机器码,所以这就是为什么程序员需要了解编译系统是如何工作的?

  • 优化程序性能。现代的编译器是复杂的工具而且会生成做正确的机器码。作为程序员,为了写出更有效率的代码我们不需要知道编译器具体的工作,但是需要基本清楚编译器是如何把C语言代码翻译成机器代码的。例如switch语句是否比if else语句更有效率?函数会产生多少开销? while循环是否比for循环更有效率?C指针是否比数组索引更有效率? 为什么在循环中我们使用引用参数调用的方式比直接在本地定义变量进行累加存储更慢呢?在一个函数中我们只是简单调整下算法表达式的括号就可以使其更有效率,这是为什么呢?
    在第三章,我们将会介绍x86(64位)的Linux,Mac和Windows系统的最新机器语言程序,我们会描述如何把C语言如何翻译成机器代码。在第五章,我们将会学习如何通过简单调整C语言代码就可以让编译器工作更快,从而提升程序性能。在第六章那你会学习内存的分层,C语言程序如何把数据存储到内存中,你可以利用这些知识使得你的C语言程序更有效率。

  • 读懂链接器的异常信息。在我们经验中,大部分程序错误都是跟链接器的操作有关,尤其是你在搭建大型软件系统的时候。链接器出现无法解决引用的异常的时候,这是什么情况?静态变量和全局变量的区别?当你定义了两个相同名称的全局变量将会发生什么?为什么我们列举引用库的顺序很重要?静态库和动态库的区别?为什么很多链接器关联的错误会出现在运行时?在第七章你会找到以上问题的答案。

  • 避免安全漏洞。这些多年来内存泄漏的问题是网络和互联网服务器中出现安全异常的主要原因。这些异常问题依然存在,是因为只有少数的程序员小心处理数量的限制,尤其是那些不受信任的数据来源。首先学习安全程序是知道任何一种数据来源的数据和控制那些在程序栈的信息。我们会在第三章通过汇编代码学习栈的规则和内存泄漏。我们也会学习通过编译器、操作系统来减少攻击威胁。

For simple programs such as hello.c , we can rely on the compilationsystem to produce correct and efficient machine code. However, there are some important reasons why programmers need to understandhow compilation systems work:

Optimizing program performance. Modern compilers are sophisticated tools that usually produce good code. As programmers, we do not need to know the inner workings of the compiler in order to write efficient code. However, in order to make good coding decisions in our C programs, we do need a basic understanding of machine-level code and how the compiler translates different C statements into machine code. For example,is a switch statement always more efficient than a sequence of if-else statements? How much overhead is incurred by a function call? Is a while loop more efficient than a for loop? Are pointer references more efficient than array indexes? Why does our loop run so much faster if we sum into a local variable instead of an argument that is passed by reference? How can a function run faster when we simply rearrange the parentheses in an arithmetic expression?

In Chapter 3 , we introduce x86-64, the machine language of recent generations of Linux, Macintosh, and Windows computers.We describe how compilers translate different C constructs into this language. In Chapter 5 , you will learn how to tune the performance of your C programs by making simple transformations to the C code that help the compiler do its job better. In Chapter6 , you will learn about the hierarchical nature of the memorysystem, how C compilers store data arrays in memory, and howyour C programs can exploit this knowledge to run more efficiently.

Understanding link-time errors. In our experience, some of the most perplexing programming errors are related to the operation of the linker, especially when you are trying to build large software systems. For example, what does it mean when the linker reports that it cannot resolve a reference? What is the difference between a static variable and a global variable? What happens if you define two global variables in different C files with the same name? What is the difference between a static library and a dynamic library? Why does it matter what order we list libraries on the command line? And scariest of all, why do some linker-related errors not appear until run time? You will learn the answers to these kinds of questions in Chapter 7 .

Avoiding security holes. For many years, buffer overflow vulnerabilities have accounted for many of the security holes in network and Internet servers. These vulnerabilities exist because too few programmers understand the need to carefully restrict the quantity and forms of data they accept from untrusted sources. A
first step in learning secure programming is to understand the consequences of the way data and control information are stored on the program stack. We cover the stack discipline and buffer overflow vulnerabilities in Chapter 3 as part of our study of assembly language. We will also learn about methods that can be
used by the programmer, compiler, and operating system to reduce the threat of attack.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值