DISTRIBUTION OF IMAGES Example of Mehridad (GENERAL VERSIONS)
I will try to understand Mehrdad a little deeper, for people like me who do not read assembly code very well. This code can be written in Visual Studio when we debbuging by clicking Debug -> Windows -> Dissasembly.
VERSION using REF
Source:
namespace RefTest { class Program { static void Test(ref object o) { GC.KeepAlive(o); } static void Main(string[] args) { object temp = args; Test(ref temp); } } }
Assembly Language (x86) (only showing the part that is different):
object temp = args; 00000030 mov eax,dword ptr [ebp-3Ch] 00000033 mov dword ptr [ebp-40h],eax Test(ref temp); 00000036 lea ecx,[ebp-40h]
VERSION WITHOUT REF.
Source:
namespace RefTest { class Program { static void Test(object o) { GC.KeepAlive(o); } static void Main(string[] args) { object temp = args; Test(temp); } } }
Assembly Language (x86) (only showing the part that is different):
object temp = args; 00000035 mov eax,dword ptr [ebp-3Ch] 00000038 mov dword ptr [ebp-40h],eax Test(temp); 0000003b mov ecx,dword ptr [ebp-40h]
In addition to the commented line, the code for both versions is the same: with ref, the function call precedes the LEA instruction, without a link to the link we have a simpler MOV instruction. After executing this line, LEA loaded the ecx register with a pointer to an object pointer, while MOV loaded ecx with an object pointer. This means that the FD30B000 routine (pointing to our test function) in the first case will have to make additional memory access to access the object. If we check the build code for each released version of this function, we will see that at some point (in fact, the only line that differs from the two versions) additional access is provided:
static void Test(ref object o) { GC.KeepAlive(o); } ... 00000025 mov eax,dword ptr [ebp-3Ch] 00000028 mov ecx,dword ptr [eax] ...
So far, a function without ref can go directly to the object:
static void Test(object o) { GC.KeepAlive(o); } ... 00000025 mov ecx,dword ptr [ebp-3Ch] ...
Hope this helps.