PDA

View Full Version : [Asm/C]Asm prodotto e ottimizzazioni


Luc@s
09-09-2004, 20:30
questo è il sorgente C che ho compilato:

/*
### Copyright (c) 2004 Luca Francesca
### This script is provided under the terms of the GNU General Public License
### (see http://www.gnu.org/licenses/gpl.html for the full license text.)
*/
#include <stdio.h>
#include <stdlib.h>
#define MASK 0x40 /* control bit 6 */

int main(int argc, char *argv[])
{
unsigned int i = 100, tmp1 , tmp2, tmp3, tmp4;
tmp1 = i & MASK;
tmp2 = i ^ MASK;
tmp3 = (i >> 2);
tmp4 = (i << 2);
printf("i = %d \n \
i & MASK = %d \n \
i ^ MASK = %d \n \
i >> 2 = %d \n\
i << 2 = %d\n", i, tmp1, tmp2, tmp3, tmp4);
char c;
scanf("%c", &c);
return 0;
}



Questo il risultato senza opt:

.file "i.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 32
LC0:
.ascii "i = %d \12 i & MASK = %d \12 i ^ MASK = %d \12 i >> 1 = %d \12 i << 1 = %d\12\0"
LC1:
.ascii "%c\0"
.align 2
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -28(%ebp)
movl -28(%ebp), %eax
call __alloca
call ___main
movl $100, -4(%ebp)
movl -4(%ebp), %eax
andl $64, %eax
movl %eax, -8(%ebp)
movl -4(%ebp), %eax
xorl $64, %eax
movl %eax, -12(%ebp)
movl -4(%ebp), %eax
shrl %eax
movl %eax, -16(%ebp)
movl -4(%ebp), %eax
addl %eax, %eax
movl %eax, -20(%ebp)
subl $8, %esp
pushl -20(%ebp)
pushl -16(%ebp)
pushl -12(%ebp)
pushl -8(%ebp)
pushl -4(%ebp)
pushl $LC0
call _printf
addl $32, %esp
subl $8, %esp
leal -21(%ebp), %eax
pushl %eax
pushl $LC1
call _scanf
addl $16, %esp
movl $0, %eax
leave
ret
.def _scanf; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef



Questo con -O3 -std=c99

.file "i.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 32
LC0:
.ascii "i = %d \12 i & MASK = %d \12 i ^ MASK = %d \12 i >> 2 = %d \12 i << 2 = %d\12\0"
LC1:
.ascii "%c\0"
.align 2
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
xorl %eax, %eax
movl %esp, %ebp
pushl %edx
pushl %edx
andl $-16, %esp
call __alloca
call ___main
pushl %eax
pushl %eax
pushl $400
pushl $25
pushl $36
pushl $64
pushl $100
pushl $LC0
call _printf
leal -1(%ebp), %edx
addl $24, %esp
pushl %edx
pushl $LC1
call _scanf
movl %ebp, %esp
xorl %eax, %eax
popl %ebp
ret
.def _scanf; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef



La v in O3 sa gia quali valori dare a printf mentre la senza opt li prende dallo stack......questo ad una prima analisi...

Ora vorrei riflettere con voi su una cosa.......ma perche cìè questo divario di codice?

P.S: qualcuno puo postare il codice del VC++6 e del VC++.NET per confrontare l'output??
P.P.S: se volete..chiudetemi il post.....ma secondo me è molto bello vedere le diff tra compilatori

ilsensine
09-09-2004, 21:01
Cosa ti aspettavi scusa? Che l'ottimizzatore facesse ugualmente i calcoli, anche se poteva conoscere il risultato a priori? :D

Luc@s
09-09-2004, 21:07
la mia era curiosita :D
Il mio PS sarebbe motlo interessante cmq, IMHO :D

ilsensine
09-09-2004, 21:17
Intanto, IO ho fatto meglio di TE :D

.file "aa.c"
.version "01.01"
gcc2_compiled.:
.section .rodata.str1.32,"ams",@progbits,1
.align 32
.LC0:
.string "i = %d \n i & MASK = %d \n i ^ MASK = %d \n i >> 2 = %d \n i << 2 = %d\n"
.section .rodata.str1.1,"ams",@progbits,1
.LC1:
.string "%c"
.text
.align 16
.globl main
.type main,@function
main:
subl $20, %esp
pushl $400
pushl $25
pushl $36
pushl $64
pushl $100
pushl $.LC0
call printf
addl $24, %esp
leal 19(%esp), %eax
pushl %eax
pushl $.LC1
call scanf
xorl %eax, %eax
addl $28, %esp
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)"

:p

ilsensine
09-09-2004, 21:53
...la mano magica :D
(mi scuso per le .string, ma non mi veniva in mente altro per riservare un pò di spazio :p )


.file "aa.c"
.version "01.01"
gcc2_compiled.:
.section .rodata.str1.32,"ams",@progbits,1
.align 32
.LC0:
.string "i = %d \n i & MASK = %d \n i ^ MASK = %d \n i >> 2 = %d \n i << 2 = %d\n"
.section .rodata.str1.1,"ams",@progbits,1
.text
.align 16
.globl main
.type main,@function
main:
movl %esp, (zampa)
leal zumpa, %esp
call printf
movl (zampa), %esp
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)"
.data
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
.string " "
zumpa:
.long .LC0
.long 100
.long 64
.long 36
.long 25
.long 400
zampa:
.long 0

VICIUS
09-09-2004, 21:57
questa è la versione del 3.5 :D
.file "a.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "i = %d \n i & MASK = %d \n i ^ MASK = %d \n i >> 2 = %d \n i << 2 = %d\n"
.LC1:
.string "%c"
.text
.align 4
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushl %eax
pushl $400
pushl $25
pushl $36
pushl $64
pushl $100
pushl $.LC0
call printf
leal -1(%ebp), %eax
pushl %eax
pushl $.LC1
call scanf
xorl %eax, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.5.0 20040724 (experimental) (Debian 3.5-0pre1)"
.section .note.GNU-stack,"",@progbits

ciao ;)

ilsensine
09-09-2004, 22:07
Battetemi questa :p
(oops mi ero scordato la scanf :D )

fmt: .string "%c\0i = %d \n i & MASK = %d \n i ^ MASK = %d \n i >> 2 = %d \n i << 2 = %d\n"
.globl main
main:
movl %esp, (zampa)
leal zumpa, %esp
call printf
leal zompa, %esp
call scanf
movl (zampa), %esp
ret
.data
zampa:
.zero 4096
zumpa:
.long fmt+3
.long 100
.long 64
.long 36
.long 25
.long 400
zompa:
.long fmt
.long zampa+4

VICIUS
09-09-2004, 22:32
Originariamente inviato da ilsensine
Battetemi questa :p
(oops mi ero scordato la scanf :D )
[...]

che opzione hai usato ? -fuse-zompa-labels ? ma se volessi provare anche io ilsecc dove lo posso scaricare ? :D

ciao ;)