自动换行 【由 孤帆代码着色器1.0.1 着色】 孤帆Blog
1 以前练手写的代码,拿来凑些热闹.如果有不妥之处请各位多多指教!
2 ;asm部份
3 .386
4 .model flat,stdcall
5 option casemap:none
6
7 .code
8 start:
9 ;冒泡排序.只对long型数字按伸序排序
10 DunkSort proc uses edx edi ebx /
11 lpArray:DWORD,nlen:DWORD,x1,x2
12 mov edi,lpArray ;取第一个形参
13 mov ecx,nlen ;取第二个形参
14 dec ecx ;置外循环次数
15 ;mov dl,1
16 lopfa:
17 ;or dl,dl
18 ;jz exit
19 ;xor dl,dl
20 push ecx ;保存外循环次数
21 xor ebx,ebx
22 lopch:
23 mov eax,[edi+ebx]
24 cmp eax,[edi+ebx+4]
25 jle next
26 xchg eax, [edi+ebx+4]
27 mov [edi+ebx],eax
28 ;mov dl,1
29 next:
30 add ebx,4
31 loop lopch
32 pop ecx ;弹出外循环次数
33 loop lopfa
34 exit:
35 ret
36 DunkSort ENDP
37 end start
38
39 VERSION 5.00
40 Begin VB.Form frmSort
41 Caption = "VB内嵌汇编玩排序"
42 ClientHeight = 5850
43 ClientLeft = 60
44 ClientTop = 450
45 ClientWidth = 10425
46 LinkTopic = "Form1"
47 ScaleHeight = 5850
48 ScaleWidth = 10425
49 StartUpPosition = 3 '窗口缺省
50 Begin VB.TextBox txtUseTime
51 Height = 285
52 Left = 780
53 TabIndex = 6
54 Top = 5310
55 Width = 1110
56 End
57 Begin VB.CommandButton cmdSortVB
58 Caption = "VB冒泡排序"
59 Enabled = 0 'False
60 Height = 390
61 Left = 6045
62 TabIndex = 4
63 Top = 5265
64 Width = 1140
65 End
66 Begin VB.CommandButton cmdSortAsm
67 Caption = "ASM冒泡排序"
68 Enabled = 0 'False
69 Height = 390
70 Left = 7395
71 TabIndex = 3
72 Top = 5265
73 Width = 1140
74 End
75 Begin VB.CommandButton cmdRndArray
76 Caption = "随机产生"
77 Height = 390
78 Left = 4650
79 TabIndex = 2
80 Top = 5250
81 Width = 1140
82 End
83 Begin VB.TextBox txtArrBottom
84 Height = 285
85 Left = 4050
86 TabIndex = 0
87 Top = 5295
88 Width = 465
89 End
90 Begin VB.Label Label2
91 AutoSize = -1 'True
92 Caption = "耗时: n秒"
93 Height = 180
94 Left = 210
95 TabIndex = 5
96 Top = 5355
97 Width = 1980
98 End
99 Begin VB.Label Label1
100 AutoSize = -1 'True
101 Caption = "数组下标:"
102 Height = 180
103 Left = 3165
104 TabIndex = 1
105 Top = 5340
106 Width = 900
107 End
108 End
109 Attribute VB_Name = "frmSort"
110 Attribute VB_GlobalNameSpace = False
111 Attribute VB_Creatable = False
112 Attribute VB_PredeclaredId = True
113 Attribute VB_Exposed = False
114 Option Explicit
115 Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
116 Private Type LARGE_INTEGER
117 lowpart As Long
118 highpart As Long
119 End Type
120 Private Declare Function CallAsmProc Lib "user32" Alias "CallWindowProcA" _
121 (ByVal lpAsmFn&, ByVal var1&, ByVal var2&, ByVal var3&, ByVal var4&) As Long
122
123 Dim szAsmDKfn(0 To 46) As Byte '汇编字节码
124 Dim arrVB() As Long, arrAsmDK() As Long '欲排序的数组
125 Dim arrBottom As Long '数组下标
126
127 '随机产生排序数组值
128 Private Sub cmdRndArray_Click()
129 Dim I As Long
130 Me.Cls
131 arrBottom = Val(txtArrBottom.Text)
132 If arrBottom < 1 Then Exit Sub
133 ReDim arrVB(arrBottom), arrAsmDK(arrBottom)
134 For I = 0 To arrBottom
135 arrVB(I) = Rnd * (arrBottom + 20)
136 arrAsmDK(I) = arrVB(I)
137 Next
138
139 PrintData arrVB
140 cmdSortVB.Enabled = True: cmdSortAsm.Enabled = True
141 End Sub
142
143 '使用内嵌汇编排序
144 Private Sub cmdSortAsm_Click()
145 Dim J As Long, I As Long, tmp As Long, t1 As Long
146 t1 = GetTime
147 CallAsmProc VarPtr(szAsmDKfn(0)), VarPtr(arrAsmDK(0)), arrBottom + 1, 0, 0
148 txtUseTime.Text = GetTime - t1
149
150 PrintData arrAsmDK
151 cmdSortAsm.Enabled = False
152 End Sub
153
154 'vb排序
155 Private Sub cmdSortVB_Click()
156 Dim J As Long, I As Long, tmp As Long, t1 As Long
157 t1 = GetTime
158 For I = 0 To arrBottom - 1
159 For J = 0 To arrBottom - I - 1
160 If arrVB(J) > arrVB(J + 1) Then
161 tmp = arrVB(J)
162 arrVB(J) = arrVB(J + 1): arrVB(J + 1) = tmp
163 End If
164 Next
165 Next
166 txtUseTime.Text = GetTime - t1
167
168 PrintData arrVB
169 cmdSortVB.Enabled = False
170 End Sub
171
172 Private Sub Form_Load()
173 '汇编字节码数组赋值
174 szAsmDKfn(0) = 85: szAsmDKfn(1) = 139: szAsmDKfn(2) = 236: szAsmDKfn(3) = 82: szAsmDKfn(4) = 87: szAsmDKfn(5) = 83
175 szAsmDKfn(6) = 139: szAsmDKfn(7) = 125: szAsmDKfn(8) = 8: szAsmDKfn(9) = 139: szAsmDKfn(10) = 77: szAsmDKfn(11) = 12
176 szAsmDKfn(12) = 73: szAsmDKfn(13) = 81: szAsmDKfn(14) = 51: szAsmDKfn(15) = 219: szAsmDKfn(16) = 139: szAsmDKfn(17) = 4
177 szAsmDKfn(18) = 59: szAsmDKfn(19) = 59: szAsmDKfn(20) = 68: szAsmDKfn(21) = 59: szAsmDKfn(22) = 4: szAsmDKfn(23) = 126
178 szAsmDKfn(24) = 7: szAsmDKfn(25) = 135: szAsmDKfn(26) = 68: szAsmDKfn(27) = 59: szAsmDKfn(28) = 4: szAsmDKfn(29) = 137
179 szAsmDKfn(30) = 4: szAsmDKfn(31) = 59: szAsmDKfn(32) = 131: szAsmDKfn(33) = 195: szAsmDKfn(34) = 4: szAsmDKfn(35) = 226
180 szAsmDKfn(36) = 235: szAsmDKfn(37) = 89: szAsmDKfn(38) = 226: szAsmDKfn(39) = 229: szAsmDKfn(40) = 91: szAsmDKfn(41) = 95
181 szAsmDKfn(42) = 90: szAsmDKfn(43) = 201: szAsmDKfn(44) = 194: szAsmDKfn(45) = 16: szAsmDKfn(46) = 0
182 End Sub
183
184 '打印数组值
185 Private Sub PrintData(arr() As Long)
186 Dim I As Long
187 Me.Cls
188 For I = 0 To arrBottom
189 Print arr(I);
190 If (I + 1) Mod 20 = 0 Then Print
191 Next
192 End Sub
193
194 '测试使用时间函数
195 Private Function GetTime() As Long
196 Dim LI As LARGE_INTEGER
197 QueryPerformanceCounter LI
198 GetTime = LI.lowpart
199 End Function
200