汇编排序,输入一个整数N,接下来输入N个整数,进行排序,用汇编语言

输入一个整数N,接下来输入N个整数,进行排序,用汇编语言... 输入一个整数N,接下来输入N个整数,进行排序,用汇编语言 展开
 我来答
永恒pascal
2018-05-14 · TA获得超过4357个赞
知道大有可为答主
回答量:3902
采纳率:93%
帮助的人:671万
展开全部



        CODE  SEGMENT
              ASSUME    CS:CODE
      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              
              CALL      INPUTNUM
              LEA       DI,@ARRAY
              ADD       DI,2        ; 从第2个数开始
              MOV       CX,@NUM
              DEC       CX          ; 个数减一
              CALL      SELECTSORT
              
              MOV       AH,2
              MOV       DL,13
              INT       21H
              MOV       DL,10
              INT       21H
              
              LEA       SI,@ARRAY
              ADD       SI,2
              MOV       CX,@NUM
              DEC       CX
              CMP       CX,0
              JZ        @EXIT
              CLD
         @1:
              LODSW
              CALL      DSPAXS
              LOOP      @1

      @EXIT:
              MOV       AH,4CH
              INT       21H

;===============================================================
;  键盘输入数值数组子程序(数组名为@array,元素个数存放在@num中)
;  可以是负数
;  采用字符串缓冲输入,避免原来键入后不能回退修改的问题 ! 
    INPUTNUM  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSHF
      ; 对数组@array清零
              MOV       CX,100
              MOV       AX,0
              LEA       DI,@ARRAY
              REP       STOSW
              MOV       WORD PTR @NUM,0
      ; 输入的数据以一个空格分隔,以回车符结束输入
              LEA       DX,@STRA
              MOV       AH,10
              INT       21H
              MOV       CL,@STRN
              MOV       CH,0
              MOV       WORD PTR @STRM,CX
              LEA       SI,@STR
              CLD
              
              LEA       DI,@ARRAY
              MOV       WORD PTR @NUM,0
      @STIN:
              MOV       AX,0
              PUSH      AX
              MOV       BYTE PTR @SIGNN,0
    @AGAIN1:
              CMP       WORD PTR @STRM,0
              JE        @LINE0
              LODSB
              DEC       WORD PTR @STRM
              CMP       AL,' '
              JE        @LINE0
              CMP       AL,'-'
              JNE       @INPUT1
              MOV       BYTE PTR @SIGNN,1
              JMP       @AGAIN1
    @INPUT1:
              SUB       AL,30H
              MOV       AH,0
              MOV       BX,AX
              POP       AX
              MOV       CL,10
              MOV       CH,0
              MUL       CX
              ADD       AX,BX
              PUSH      AX
              JMP       @AGAIN1
     @LINE0:
              POP       AX
              CMP       BYTE PTR @SIGNN,1
              JNE       @INPUT2
              NEG       AX
    @INPUT2:
              MOV       WORD PTR[DI],AX
              INC       WORD PTR @NUM
              CMP       WORD PTR @STRM,0
              JE        @STINEND
              INC       DI
              INC       DI
              JMP       @STIN
   @STINEND:
              POPF
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       @STRA  DB        255
       @STRN  DB        ?
        @STR  DB        255 DUP(?)
       @STRM  DW        0
      @ARRAY  DW        100 DUP(0)
        @NUM  DW        0
       @CHAR  DB        ?
      @SIGNN  DB        0
    INPUTNUM  ENDP

;=========================================
  SELECTSORT  PROC      NEAR
              ; 数组起始地址 存入 di,元素个数 存入 cx  
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSH      SI
              PUSH      DI
              PUSHF
              CMP       CX,1
              JE        @@SELE5
              DEC       CX          ; ################
              MOV       BX,0
    @@SELE1:
              PUSH      CX
              MOV       SI,DI
              INC       SI
              INC       SI          ; {SI=DI+2}
              PUSH      DI          ;{保存DI }
              MOV       AX,[DI][BX] ; {假设MAX 为第一个}
    @@SELE2:  MOV       DX,[SI][BX] ; {存储器到寄存器,节省时间}
              CMP       AX,DX
              JGE       @@SELE3     ; 从大到小,有符号数排序   #################
              MOV       DI,SI       ; {小于时,将SI替换DI,即将J替换I, 保存MAXI}
              MOV       AX,DX       ; {保存MAX 到AX}
    @@SELE3:  INC       SI
              INC       SI
              LOOP      @@SELE2
              MOV       SI,DI       ; {还原SI,即将I替换J}
              POP       DI          ; {取出栈中保存的I}
              CMP       DI,SI       ; {与J 比较}
              JE        @@SELE4     ;  {相同时不交换}
              MOV       AX,[DI][BX] ; {在内循环之外交换}
              XCHG      AX,[SI][BX]
              MOV       [DI][BX],AX
    @@SELE4:  INC       BX
              INC       BX
              POP       CX
              LOOP      @@SELE1
    @@SELE5:
              POPF
              POP       DI
              POP       SI
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
  SELECTSORT  ENDP
;==================================
; 输出有符号数,输出的数应放 ax中 
      DSPAXS  PROC      NEAR
              PUSH      AX
              OR        AX,AX
              JNS       @DSPAXS
              NEG       AX
              PUSH      AX
              PUSH      DX
              MOV       AH,2
              MOV       DL,'-'
              INT       21H
              POP       DX
              POP       AX
    @DSPAXS:
              CALL      DSPAX
              POP       AX
              RET
      DSPAXS  ENDP
     
;===============================
       DSPAX  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSHF
              XOR       CX,CX
              MOV       BX,10
    @DSPAX1:
              XOR       DX,DX
              DIV       BX
              INC       CX
              OR        DX,30H
              PUSH      DX
              CMP       AX,0
              JNE       @DSPAX1
              MOV       AH,2
   @DISPAX2:
              POP       DX
              INT       21H
              LOOP      @DISPAX2
              MOV       DL,32
              INT       21H
              POPF
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       DSPAX  ENDP
;================================  
 
        CODE  ENDS
              END       START
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式