是编程语言和方法效率低下? (汇编和C所需要的知识) [英] Are programming languages and methods inefficient? (assembler and C knowledge needed)

查看:120
本文介绍了是编程语言和方法效率低下? (汇编和C所需要的知识)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间,我想和研究汇编形式的C语言编译器的输出,以及CPU架构。我知道这可能是愚蠢的给你,但在我看来,什么是非常无效。请鸵鸟政策生气,如果我错了,并有一些原因,我没有看到所有的这些原则。如果你告诉我,为什么是这样设计的,我会很高兴。其实,我真的相信我错了,我所知道的人当中去电脑共同认识的一个理由这样做天才的头脑。什么,你问这个干什么? I'll立刻告诉你,我用C作为一个例子:

1:堆栈局部范围内存分配:

因此​​,典型的本地内存分配使用堆栈。只是ESP复制到EBP和比通过EBP分配所有的内存。好吧,我能明白这一点,如果你明确需要通过默认堆栈值分配RAM,但如果我不理解是正确的,现代的操作系统使用分页应用和物理内存之间的转换层,当你渴望地址达到实际RAM之前被进一步翻译字节。那么,为什么别只说是00000000 int类型的,0x00000004为int b和这样的吗?而仅仅通过MOV 00000000,#10访问它们?因为你不会真正访问内存块00000000和0x00000004,但这些操作系统设置页面表。事实上,由于EBP内存的分配和使用ESP间接寻址,我的方式将更快。

2:可变分配口是心非:

当你运行应用程序,装载机加载其code到RAM中。当您创建变量,或字符串,编译器生成code,在主创建时的顶O堆栈上推动这些​​值。因此,有实际的指令这样做,并且在内存中实际数目。所以,有在RAM中的值相同的2项。之一在指令的形式中,第二在RAM实际字节的形式。但为什么?为什么不只是宣告变盘点在该内存块的还要好,使用时比当,只需插入该内存位置?


解决方案

  1. 由于C(和大多数其他语言)的支持递归,所以一个函数可以调用本身和函数的每次调用需要的任何局部变量的单独副本。此外,在最新的处理器,用自己的方式实际上是慢 - 间接寻址是如此普遍,处理器针对它进行了优化


  2. 您似乎想要C的行为(或至少是C允许)的字符串。有好的和坏的点,这一点,比如,即使你已经定义了一个变量,你不能真正修改其内容(而不会影响在同一位置指向其他变量)的​​事实。


for a long time, I am thinking and studying output of C language compiler in assembler form, as well as CPU architecture. I know this may be silly to you, but it seems to me that something is very ineffective. Please, don´t be angry if I am wrong, and there is some reason I do not see for all these principles. I will be very glad if you tell me why is it designed this way. I actually truly believe I am wrong, I know the genius minds of people which get PCs together knew a reason to do so. What exactly, do you ask? I´ll tell you right away, I use C as a example:

1: Stack local scope memory allocation:

So, typical local memory allocation uses stack. Just copy esp to ebp and than allocate all the memory via ebp. OK, I would understand this if you explicitly need allocate RAM by default stack values, but if I do understand it correctly, modern OS use paging as a translation layer between application and physical RAM, when address you desire is further translated before reaching actual RAM byte. So why don´t just say 0x00000000 is int a,0x00000004 is int b and so? And access them just by mov 0x00000000,#10? Because you wont actually access memory blocks 0x00000000 and 0x00000004 but those your OS set the paging tables to. Actually, since memory allocation by ebp and esp use indirect addressing, "my" way would be even faster.

2: Variable allocation duplicity:

When you run application, Loader load its code into RAM. When you create variable, or string, compiler generates code that pushes these values on the top o stack when created in main. So there is actual instruction for do so, and that actual number in memory. So, there are 2 entries of the same value in RAM. One in form of instruction, second in form of actual bytes in the RAM. But why? Why not to just when declaring variable count at which memory block it would be, than when used, just insert this memory location?

解决方案

  1. Because C (and most other languages) support recursion, so a function can call itself, and each call of the function needs separate copies of any local variables. Also, on most current processors, your way would actually be slower -- indirect addressing is so common that processors are optimized for it.

  2. You seem to want the behavior of C (or at least that C allows) for string literals. There are good and bad points to this, such as the fact that even though you've defined a "variable", you can't actually modify its contents (without affecting other variables that are pointing at the same location).

这篇关于是编程语言和方法效率低下? (汇编和C所需要的知识)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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