Re: lshift & rshift
On Sep 27, 5:23 am, "ie...@free .fr" <ie...@free.frw rote:
The first routine compiles the (a*7) to ((a*8-a), with the
multiplication by eight done via a shift. In any event it looks like
you have not, as suggested, turned up the optimization level for GCC,
so he's generated fairly lame code. Try it with -O2 and see if that
doesn't reduce to a mov/lea/sub/ret sequence.
On Sep 27, 5:23 am, "ie...@free .fr" <ie...@free.frw rote:
On Sep 27, 12:15 pm, "ie...@free .fr" <ie...@free.frw rote:
>
>
>
Finnaly I can, here you are :
I made gcc -S mult.c to get the assembly code.
>
****mult.c****
**************
int mult_by_7 (int a)
{
return a * 7;
>
}
>
int mult_by_2 (int a)
{
return a * 2;
>
}
>
****mult.s****
**************
.file "mult.c"
.text
.globl mult_by_7
.type mult_by_7, @function
mult_by_7:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, %eax
sall $3, %eax
subl %edx, %eax
popl %ebp
ret
.size mult_by_7, .-mult_by_7
.globl mult_by_2
.type mult_by_2, @function
mult_by_2:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
addl %eax, %eax
popl %ebp
ret
.size mult_by_2, .-mult_by_2
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progb its
>
I don't understand the first (assembly language is a little bit new
for me).
Bu for mult_by_2 you can see the addl %eax,%eax
so it changed a * 2 by a + A.
>
Or perhaps I don't understand what does this assembly code.
>
Can you post the snapshot of the internal of a*2 and a+a that you
got ?
got ?
I can't, it was in past. But I'm sure. I asked a friend, and he
explain me that on some architecture (like the HP adding machine)
there wasn't a multiplication in the CPU, so the multiplication was a
software and it was better to use + than *. And it was not a lshift, I
swear it.
explain me that on some architecture (like the HP adding machine)
there wasn't a multiplication in the CPU, so the multiplication was a
software and it was better to use + than *. And it was not a lshift, I
swear it.
Finnaly I can, here you are :
I made gcc -S mult.c to get the assembly code.
>
****mult.c****
**************
int mult_by_7 (int a)
{
return a * 7;
>
}
>
int mult_by_2 (int a)
{
return a * 2;
>
}
>
****mult.s****
**************
.file "mult.c"
.text
.globl mult_by_7
.type mult_by_7, @function
mult_by_7:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, %eax
sall $3, %eax
subl %edx, %eax
popl %ebp
ret
.size mult_by_7, .-mult_by_7
.globl mult_by_2
.type mult_by_2, @function
mult_by_2:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
addl %eax, %eax
popl %ebp
ret
.size mult_by_2, .-mult_by_2
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progb its
>
I don't understand the first (assembly language is a little bit new
for me).
Bu for mult_by_2 you can see the addl %eax,%eax
so it changed a * 2 by a + A.
>
Or perhaps I don't understand what does this assembly code.
The first routine compiles the (a*7) to ((a*8-a), with the
multiplication by eight done via a shift. In any event it looks like
you have not, as suggested, turned up the optimization level for GCC,
so he's generated fairly lame code. Try it with -O2 and see if that
doesn't reduce to a mov/lea/sub/ret sequence.
Comment