首页 > 代码库 > 汇编-排序实验

汇编-排序实验

一、实习目的

    握用汇编语言编写程序的思路和方法。

二、实习内容

    从首地址为1000H开始存放10个数,要求设计程序将这些数由小到大排序,排序后的数仍放在原地址处。

三、代码

data segment 
   count   db  10;
   array   db  12,34,56,21,234,54,45,58,96,14;
   msgOne  db  'The source array number is:','$'
   msgTwo  db  0ah,0dh,'The sorted array number is:','$'
   c10     db  10;
data ends

myStack segment stack
           db  100 dup(?)
myStack ends

code segment
  assume DS:data,CS:code
start:
  mov ax,data;
  mov DS,ax;

  lea dx,msgOne;          输出排序前的数组
  mov ah,09h;
  int 21h;
  mov ch,0;
  mov cl,count;
  mov bx,0;
outPutSource:
  mov ah,0;
  mov al,array[bx];
  call printNum;
  
  mov dl,' '
  mov ah,02h;
  int 21h;
  
  inc bx;
  loop outPutSource

  lea si,array;        设置参数,调用排序函数
  mov ch,0;
  mov cl,count;
  call mySort;
  
  lea dx,msgTwo;        输出排序后的数组
  mov ah,09h;
  int 21h;
  mov ch,0;
  mov cl,count;
  mov bx,0;
outPutSortedArray:
  mov ah,0;
  mov al,array[bx];
  call printNum;
  
  mov dl,' '
  mov ah,02h;
  int 21h;
  
  inc bx;
  loop outPutSortedArray
  
  mov ax,4c00h;
  int 21h;

mySort proc;         数组排序,si中存放数组偏移地址,cx中存放数组元素个数  push ax;
  push cx;
  dec cx;
  mov ax,0;
loopOne:
  push cx;
  push si;
  
    loopTwo:
      mov al,byte ptr[si];
      cmp al,byte ptr[si + 1];
      jbe next;
      
      xchg al,[si + 1];  
      xchg al,[si];
 
    next:   
      inc si;
    loop loopTwo
  
  pop si;
  pop cx;
  loop loopOne
  
  pop cx;
  pop ax;
  ret

printNum proc;       将al中存放的数字以十进制形式输出
  push ax;
  push dx;
  push cx;

  mov cx,0;
tranToChar:
  mov ah,0;
  cmp al,0;
  jz  outTranToChar;
  
  div c10;
  mov dl,ah;
  mov dh,0;
  push dx;
  
  inc cx;
  jmp  tranToChar;
  
outTranToChar:
  cmp cx,0;
  jz exit_printNum
  
print: 
  pop dx;
  add dx,30h;
  mov ah,02h;
  int 21h;
  loop print
  
exit_printNum:
  pop cx;
  pop dx;
  pop ax;
  ret;
  

code ends
end start


四、运行结果



汇编-排序实验