8086汇编排序(冒泡、选择、快速)

本代码通过测试,可通过masm查验

冒泡排序

DSEG SEGMENT
	SCORE DB 11H,02H,15H,32H,5H,6H,7H,8H,9H,10H,90 DUP(05H)
	MAX DB ?
	MIN DB ?
DSEG ENDS
CSEG SEGMENT
	ASSUME DS:DSEG,CS:CSEG
START:  MOV AX, DSEG
	MOV DS, AX
	;————————————————到这,上面的均是模板
	LEA BX, SCORE	   ;取数组的首地址
	MOV CX, 100	   ;控制循环次数
	XOR SI, SI	   ;将SI清零
	XOR DI, DI	   ;将DI清零
    L1: MOV AH, [BX+SI] ;用基变址寻址取操作数,L1为外循环,(SI)为循环变量,;相当于i
    L2: MOV AL, [BX+DI] ;L2为内循环,(DI)为循环变量,相当于j
	CMP AH, AL
	JAE L3
	MOV DH, AH      ;AH=AL,不需交换,(AH)直接和后一个数比较,相当于j++
	CMP  DI,100	;判断内层循环是否结束
	JB   L2  	;没结束,继续循环;内层循环结束了
	INC  SI		;外层变量SI加一,相当于i++
	MOV  DI,SI	;相当于j=i
	LOOP L1  	;通过寄存器实现两个存储器数据间的交换
	MOV  AH,BYTE  PTR[BX];基址寻址
	MOV  AL,BYTE  PTR[BX+99]
	MOV  MAX,AH
	MOV  MIN,AL
	MOV  AH,4CH     ;返回操作系统
	INT  21H
CSEG ENDS
	END START

选择排序

DATA SEGMENT
	arr DB 73H,54H,68H,21H,32H,45H,67H,87H,95H,99H
DATA ENDS
CODE SEGMENT
ASSUME CS : CODE, DS : DATA
		START:  PUSH  DS
			XOR   AX, AX
			PUSH  AX
			MOV   AX, DATA
			MOV   DS, AX

			MOV   CX, 9      	; 内层循环次数     
			MOV   SI, OFFSET arr	; SI游动下标
			XOR   DI, SI     	; DI记录最小数下标	
			PUSH  SI		
		LOOP1:  INC   SI
			MOV   AL, [SI]
			CMP   AL, [DI] 
			JAE   LOOP2       ;unsigned data
			MOV   DI, SI  
		LOOP2:	DEC   CX			
			JNZ   LOOP1
			POP   SI
			MOV   AL,   [DI]
			XCHG  [SI], AL
			XCHG  AL,   [DI]
			INC   SI
			MOV   CX,   9
			SUB   CX,   SI   
			MOV   DI, SI
			PUSH  SI
			JNZ   LOOP1
			RET
CODE ENDS
     END START

快速排序(不用内存辅助排序)

DATASEG SEGMENT
	DATABASE DW 9991H,9999H,9996H,9993H,9992H,9998H,9994H,9990H,9997H,8888H
	NUM      DW 0014H
DATASEG ENDS
CODESEG SEGMENT
	ASSUME   CS : CODESEG, DS : DATASEG
	MAIN PROC FAR
	    START :		
						PUSH DS
						XOR  AX,AX
						PUSH AX
						
	           			MOV  AX,DATASEG  
						MOV  DS,AX	
	           
						MOV  SI, OFFSET DATABASE  ;EQUALENT: LEA SI DATABASE 
						MOV  DI, NUM
						SUB  DI, TYPE DATABASE    ;RIGHT 	BOUNDARY
						CALL QUICKSORT           
						CALL DISP
				        RET
	MAIN ENDP	
	QUICKSORT PROC NEAR
					    CMP  SI,DI
						JNL  END_QUICKSORT       ; CONTINUE WHEN LEFT BOUNDARY < RIGHT BOUNDRY
						PUSH DI                  ; develer right boundary parameter
						PUSH SI			 		 ; develer left  boundary parameter					 
						CALL QUICKPASS
      
						POP  DI             	 ; POP  divid BOUNDARY
						POP  SI		         	 ; POP  left  BOUNDARY
						PUSH DI                  ; PUSH divid BOUNDARY						
						SUB  DI, TYPE DATABASE   ; COPY LEFT-HALF MAX INDEX TO DI
						CMP  SI, DI
						JL   IF_TO
				    	JNL  ELSE_TO				
			    IF_TO:	
			    		CALL QUICKSORT           ; recursively call QuickSort 	
			    ELSE_TO:
			    		POP  SI 	             ; POP  divid BOUNDARY
						POP  DI                  ; POP  RIGHT BOUNDARY
						ADD  SI, TYPE DATABASE   ; COPY RIGHT-HALF MAX INDEX TO SI
						CMP  SI, DI
						JNL  END_QUICKSORT
						
						CALL QUICKSORT           ; recursively call QuickSort 
		END_QUICKSORT : 
						RET	         ; 43 Line
				
	QUICKSORT ENDP	

	QUICKPASS PROC NEAR 
						POP  BX    
						POP  SI
						POP  DI
						PUSH DI
						PUSH SI
						MOV  DX,WORD PTR[SI]   	; STORE FIRAT VALUE AS DIVID BOUNDARY

	    	LOOP_OUT :	
	    				CMP  SI,DI              ;50
						JNB  END_QUICKPASS	    ; CONTINUE WHEN LEFT BOUNDARY < RIGHT BOUNDRY

		    LOOP_IN1 :	
		    			CMP  SI,DI
						LAHF           		    ;LOAD FLAGS TO AH
						AND  AH,01H
						CMP  AH,01H
						JNE  IGNORE_LOOP_IN1	;57 Line			     
						CMP  DX,WORD PTR[DI]
						LAHF           		    ;LOAD FLAGS TO AH
						AND  AH,41H
						CMP  AH,0H  
						JZ   IGNORE_LOOP_IN1
						SUB  DI,TYPE DATABASE
						JMP  LOOP_IN1   

	   IGNORE_LOOP_IN1: 
	   					CMP  SI,DI
	   					JNL  LOOP_IN2             ;WHEN SI IS EQUAL TO DI ,SWAP DATA BETWWEN RAM IS MORE SLOWLY AND INVALID 
	   					MOV  AX, WORD PTR[DI]		
						MOV  [SI],AX

		      LOOP_IN2:	
		      			CMP  SI,DI
						LAHF           		      ;LOAD FLAGS TO AH
						AND  AH,01H
						CMP  AH,01H
						JNE  IGNORE_LOOP_IN2				     
						CMP  WORD PTR[SI],DX
						LAHF           		      ;LOAD FLAGS TO AH
						AND  AH,41H 
						CMP  AH,0H 
						JZ   IGNORE_LOOP_IN2
						ADD  SI,TYPE DATABASE
						JMP  LOOP_IN2
	   IGNORE_LOOP_IN2: 
	   					CMP  SI,DI                ;WHEN SI IS EQUAL TO DI ,SWAP DATA BETWWEN RAM IS MORE SLOWLY AND INVALID
	   					JNL  IGNORE_SWAP  
	   					MOV  AX,WORD PTR[SI] 
				        MOV  [DI],AX
		  IGNORE_SWAP:
						JMP  LOOP_OUT
		 END_QUICKPASS:	
		 				MOV  [SI],DX         	  ;FIRST VALUE STORED INTO DIVID ADRRESS			
					    PUSH SI	
					    PUSH BX		
					    RET
	QUICKPASS ENDP	
	DISP PROC NEAR
						MOV  BX, OFFSET DATABASE        ; INITIAL LOOP CONTROL VARIABLE
						
			LOOP_OUTER:
						CMP  BX, NUM
						JNB  END_OUTER_LOOP
						MOV  BP, SP
						MOV  AX, [BX]
			LOOP_IN:
						CMP  AX, 00H
						JE   LOOP_DISP
						MOV  DX, 0000H
						MOV  CX, 0AH 
						DIV  CX
						ADD  DL, 30H
						PUSH DX
						JMP LOOP_IN
						 
			LOOP_DISP:	
						CMP  SP,BP
						JNB  END_LOOP_DISP
						POP  DX
						MOV  AH, 02H
						INT  21H
						JMP  LOOP_DISP
		END_LOOP_DISP:
						MOV  DL, 20H         ;SPACE ASCII VALUE :20H
						MOV  AH, 02H
						INT  21H

						ADD  BX, TYPE DATABASE
						JMP  LOOP_OUTER
		END_OUTER_LOOP:			
						RET
	DISP ENDP
CODESEG ENDS
        END START

快速排序(用到内存辅助排序)

DATASEG SEGMENT
	DATABASE DW 9991H,9999H,9996H,9993H,9992H,9998H,9994H,9990H,9997H,8888H
	NUM      DW 0014H
DATASEG ENDS
STACKSEG SEGMENT 'STACK'
	DB 200H DUP(?)
STACKSEG ENDS
CODESEG SEGMENT
	ASSUME   CS : CODESEG, DS : DATASEG,SS : STACKSEG
	MAIN PROC FAR
	    START :		
	    				MOV  AX,STACKSEG
						MOV  SS,AX	
						MOV  SP,200H		
						
						PUSH DS
						XOR  AX,AX
						PUSH AX
						
	           			MOV  AX,DATASEG  
						MOV  DS,AX	

	           
						;MOV  SI, OFFSET DATABASE  ;EQUALENT: LEA SI DATABASE 
						;MOV  DI, NUM
						;SUB  DI, TYPE DATABASE    ;RIGHT 	BOUNDARY

						CALL  CALL_QUICK_SORT          
						CALL  DISP
				        RET
	MAIN ENDP 
	CALL_QUICK_SORT PROC NEAR
						MOV  SI, OFFSET DATABASE  ;EQUALENT: LEA SI DATABASE 
						MOV  DI, NUM
						SUB  DI, TYPE DATABASE    ;RIGHT 	BOUNDARY

						CALL QUICKSORT

						RET
	CALL_QUICK_SORT ENDP	
	QUICKSORT PROC NEAR
					    CMP  SI,DI
						JNL  END_QUICKSORT       ; CONTINUE WHEN LEFT BOUNDARY < RIGHT BOUNDRY
						PUSH DI                  ; develer right boundary parameter
						PUSH SI			 		 ; develer left  boundary parameter					 
						CALL QUICKPASS
      
						POP  DI             	 ; POP  divid BOUNDARY
						POP  SI		         	 ; POP  left  BOUNDARY
						PUSH DI                  ; PUSH divid BOUNDARY						
						SUB  DI, TYPE DATABASE   ; COPY LEFT-HALF MAX INDEX TO DI
						CMP  SI, DI
						JL   IF_TO
				    	JNL  ELSE_TO				
			    IF_TO:	
			    		CALL QUICKSORT           ; recursively call QuickSort 	
			    ELSE_TO:
			    		POP  SI 	             ; POP  divid BOUNDARY
						POP  DI                  ; POP  RIGHT BOUNDARY
						ADD  SI, TYPE DATABASE   ; COPY RIGHT-HALF MAX INDEX TO SI
						CMP  SI, DI
						JNL  END_QUICKSORT
						
						CALL QUICKSORT           ; recursively call QuickSort 
		END_QUICKSORT : 
						RET	         ; 43 Line
				
	QUICKSORT ENDP	

	QUICKPASS PROC NEAR 
						POP  BX    
						POP  SI
						POP  DI
						PUSH DI
						PUSH SI
						MOV  DX,WORD PTR[SI]   	; STORE FIRAT VALUE AS DIVID BOUNDARY

	    	LOOP_OUT :	
	    				CMP  SI,DI              ;50
						JNB  END_QUICKPASS	    ; CONTINUE WHEN LEFT BOUNDARY < RIGHT BOUNDRY

		    LOOP_IN1 :	
		    			CMP  SI,DI
						LAHF           		    ;LOAD FLAGS TO AH
						AND  AH,01H
						CMP  AH,01H
						JNE  IGNORE_LOOP_IN1	;57 Line			     
						CMP  DX,WORD PTR[DI]
						LAHF           		    ;LOAD FLAGS TO AH
						AND  AH,41H
						CMP  AH,0H  
						JZ   IGNORE_LOOP_IN1
						SUB  DI,TYPE DATABASE
						JMP  LOOP_IN1   

	   IGNORE_LOOP_IN1: 
	   					CMP  SI,DI
	   					JNL  LOOP_IN2             ;WHEN SI IS EQUAL TO DI ,SWAP DATA BETWWEN RAM IS MORE SLOWLY AND INVALID 
	   					MOV  AX, WORD PTR[DI]		
						MOV  [SI],AX

		      LOOP_IN2:	
		      			CMP  SI,DI
						LAHF           		      ;LOAD FLAGS TO AH
						AND  AH,01H
						CMP  AH,01H
						JNE  IGNORE_LOOP_IN2				     
						CMP  WORD PTR[SI],DX
						LAHF           		      ;LOAD FLAGS TO AH
						AND  AH,41H 
						CMP  AH,0H 
						JZ   IGNORE_LOOP_IN2
						ADD  SI,TYPE DATABASE
						JMP  LOOP_IN2
	   IGNORE_LOOP_IN2: 
	   					CMP  SI,DI                ;WHEN SI IS EQUAL TO DI ,SWAP DATA BETWWEN RAM IS MORE SLOWLY AND INVALID
	   					JNL  IGNORE_SWAP  
	   					MOV  AX,WORD PTR[SI] 
				        MOV  [DI],AX
		  IGNORE_SWAP:
						JMP  LOOP_OUT
		 END_QUICKPASS:	
		 				MOV  [SI],DX         	  ;FIRST VALUE STORED INTO DIVID ADRRESS			
					    PUSH SI	
					    PUSH BX		
					    RET
	QUICKPASS ENDP	
	DISP PROC NEAR
						MOV  BX, OFFSET DATABASE        ; INITIAL LOOP CONTROL VARIABLE
						
			LOOP_OUTER:
						CMP  BX, NUM
						JNB  END_OUTER_LOOP
						MOV  BP, SP
						MOV  AX, [BX]
			LOOP_IN:
						CMP  AX, 00H
						JE   LOOP_DISP
						MOV  DX, 0000H
						MOV  CX, 0AH 
						DIV  CX
						ADD  DL, 30H
						PUSH DX
						JMP LOOP_IN
						 
			LOOP_DISP:	
						CMP  SP,BP
						JNB  END_LOOP_DISP
						POP  DX
						MOV  AH, 02H
						INT  21H
						JMP  LOOP_DISP
		END_LOOP_DISP:
						MOV  DL, 20H         ;SPACE ASCII VALUE :20H
						MOV  AH, 02H
						INT  21H

						ADD  BX, TYPE DATABASE
						JMP  LOOP_OUTER
		END_OUTER_LOOP:			
						RET
	DISP ENDP
CODESEG ENDS
        END START



你可能感兴趣的:(8086汇编)