读书人

汇编-实现二分法跟划分法

发布时间: 2013-10-31 12:03:52 作者: rapoo

汇编--实现二分法和划分法

include irvine32.inc.dataarray DWORD 2,6,12,17,24,29,53,64,80,91         ;Define an array array1 DWORD 51,3,34,6,5,1,31,19,72,48step DWORD TYPE array;this is stepkey BYTE 12pivot BYTE 30;Keyprompt BYTE "There is not key!!!";Prompt.codemain PROCmov esi,offset array;esi load the array of pointermov ecx,lengthof array;ecx is the length of arraymov al,key;It is the keycall binarySearchBcall crlfmov esi,offset array1mov al,pivotcall partitionBcall crlf call waitmsgexitmain ENDPbinarySearchB PROC uses EAX EBX ECX ESI EDXmov edx,0;for the first element in array(first)mov ecx,lengthof array - 1;foe the last element in array(last)beginwhile:;This is the main whilecmp edx,ecx;compare first and lastJA failwhile;if first > last and still do not find key;jump to failwhilemov ebp,edx;add ebp,ecx;calculate the mid = (first + last)/2 shr ebp,1;mov AH,[esi+ebp*4];move array[mid] to the AH registercmp AL,AH;compare the key and the number in the arrayJA L2;jump if AL > AH  (JA is no-signed compare)JB L3;jump if AL < AH  (JB is also no-signed compare)JZ succeedwhile;jump if equal L2:inc ebpmov edx,ebp;let mid add 1 and assign to edx as firstJMP beginwhileL3:dec ebpmov ecx,ebp;let mid subtract 1 and assign to edx as lastJMP beginwhilesucceedwhile:mov eax,ebpcall writeint;output midretfailwhile:;output "There is no key!!!"mov edx,offset promptcall writestringretbinarySearchB ENDPpartitionB PROC uses ESI EDI EAX EBXmov ecx,lengthof array1mov esi,offset array1;mov edi,offset array1 + 40mov bh,0;bh is downmov bl,lengthof array1-1;bl is updec blbeginwhile:cmp bh,bl               ;while(down < up)JB while_1;go ahead to while_1JNB successwhile;if not ,succeedwhile_1:;whilecmp bh,bl;compare down and upJNA J1;if(down<=up) jump J1JA while_2;if not jump while_2J1:movzx edx,bhshl edx,2mov ah,[esi + edx]cmp ah,al;compare array[down] and pivotJNA J2;if(array[down]<=pivot) jump J2JA while_2;if not jump while_2J2:inc bh;handle bh.down ++jmp while_1;loopwhile_2:cmp bl,bh;compare bl(up) and bh(down)JNB J3;if up >= down jump J3JB Jump_if;if not jump ifJ3:movzx edx,blshl edx,2mov ah,[esi + edx]cmp ah,al;compare array[up] and pivotJA J4;if bl >= bl jump J4JNA Jump_if;if not jump ifJ4:dec bl;handle bl(up)jmp while_2jump_if:cmp bh,blJB Jump_exchangeJNB beginwhileJump_exchange:;exchange its valuemovzx edx,bhshl edx,2mov edx,[esi+edx]          ;array[down]movzx edi,blshl edi,2mov edi,[esi+edi];array[up]movzx ecx,bhshl ecx,2mov [esi+ecx],edimovzx ecx,blshl ecx,2mov [esi+ecx],edxjmp beginwhilesuccesswhile:movzx eax,blcall writeintretpartitionB ENDPend main


读书人网 >编程

热点推荐