汇编编写冒泡排序

  关于汇编冒泡排序法。

  小编在编的时候,因为定义的是字节类型的数据,MOV数据的时候却用的AX,导致发生错误(好久都没找出来哪里的问题)。

键盘输入十个十六进制字节数据,存入给定的存储单元中,用冒泡排序法将数据按从大到小的顺序排列好,在屏幕上显示出来.

 这里,我们采用两个字节分别存储16进制的两位,以空格间隔每个数据。

DATA SEGMENT
    DATA1 DB 40,0,40 DUP(0)
    COUNT EQU 10
    STR DB 0DH,0AH,"Please input 10 number:",0DH,0AH,"$"
    RANKED DB 0DH,0AH,'Ordered numbers:',0dh,0ah,"$"
DATA ENDS

CODE SEGMENT
    ASSUME DS:DATA,CS:CODE
    START:MOV AX,DATA
    MOV DS,AX
    
    
    CALL INPUT
    CALL RANK
    CALL DISPLAY
    MOV AH,4CH
    INT 21H 

INPUT PROC
    MOV SI,0
    MOV AH,09H
    MOV DX,OFFSET STR
    INT 21H
    MOV AH,0AH
    MOV DX,OFFSET DATA1
    INT 21H
    RET
INPUT ENDP

RANK PROC
    MOV CX,COUNT
 S1:PUSH CX
    MOV SI,0
    MOV BX,OFFSET DATA1
    ADD BX,2
    MOV CX,COUNT-1
 S2:MOV DL,[BX+SI]
    CMP DL,[BX+SI+3]
    JC EXCH
    JNZ  S3
    MOV DL,[BX+SI+1]
    CMP DL,[BX+SI+4]
    JNC S3
EXCH:MOV DL,[BX+SI]
    XCHG DL,[BX+SI+3]
    MOV [BX+SI],DL
    MOV DL,[BX+SI+1]
    XCHG DL,[BX+SI+4]
    MOV [BX+SI+1],DL
   S3: NOP 
   ADD SI,3
    LOOP S2
    POP CX
    LOOP S1
    RET
RANK ENDP    
 
DISPLAY PROC
    LEA DX,RANKED
    MOV AH,09H
    INT 21H
    LEA DX,DATA1
    MOV CL,DATA1+1
    XOR CH,CH
    ADD DX,CX
    MOV BX,DX
    MOV BYTE PTR[BX+2],"$"
    LEA DX,DATA1
    ADD DX,2
    INT 21H
    RET
DISPLAY ENDP      
    
CODE ENDS
END START

 

 

 

已知数据为:  41H, 70H, 03H, 53H, 88H, 16H, 8EH, 3FH,用冒泡排序法将数据按从小到大的顺序排列好,再由键盘键入75H, 将它插入这组数据中,并保持从小到大的顺序不变. 将插入数据后的数组在屏幕上显示出来.

  小编对上面的程序做了些修改。插入过程的入口参数为COUNT,需要重新定义。

  根据书上面的用“=”伪指令定义的常量,可以重复赋值,但是小编在程序中,并未实现~~~

  无奈,哭了,在emu8086运行=伪指令不起作用.

  小编想的是,可u哦手动输入8个数据进行排序,然后再输入另一个值插入排序。但是emu8086‘=’赋值只针对第一次起作用,而dosbox只针对最后一次起作用。

所以dosbox运行会产生第一个数据为00的现象。(以下代码 理论上应该没有问题的)

  但是运行结果不对!!!!!

DATA SEGMENT
DATA1 DB 40,0,40 DUP(0)
STR DB 0DH,0AH,"Please input 8 number:",0DH,0AH,"$"
RANKED DB 0DH,0AH,'Ordered numbers:',0dh,0ah,"$"
INSERT DB 0DH,0AH,"Please input a number you want to insert:",0DH,0AH,"$"
DATA2 DB 10,0,10 DUP(0)
COUNT=8
DATA ENDS

CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:MOV AX,DATA
MOV DS,AX

CALL INPUT
MOV CX,COUNT
CALL RANK
CALL DISPLAY
CALL INSERTDA
COUNT=COUNT+1
MOV CX,COUNT
CALL RANK
CALL DISPLAY
MOV AH,4CH
INT 21H

INPUT PROC
MOV SI,0
MOV AH,09H
MOV DX,OFFSET STR
INT 21H
MOV AH,0AH
MOV DX,OFFSET DATA1
INT 21H
RET
INPUT ENDP

RANK PROC
MOV AH,02H
MOV DL,COUNT
ADD DL,30H
INT 21H
S1: PUSH CX
MOV SI,0
MOV BX,OFFSET DATA1
ADD BX,2
MOV CX,COUNT-1
S2:MOV DL,[BX+SI]
CMP DL,[BX+SI+3]
JA EXCH
JNZ S3
MOV DL,[BX+SI+1]
CMP DL,[BX+SI+4]
JC S3
EXCH:MOV DL,[BX+SI]
XCHG DL,[BX+SI+3]
MOV [BX+SI],DL
MOV DL,[BX+SI+1]
XCHG DL,[BX+SI+4]
MOV [BX+SI+1],DL
S3: NOP
ADD SI,3
LOOP S2
POP CX
LOOP S1
RET
RANK ENDP

DISPLAY PROC
LEA DX,RANKED
MOV AH,09H
INT 21H
LEA DX,DATA1
MOV CL,DATA1+1
XOR CH,CH
ADD DX,CX
MOV BX,DX
MOV BYTE PTR[BX+2],"$"
LEA DX,DATA1
ADD DX,2
INT 21H
RET
DISPLAY ENDP

INSERTDA PROC
PUSH CX
PUSH DX
MOV AH,09H
LEA DX,INSERT
INT 21H
MOV AH,0AH
LEA DX,DATA2
INT 21H
MOV DX,OFFSET DATA1
MOV CL,DATA1+1
XOR CH,CH
ADD DX,CX
MOV BX,DX
LEA DX,DATA2
MOV SI,OFFSET DATA2
MOV BYTE PTR[BX+2],20H
MOV CL,[SI+2]
MOV [BX+3],CL
MOV CL,[SI+3]
MOV [BX+4],CL
MOV DX,OFFSET DATA1
MOV BX,DX
ADD BYTE PTR[BX+1],3
POP DX
POP CX
RET
INSERTDA ENDP


CODE ENDS
END START

 

  只能采用两个过程调用了。RANK1和RANK2

  小编至此还是不知道原因在哪里,

本来打算用一个过程完成两次不同调用,失败了,所以还是用两个过程好了。

DATA SEGMENT
    DATA1 DB 40,0,40 DUP(0)
    STR DB 0DH,0AH,"Please input 8 number:",0DH,0AH,"$"
    RANKED DB 0DH,0AH,'Ordered numbers:',0dh,0ah,"$"
    INSERT DB 0DH,0AH,"Please input a number you want to insert:",0DH,0AH,"$"
    DATA2 DB 10,0,10 DUP(0)
DATA ENDS

CODE SEGMENT
    ASSUME DS:DATA,CS:CODE
    START:MOV AX,DATA
    MOV DS,AX
    
    CALL INPUT
    CALL RANK1
    CALL DISPLAY   
    CALL INSERTDA
    MOV AH,4CH
    INT 21H 

INPUT PROC
    MOV SI,0
    MOV AH,09H
    MOV DX,OFFSET STR
    INT 21H
    MOV AH,0AH
    MOV DX,OFFSET DATA1
    INT 21H
    RET
INPUT ENDP

RANK1 PROC 
     MOV CX,8  
 S1: PUSH CX
    MOV SI,0
    MOV BX,OFFSET DATA1
    ADD BX,2
    MOV CX,7
 S2:MOV DL,[BX+SI]
    CMP DL,[BX+SI+3]
    JA EXCH
    JNZ  S3
    MOV DL,[BX+SI+1]
    CMP DL,[BX+SI+4]
    JC S3
EXCH:MOV DL,[BX+SI]
    XCHG DL,[BX+SI+3]
    MOV [BX+SI],DL
    MOV DL,[BX+SI+1]
    XCHG DL,[BX+SI+4]
    MOV [BX+SI+1],DL
   S3: NOP 
   ADD SI,3
    LOOP S2
    POP CX
    LOOP S1
    RET
RANK1 ENDP
RANK2 PROC 
     MOV CX,9 
 S6: PUSH CX
    MOV SI,0
    MOV BX,OFFSET DATA1
    ADD BX,2
    MOV CX,8
 S4:MOV DL,[BX+SI]
    CMP DL,[BX+SI+3]
    JA EXCH1
    JNZ  S5
    MOV DL,[BX+SI+1]
    CMP DL,[BX+SI+4]
    JC S5
EXCH1:MOV DL,[BX+SI]
    XCHG DL,[BX+SI+3]
    MOV [BX+SI],DL
    MOV DL,[BX+SI+1]
    XCHG DL,[BX+SI+4]
    MOV [BX+SI+1],DL
   S5: NOP 
   ADD SI,3
    LOOP S4
    POP CX
    LOOP S6
    RET
RANK2 ENDP    
 
DISPLAY PROC
    LEA DX,RANKED
    MOV AH,09H
    INT 21H
    LEA DX,DATA1
    MOV CL,DATA1+1
    XOR CH,CH
    ADD DX,CX
    MOV BX,DX
    MOV BYTE PTR[BX+2],"$"
    LEA DX,DATA1
    ADD DX,2
    INT 21H
    RET
DISPLAY ENDP      

INSERTDA PROC
      PUSH CX
      PUSH DX 
      MOV AH,09H
      LEA DX,INSERT
      INT 21H
      MOV AH,0AH
      LEA DX,DATA2
      INT 21H
      MOV DX,OFFSET DATA1
      MOV CL,DATA1+1
      XOR CH,CH
      ADD DX,CX
      MOV BX,DX
      LEA DX,DATA2
      MOV SI,OFFSET DATA2
      MOV BYTE PTR[BX+2],20H
      MOV CL,[SI+2]
      MOV [BX+3],CL
      MOV CL,[SI+3]
      MOV [BX+4],CL
      MOV DX,OFFSET DATA1
      MOV BX,DX
      ADD BYTE PTR[BX+1],3
      POP DX
      POP CX
      CALL RANK2
      CALL DISPLAY
      RET
INSERTDA ENDP

 

 

  

posted @ 2020-05-28 10:26  为红颜  阅读(392)  评论(5编辑  收藏