用有符号数实现排序的汇编语言程序

用有符号数实现排序的汇编语言程序,第1张

设有三个字序无符橡首号数存放在buffer缓冲姿氏器中,现要对其按从大到小的排列

程序如下:

dseg segment

buffer db 78,90,11 假设的三个无符梁册数号数

dseg ends

cseg segment

assume cs:cseg,ds:dseg

start: mov ax,dseg

mov ds,ax

mov si,offset buffer

mov al,[si]

cmp al,[si+1] 第一个数与第二个数比较

jbe next1

xchg al,[si+1]

mov [si],al

next1: mov al,[si]

cmp al,[si+2] 第一个数与第三个数比较

jbe next2

xchg al,[si+2]

mov [si],al

next2: mov al,[si+1]

cmp al,[si+2] 第三个数与第三个数比较

jbe next3

xchg al,[si+2]

mov [si+1],al

next3: mov ah,4ch

int 21h

cseg ends

end start

        CODE  SEGMENT

              ASSUME    CS:CODE

              ORG       100H

      START:  JMP       BEGIN

        ARR1  DB        3AH,80H,0BH,55H,0FFH

         NUM  EQU       $-ARR1

      BEGIN:  PUSH      CS

              POP       DS

              PUSH      CS

              POP       ES

              LEA       SI,ARR1

              MOV       CX,NUM

              CALL      SORTBYTE

              LEA       SI,ARR1

              MOV       CX,NUM

         @0:

              LODSB

              CBW

              CALL      DISPAXS

              LOOP      @0

              MOV       AH,4CH

              INT       21H

 

===============================================

 字节数组排序(有符号数冒泡排序)

    SORTBYTE  PROC      NEAR

       数组长度置入cx,数组首地址置入 si

              PUSH      AX

              PUSH      CX

              PUSH      DX

              PUSH      SI

              PUSH      DI

              PUSHF

              PUSH      CX

              POP       DX

              DEC       裤好DX

   @SORTBL1:

              MOV       CX,DX

              MOV       DI,SI

   @SORTBL2:

              MOV       AL,[DI+1]

              CMP       AL,[DI]

              JL        @SORTBNEXT   从大到小(无符号换成 JB)

              XCHG      AL,[DI]

              MOV       [DI+1],AL

 @SORTBNEXT:

              INC       DI

              LOOP      @SORTBL2

              DEC       DX

              JNZ       @SORTBL1

              POPF

              POP       DI

              POP       SI

              POP       DX

              POP       CX

              POP       AX

              RET

    SORTBYTE  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

data segment

num dw -28971

decasc db 6 dup('0')

ends

stack segment para stack

dw 128 dup(0)

ends

code segment

start:

mov ax, data

mov ds, ax

mov bx,num

and bx,bx

jns next1判断正负

mov decasc,'-'

neg bx 是负数,则求其相反数

next1:mov si,5 将二进制数转换成十进制数的ASCII码

mov ax,bx

mov cx,5

mov di,10

next2:xor dx,dx

div di

or dl,30h

mov decasc[si],dl

dec si

loop next2

mov cx,5

mov si,0

mov dl,decasc[si]

cmp dl,'-' 若是负数,则显示负号,正数不显示符号

jne next3

mov ah,02h

int 21h

next3: 跳过前导0

inc si

mov dl,decasc[si]

cmp dl,'0'

jne next4

loop next3

next4:mov ah,02h显示有效数字,若BX为0,则只显示0

int 21h

inc si

mov dl,decasc[si]

jcxz next5

loop next4

next5:

mov ax, 4c00h exit to operating system.

int 21h

ends

end start


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12549979.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存