汇编语言有性能优势,C#有灵活性,组织结构优势。两者结合使用是我的理想。
想法是大的组件和类层次使用C#;关键子程序使用汇编DLL。
可以从网上下载MASM32工具包。版本可以是8.2或者9.0。
我用汇编写了一个DLL。功能是求和整数数组。

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.code
LibMain proc hInstDLL:DWORD, reason:DWORD, unused:DWORD
    .if reason == DLL_PROCESS_ATTACH
        MOV EAX,TRUE
        ret
    .endif
    ret
LibMain Endp

SumArray proc StdCall point:DWORD,len:DWORD
    push esi
    push ebx
    mov esi,point
    mov ebx,len
    mov eax,[esi]

    dec ebx
    .if ! zero?
        .repeat
            add esi,4
            add eax,[esi]
            dec ebx
        .until zero?
    .endif

    pop ebx
    pop esi
    ret
SumArray endp

End LibMain

我也是刚学的汇编,如果有什么失误的话,就请多指教了。
保存为dll.asm之后,点project->build all菜单。已经生成了dll.obj。
之后写dll.def文件:

LIBRARY dll
EXPORTS SumArray

运行masm32/bin目录下的命令link /subsystem:windows /dll /def:d:/project/asm/dll.def d:/project/asm/dll.obj
这样在bin目录下就有了dll.dll文件。(不是很熟,勿笑)

在C#里调用这个dll并不复杂。
1.将那个dll.dll拷贝到bin/debug目录下。
2.加上using System.Runtime.InteropServices;
3.声明外部方法
    [DllImport("dll.dll")]
    public static extern Int32 SumArray(Int32* point, Int32 length);
4.调用
    sum = SumArray(point, source.Length);
5.因为使用了指针,所以需要把类设为unsafe;
    public unsafe partial class Form1
6.设定项目的unsafe属性。

我编了段程序测试它的性能:

 

C#调用汇编dll (载)_汇编语言            int[] source = new int[100000];
C#调用汇编dll (载)_汇编语言            Random random = new Random();
C#调用汇编dll (载)_汇编语言            for (int i = 0; i < source.Length; i++)
C#调用汇编dll (载)_sed_04C#调用汇编dll (载)_sed_05            C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                source[i] = random.Next(100);
C#调用汇编dll (载)_子程序_08            }
C#调用汇编dll (载)_汇编语言
C#调用汇编dll (载)_汇编语言            int sum=0;
C#调用汇编dll (载)_汇编语言            long start = Environment.TickCount;
C#调用汇编dll (载)_汇编语言            for (int time = 0; time < 1000; time++)
C#调用汇编dll (载)_sed_04C#调用汇编dll (载)_sed_05            C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                sum = 0;
C#调用汇编dll (载)_sed_07                foreach (int v in source)
C#调用汇编dll (载)_汇编语言_18C#调用汇编dll (载)_sed_19                C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                    sum += v;
C#调用汇编dll (载)_sed_22                }
C#调用汇编dll (载)_子程序_08            }
C#调用汇编dll (载)_汇编语言            long cost = Environment.TickCount - start;
C#调用汇编dll (载)_汇编语言            Console.WriteLine("MSIL result={0};cost={1}", sum, cost);
C#调用汇编dll (载)_汇编语言
C#调用汇编dll (载)_汇编语言            start = Environment.TickCount;
C#调用汇编dll (载)_汇编语言            for (int time = 0; time < 1000; time++)
C#调用汇编dll (载)_sed_04C#调用汇编dll (载)_sed_05            C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                sum = 0;
C#调用汇编dll (载)_sed_07                fixed(int* pstart = source, pend = &source[source.Length-1])
C#调用汇编dll (载)_汇编语言_18C#调用汇编dll (载)_sed_19                C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                    for (int* pnow=pstart; pnow < pend; pnow++)
C#调用汇编dll (载)_汇编语言_18C#调用汇编dll (载)_sed_19                    C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                        sum += *pnow;
C#调用汇编dll (载)_sed_22                    }
C#调用汇编dll (载)_sed_22                }
C#调用汇编dll (载)_子程序_08            }
C#调用汇编dll (载)_汇编语言            cost = Environment.TickCount - start;
C#调用汇编dll (载)_汇编语言            Console.WriteLine("CSP result={0};cost={1}", sum, cost);
C#调用汇编dll (载)_汇编语言
C#调用汇编dll (载)_汇编语言            fixed (int* point = source)
C#调用汇编dll (载)_sed_04C#调用汇编dll (载)_sed_05            C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                start = Environment.TickCount;
C#调用汇编dll (载)_sed_07                for (int time = 0; time < 1000; time++)
C#调用汇编dll (载)_汇编语言_18C#调用汇编dll (载)_sed_19                C#调用汇编dll (载)_mysql数据库_06{
C#调用汇编dll (载)_sed_07                    sum = CalArray(point, source.Length);
C#调用汇编dll (载)_sed_22                }
C#调用汇编dll (载)_sed_07                cost = Environment.TickCount - start;
C#调用汇编dll (载)_sed_07                Console.WriteLine("MASM result={0};cost={1}",sum,cost);
C#调用汇编dll (载)_子程序_08            }
C#调用汇编dll (载)_汇编语言

跑下来的结果,C#遍历求和平均630毫秒,C#指针求和420毫秒,汇编求和180毫秒。这还没用MMX。