Re: Strange code generated by compiler
On Mar 22, 9:43 am, Norbert Unterberg <nunterb...@newsgroups.nospam>
wrote:
Autumn schrieb:
v[3]=78;
0040103B mov dword ptr [eax+0Ch],4Eh
...
this is ok until:
v[7]=22;
00401057 mov edi,16h
0040105C mov dword ptr [eax+1Ch],edi
here 16h (22) is copied to edi before being copied to dword ptr [eax
+1Ch]
why does VC do this?
Show your complete function. Maybe you use the value 22 more than once, so the
compiler gains sowe clock cycles by caching the value in a register.
Norbert
Oh yes, thanks! I do use the same numbers a few lines below to
initialize a normal C style array.
You can see the complete code at the end of this message. from line
004010B4 to 004010BC
the program uses those registers to assign those numbers to the normal
array. but I'm
still not convinced why VC needs to do this. what it is doing now is:
v[6]=87;
00401050 mov edi,57h
00401055 mov dword ptr [eax+18h],edi
and then:
004010B4 mov dword ptr [esp+38h],edi
while it could simply do this:
00401055 mov dword ptr [eax+18h],57h
004010B4 mov dword ptr [esp+38h],57h
2 instructions vs 3! it's faster or maybe I'm missing something
obvious here due to
my lack of assembly expertise :)
the loop at the end of the program is to convince the compiler that
these arrays are
useful things otherwise it will ignore them.
I made 'sum' volatile at the end of the program because if I don't do
that
VC doesn't make the normal array at all ! it expands the loop and adds
the numbers
assigned to array elements one by one to sum! VC compiler is so
intelligent by the way ;)
#define _SECURE_SCL 0
#include <vector>
using namespace std;
int main()
{
00401000 push ebp
00401001 mov ebp,esp
00401003 and esp,0FFFFFFF8h
00401006 sub esp,1A8h
0040100C push esi
0040100D push edi
vector<int> v(100);
0040100E lea edi,[esp+0Ch]
00401012 lea esi,[esp+10h]
00401016 mov dword ptr [esp+0Ch],0
0040101E call std::vector<int,std::allocator<int>
::_Construct_n (401100h)
v[0]=45;
00401023 mov eax,dword ptr [esp+14h]
00401027 mov dword ptr [eax],2Dh
v[1]=56;
0040102D mov dword ptr [eax+4],38h
v[2]=85;
00401034 mov dword ptr [eax+8],55h
v[3]=78;
0040103B mov dword ptr [eax+0Ch],4Eh
v[4]=88;
00401042 mov dword ptr [eax+10h],58h
v[5]=120;
00401049 mov dword ptr [eax+14h],78h
v[6]=87;
00401050 mov edi,57h
00401055 mov dword ptr [eax+18h],edi
v[7]=22;
00401058 mov esi,16h
0040105D mov dword ptr [eax+1Ch],esi
v[8]=55;
00401060 mov edx,37h
00401065 mov dword ptr [eax+20h],edx
v[9]=125;
00401068 mov dword ptr [eax+24h],7Dh
v[10]=12;
0040106F mov ecx,0Ch
00401074 mov dword ptr [eax+28h],ecx
v[11]=456;
int a[100];
a[0]=45;
a[1]=56;
a[2]=85;
a[3]=78;
a[4]=88;
a[5]=12;
00401077 mov dword ptr [esp+34h],ecx
a[6]=87;
a[7]=22;
a[8]=55;
0040107B mov dword ptr [esp+40h],edx
volatile int sum=0;
0040107F xor ecx,ecx
00401081 mov dword ptr [eax+2Ch],1C8h
for(int i=0;i<9;i++) {
00401088 lea edx,[esp+20h]
0040108C mov dword ptr [esp+20h],2Dh
00401094 mov dword ptr [esp+24h],38h
0040109C mov dword ptr [esp+28h],55h
004010A4 mov dword ptr [esp+2Ch],4Eh
004010AC mov dword ptr [esp+30h],58h
004010B4 mov dword ptr [esp+38h],edi
004010B8 mov dword ptr [esp+3Ch],esi
004010BC mov dword ptr [esp+8],ecx
004010C0 sub eax,edx
004010C2 lea edx,[eax+ecx*4]
sum+=v[i]*a[i];
004010C5 mov edx,dword ptr [esp+edx+20h]
004010C9 imul edx,dword ptr [esp+ecx*4+20h]
004010CE add dword ptr [esp+8],edx
004010D2 add ecx,1
004010D5 cmp ecx,9
004010D8 jl main+0C2h (4010C2h)
}
return sum;
004010DA mov esi,dword ptr [esp+8]
004010DE mov eax,dword ptr [esp+14h]
004010E2 push eax
004010E3 call operator delete (401168h)
004010E8 add esp,4
}
004010EB pop edi
004010EC mov eax,esi
004010EE pop esi
004010EF mov esp,ebp
004010F1 pop ebp
004010F2 ret