4Bytes
4Bytes

Reputation: 345

Fully Array alternative

When I tried to create my own alternative to classic array, I saw, that into disassembly code added one instruction: mov edx,dword ptr [myarray]. Why this additional instruction was added?

I want to use my functionality of my alternative, but do not want to lose performance! How to resolve this question? Every processor cycle is important for this application.

For example:

for (unsigned i = 0; i < 10; ++i)
{
    array1[i] = i;
    array2[i] = 10 - i;
}

Assembly (classic int arrays):

mov edx, dword ptr [ebp-480h]  
mov eax, dword ptr [ebp-480h]  
mov dword ptr array1[edx*4], eax  

mov ecx, 10
sub ecx, dword ptr [ebp-480h]
mov edx, dword ptr [ebp-480h]
mov dword ptr array2[edx*4], ecx

Assembly (my class):

mov edx,dword ptr [array1]
mov eax,dword ptr [ebp-43Ch]
mov ecx,dword ptr [ebp-43Ch]
mov dword ptr [edx+eax*4], ecx

mov edx, 10
sub edx, dword ptr [ebp-43Ch]
mov eax, dword ptr [array2]
mov ecx, dword ptr [ebp-43Ch]
mov dword ptr [eax+ecx*4], edx

Upvotes: 1

Views: 141

Answers (1)

Cole Tobin
Cole Tobin

Reputation: 9426

One instruction is not a loss of performance with today's processors. I would not worry about it and instead suggest you read Coding Horror's article on micro optimization.

However, that instruction is just moving the first index (myarray+0) to edx so it can be used.

Upvotes: 3

Related Questions