On Mon, Apr 18, 2011 at 1:47 PM, Zak Elep <[email protected]> wrote:
> I suppose the compiler optimized away
> the empty for(), among other things...

yup.. gcc will remove those doing nothing codes for optimization... in
this case the for() loop... im aware of this and thats the reason i
didnt use the -O argument.. i really thought there was a bug on the
openbsd scheduler or its gcc package and thats why i was eager to
install openbsd this morning and look for it :->

to demonstrate between optimize versus non-optimize compilation... we
can take a look at the machine code that gcc produced thru a gdb
debugger...

below.. compiled with no optimization..  you can see there when i
disassemble the code.. between line 13 and 14 produced an assembly
code for the for() loop...

$ gcc -g -o waste waste.c
$ gdb waste
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-23.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/rledesma/test/waste...done.
(gdb) list 9,17
9       void WasteCPU(int waste, int percent) {
10      int i;
11      struct timeval sleep;
12
13              for(i = waste; i; i--);
14              sleep.tv_sec = 0;
15              sleep.tv_usec = (100 - percent) * 10000;
16              select(1, NULL, NULL, NULL, &sleep);
17      }
(gdb) info line 9
Line 9 of "waste.c" starts at address 0x8048444 <WasteCPU> and ends at
0x804844a <WasteCPU+6>.
(gdb) disassemble /m 0x804844a
Dump of assembler code for function WasteCPU:
9       void WasteCPU(int waste, int percent) {
0x08048444 <WasteCPU+0>:        push   %ebp
0x08048445 <WasteCPU+1>:        mov    %esp,%ebp
0x08048447 <WasteCPU+3>:        sub    $0x28,%esp

10      int i;
11      struct timeval sleep;
12
13              for(i = waste; i; i--);
0x0804844a <WasteCPU+6>:        mov    0x8(%ebp),%eax
0x0804844d <WasteCPU+9>:        mov    %eax,-0x4(%ebp)
0x08048450 <WasteCPU+12>:       jmp    0x8048456 <WasteCPU+18>
0x08048452 <WasteCPU+14>:       subl   $0x1,-0x4(%ebp)
0x08048456 <WasteCPU+18>:       cmpl   $0x0,-0x4(%ebp)
0x0804845a <WasteCPU+22>:       jne    0x8048452 <WasteCPU+14>

14              sleep.tv_sec = 0;
0x0804845c <WasteCPU+24>:       movl   $0x0,-0xc(%ebp)

15              sleep.tv_usec = (100 - percent) * 10000;
0x08048463 <WasteCPU+31>:       mov    $0x64,%eax
0x08048468 <WasteCPU+36>:       sub    0xc(%ebp),%eax
0x0804846b <WasteCPU+39>:       imul   $0x2710,%eax,%eax
0x08048471 <WasteCPU+45>:       mov    %eax,-0x8(%ebp)

16              select(1, NULL, NULL, NULL, &sleep);
0x08048474 <WasteCPU+48>:       lea    -0xc(%ebp),%eax
0x08048477 <WasteCPU+51>:       mov    %eax,0x10(%esp)
0x0804847b <WasteCPU+55>:       movl   $0x0,0xc(%esp)
0x08048483 <WasteCPU+63>:       movl   $0x0,0x8(%esp)
0x0804848b <WasteCPU+71>:       movl   $0x0,0x4(%esp)
0x08048493 <WasteCPU+79>:       movl   $0x1,(%esp)
0x0804849a <WasteCPU+86>:       call   0x8048348 <select@plt>

17      }
0x0804849f <WasteCPU+91>:       leave
0x080484a0 <WasteCPU+92>:       ret

End of assembler dump.


below.. compiled with optimization and no machine code produced
between line 13 and 14...

$ gcc -O2 -g -o waste waste.c
$ gdb waste
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-23.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/rledesma/test/waste...done.
(gdb) list 9,17
9       void WasteCPU(int waste, int percent) {
10      int i;
11      struct timeval sleep;
12
13              for(i = waste; i; i--);
14              sleep.tv_sec = 0;
15              sleep.tv_usec = (100 - percent) * 10000;
16              select(1, NULL, NULL, NULL, &sleep);
17      }
(gdb) info line 9
Line 9 of "waste.c" starts at address 0x8048460 <WasteCPU> and ends at
0x8048461 <WasteCPU+1>.
(gdb) disassemble /m 0x8048461
Dump of assembler code for function WasteCPU:
9       void WasteCPU(int waste, int percent) {
0x08048460 <WasteCPU+0>:        push   %ebp
0x08048466 <WasteCPU+6>:        mov    %esp,%ebp
0x08048468 <WasteCPU+8>:        sub    $0x28,%esp

10      int i;
11      struct timeval sleep;
12
13              for(i = waste; i; i--);
14              sleep.tv_sec = 0;
0x0804846e <WasteCPU+14>:       movl   $0x0,-0x8(%ebp)

15              sleep.tv_usec = (100 - percent) * 10000;
0x08048461 <WasteCPU+1>:        mov    $0x64,%eax
0x0804846b <WasteCPU+11>:       sub    0xc(%ebp),%eax
0x0804847d <WasteCPU+29>:       imul   $0x2710,%eax,%eax
0x0804849a <WasteCPU+58>:       mov    %eax,-0x4(%ebp)

16              select(1, NULL, NULL, NULL, &sleep);
0x08048475 <WasteCPU+21>:       movl   $0x0,0xc(%esp)
0x08048483 <WasteCPU+35>:       movl   $0x0,0x8(%esp)
0x0804848b <WasteCPU+43>:       movl   $0x0,0x4(%esp)
0x08048493 <WasteCPU+51>:       movl   $0x1,(%esp)
0x0804849d <WasteCPU+61>:       lea    -0x8(%ebp),%eax
0x080484a0 <WasteCPU+64>:       mov    %eax,0x10(%esp)
0x080484a4 <WasteCPU+68>:       call   0x8048354 <select@plt>

17      }
0x080484a9 <WasteCPU+73>:       leave
0x080484aa <WasteCPU+74>:       ret
0x080484ab:     nop
0x080484ac:     lea    0x0(%esi,%eiz,1),%esi

End of assembler dump.


fooler.
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
http://lists.linux.org.ph/mailman/listinfo/plug
Searchable Archives: http://archives.free.net.ph

Reply via email to