如何使用汇编语言实现冒泡排序

2025-05-01 17:12:54
推荐回答(2个)
回答1:

内存以BUF单元开始有若干单字节有符号数,编程实现从大到小排序并以十进制输出。

code  segment
      assume cs:code
      org 100h
start:jmp begin
buf   db 3,4,5,0,-1,-2,-3
n     dw $-buf
begin:push cs
      pop ds
      push cs
      pop es
      lea si,buf
      mov cx,n
      call sort
      mov cx,n
      lea si,buf
@1:
      lodsb
      cbw
      call dispaxs
      loop  @1
       
      mov ah,4ch
      int 21h
;=============================================
; 字节数组排序(冒泡排序)
sort  proc near
      ; 数组元素个数置入cx,首地址置入 si
      push ax
      push cx
      push dx
      push si
      push di
      pushf
      push cx
      pop dx
      dec dx
@sortl1:
      mov cx,dx
      mov di,si
@sortl2:
      mov al,[di+1]
      cmp al,[di]
      jl @sortnext   ; 无符号数排序改为 jb
      xchg al,[di]
      mov [di+1],al
@sortnext:
      inc di
      loop @sortl2
      dec dx
      jnz @sortl1
      popf
      pop di
      pop si
      pop dx
      pop cx
      pop ax
      ret
sort  endp
;====================================================
 ; 将要显示的有符号数置于 ax 中
     DISPAXS  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSH      BP
              PUSH      DS
              PUSH      ES
              PUSHF
              PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              MOV       CX,6
              LEA       DI,DISPAXSS
   @DISPAXS:
              MOV       BYTE PTR [DI],32
              INC       DI
              LOOP      @DISPAXS
              PUSH      AX
              MOV       DL,32
              MOV       AH,2
              INT       21H
              POP       AX
              MOV       BYTE PTR NZS,0
              MOV       BYTE PTR SIGNS,0
              CMP       AX,0
              JGE       @DISPAXS0
              MOV       BYTE PTR SIGNS,1
              NEG       AX
  @DISPAXS0:
              PUSH      AX
              LEA       SI,DIVARRS
              LEA       DI,DISPAXSS
              INC       DI
              MOV       CX,5
  @DISPAXS1:
              POP       AX
              MOV       DX,0
              MOV       BX,[SI]
              DIV       BX
              PUSH      DX
              CMP       AL,0
              JNE       @DISPAXS2
              CMP       BYTE PTR NZS,1
              JE        @DISPAXS2
              CMP       CX,1
              JE        @DISPAXS2
              MOV       DL,20H
              JMP       @DISPAXS3
  @DISPAXS2:
              ADD       AL,30H
              MOV       DL,AL
              MOV       BYTE PTR NZS,1
  @DISPAXS3:
              MOV       BYTE PTR[DI],DL
              INC       SI
              INC       SI
              INC       DI
              LOOP      @DISPAXS1
              POP       DX
 
              CMP       BYTE PTR SIGNS,1
              JNE       @DISPAXS6
              LEA       SI,DISPAXSS
              ADD       SI,5
  @DISPAXS4:
              CMP       BYTE PTR [SI],32
              JE        @DISPAXS5
              DEC       SI
              JMP       @DISPAXS4
  @DISPAXS5:
              MOV       BYTE PTR [SI],'-'
  @DISPAXS6:
              LEA       DX,DISPAXSS
              MOV       AH,9
              INT       21H
              POPF
              POP       ES
              POP       DS
              POP       BP
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
     DIVARRS  DW        10000,1000,100,10,1
         NZS  DB        0
       SIGNS  DB        0
    DISPAXSS  DB        32,32,32,32,32,32,'$'
     DISPAXS  ENDP
;================================================
code  ends
      end start

回答2:

用什么汇编语言,指定先。