IS"对于(;;)QUOT;成语无限循环正确地归因于PDP-11 C编译器? [英] Is "for(;;)" idiom for infinite loop correctly attributed to PDP-11 C compiler?

查看:166
本文介绍了IS"对于(;;)QUOT;成语无限循环正确地归因于PDP-11 C编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现,声称这篇文章这个想法到preFER 为(;;)而(1)来是因为C编译器最初可在PDP-11产生的额外机器指令而(1)

Recently I found this article that claims that the idea to prefer for(;;) over while(1) for infinite loop came because the C compiler originally available on PDP-11 generated an extra machine instruction for while(1).

顺便说一句,现在连的Visual C ++警告倾向于前者

Btw now even Visual C++ warnings tend to favor the former.

如何现实是这样的归属(;;)成语?

推荐答案

下面是什么V7的Unix编译器 CC 生产(使用的 SIMH 并从的 TUHS ):

Here's what the V7 Unix compiler cc produces (using SIMH and an image from TUHS):

$ cat>a.c
main(){
 while(1);
}
$ cat>b.c
main(){
 for(;;);
}
$ cc -S a.c
$ cc -S b.c

交流转换器编译成(而

a.c (while) compiles to:

.globl  _main
.text
_main:
~~main:
jsr     r5,csv
jbr     L1
L2:L4:tst       $1
jeq     L5
jbr     L4
L5:L3:jmp       cret
L1:jbr  L2
.globl
.data

b.c )变为:

.globl  _main
.text
_main:
~~main:
jsr     r5,csv
jbr     L1
L2:L4:jbr       L4
L5:L3:jmp       cret
L1:jbr  L2
.globl
.data

所以,不使用优化编译指令较少(;;)它至少真实的。然而,随着 -O 编译时,这两个程序产生的究竟的相同的程序集:

So it's at least true that for(;;) compiled to fewer instructions when not using optimization. However, when compiling with -O, both programs produce exactly the same assembly:

.globl  _main
.text
_main:
~~main:
jsr     r5,csv
L4:jbr  L4
.globl
.data

和当我添加的循环体的printf(你好); ,节目仍然是相同的。

and when I add a loop body of printf("Hello");, the programs are still the same.

所以,它可能是成语有其PDP-11机器语言的起源,但到了1979年的差异已经基本失去意义。

So, it might be that the idiom has its origins in PDP-11 machine language, but by 1979 the difference was already largely irrelevant.

这篇关于IS"对于(;;)QUOT;成语无限循环正确地归因于PDP-11 C编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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