monoqlo's clip

448 notes

最近ツイッターで

“while(1){LOVE++}”と書き込まれてる指輪があって、

そのソースは最適化でLOVEが消えてただの無限ループになる云々という話があったので検証。


用意したソースはこちら。

int main()
{
int LOVE = 0;
while(1){LOVE++;}
}

それじゃアセンブラを見てみましょ。

まずは最適化抜き。


gcc -O0 -s hoge.c

.file “hoge.c”
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $0, -4(%ebp) ;LOVE=0
.L2:
addl $1, -4(%ebp) ;LOVE++
jmp .L2 ;JUMP TO L2
.size main, .-main
.ident “GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5”
.section .note.GNU-stack,”“,@progbits
LOVEは残ってますね。


gcc -O1 -s hoge.c

.file “hoge.c”
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
.L2:
jmp .L2 ;JUMP TO L2
.size main, .-main
.ident “GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5”
.section .note.GNU-stack,”“,@progbits


愛は最適化によって消滅しました。

while(1){LOVE++;}を最適化 - duost (via atm09td)

(via zaiga)

  1. beryl-rivers reblogged this from eimelle and added:
    while(1){LOVE ;}を最適化
  2. usoro2 reblogged this from pengm-q
  3. katumayo reblogged this from gearmann
  4. iaskell reblogged this from gearmann
  5. gearmann reblogged this from poochin
  6. fyam reblogged this from poochin
  7. flashno1 reblogged this from poochin
  8. kappumblr reblogged this from poochin
  9. poochin reblogged this from teokure
  10. teokure reblogged this from goooose350
  11. kuibar reblogged this from misao17
  12. bdal400 reblogged this from es-s-etc
  13. es-s-etc reblogged this from arccosine
  14. goooose350 reblogged this from yamada123456789
  15. bubirons reblogged this from exilias
  16. misao17 reblogged this from anijakatze
  17. yamada123456789 reblogged this from pengm-q
  18. soulsphere reblogged this from kyomtea
  19. chlono reblogged this from eimelle
  20. pengm-q reblogged this from oyaki
  21. yoc reblogged this from eimelle
  22. oyaki reblogged this from manshiro
  23. tsunafkin reblogged this from amagiri
  24. amagiri reblogged this from arccosine
  25. smbd reblogged this from anijakatze
  26. exilias reblogged this from eimelle
  27. manshiro reblogged this from anijakatze
  28. arccosine reblogged this from anijakatze
  29. kyomtea reblogged this from anijakatze
  30. anijakatze reblogged this from eimelle
  31. eimelle reblogged this from yuibb
  32. jjjackps reblogged this from mamemomonga
  33. bivanilla reblogged this from hcm-pri
  34. mamemomonga reblogged this from hcm-pri
  35. hcm-pri reblogged this from yuibb
  36. mozillerman reblogged this from yuibb
  37. inariwan reblogged this from misoka
  38. ryhmrt reblogged this from chordstriker
  39. mkoji reblogged this from chordstriker
  40. chordstriker reblogged this from hishochi