is“for(;;)”习惯用于无限循环正确归因于PDP-11 C编译器? [英] Is "for(;;)" idiom for infinite loop correctly attributed to PDP-11 C compiler?
问题描述
我最近发现这篇文章声称喜欢 for(;;) over
while(1)
用于无限循环,因为最初在PDP-11上可用的C编译器生成一个额外的机器指令 while(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)
.
Btw now even Visual C++ warnings tend to favor the former.
对于<; c>
How realistic is such attribution of for(;;)
idiom?
推荐答案
以下是V7 Unix编译器 code>产生(使用 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
ac
( while
)编译为:
.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
bc
)变为:
While b.c
(for
) becomes:
.globl _main
.text
_main:
~~main:
jsr r5,csv
jbr L1
L2:L4:jbr L4
L5:L3:jmp cret
L1:jbr L2
.globl
.data
所以至少是 for(;;)
编译为更少的指令时不使用优化。但是,在使用 -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(Hello);
,程序仍然相同。
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“for(;;)”习惯用于无限循环正确归因于PDP-11 C编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!