微机原理与汇编语言--输入五个数存入内存排序输出
一个是初始版,后一个是升级版
🎈🎈🎈做题步骤:
先输入五个数存入内存中,再在内存中使用冒泡排序法排好序,再输出这五个数
;输入五个数存入到内存,再排序输出
DATAS SEGMENT
;此处输入数据段代码
a1 word 2
a2 word 5 dup(?);这是存五个元素
DATAS ENDS
STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
;此处输入代码段代码
mov di,0
mov si,offset a2
jmp m1
m1:
cmp di,5
je ToPaixu;先去排序
call input
mov [si],bx
add si,2
add di,1
jmp m1
m3:
mov di,0
mov si,offset a2
jmp m2
m2:
cmp di,5
je over
mov ax,[si]
call output
add si,2
add di,1
jmp m2
ToPaixu:
call paixu
jmp m3
over:
MOV AH,4CH
INT 21H
paixu proc;排序函数
push ax
push bx
push cx
push dx
mov al,0;遍历元素1
pp1:
inc al
mov si,offset a2
mov cl,6
cmp al,5
je pover
mov dh,0;遍历元素2
sub cl,al
sub si,2
pp2:
add si,2
inc dh
cmp dh,cl
je pp1
mov bl,[si]
mov bh,[si+2]
cmp bl,bh
ja swap
jmp pp2
swap:
mov [si+2],bl
mov [si],bh
jmp pp2
pover:
pop ax
pop bx
pop cx
pop dx
ret
paixu endp
output proc;开始第一个函数
push bx
push cx
push dx
mov ch,0
mov cl,10
div cl
cmp al,0
ja o1
je o2
o1:
push ax
add ch,1
mov ah,0
div cl
cmp al,0
ja o1
je o2
o2:
mov dl,ah
add dl,48
mov ah,2
int 21h
cmp ch,0
ja o3
je o4
o3:
pop ax
sub ch,1
jmp o2
o4:
pop bx
pop cx
pop dx
ret ;返回
output endp;结束
input proc;第二个函数
push ax
push cx
push dx
mov bx,0
mov dh,10
jmp i0
i0:
mov ah,1
int 21h
sub al,48
cmp al,0
jb i8
je i2
i2:
cmp al,9
ja i8
jb i3
i3:
mov cl,al
mov ch,0
mov ax,bx
mul dh
add cx,ax
mov bx,cx
jmp i0
i8:
pop ax
pop cx
pop dx
ret
input endp
CODES ENDS
END START
2020.10.14更新
受朋友‘启发’,为了使程序更简洁,将不同的代码封装
DATAS SEGMENT
;此处输入数据段代码
a1 word 5 dup(?);这是存五个元素
a2 byte 'Please input five number:!'
a3 byte 'The number before sorting is:!'
a4 byte 'The number after sorting is:!'
DATAS ENDS
STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
;此处输入代码段代码
;先输出字符串a2
mov si,offset a2
call outputstr
;输入五个数
mov si,offset a1
call inputfn
call wrap;换行
;再输出字符串a3
mov si,offset a3
call outputstr
;输出原顺序
mov si,offset a1
call outputfn
call wrap
;排序
mov si,offset a1
call paixu
;输出字符串a4
mov si,offset a4
call outputstr
;输出排序后的
mov si,offset a1
call outputfn
MOV AH,4CH
INT 21H
inputfn proc;输入五个数
push cx
mov cl,0
ifn1:
cmp cl,5
je ifnover
call input
mov [si],bx
add si,2
add cl,1
jmp ifn1
ifnover:
pop cx
ret
inputfn endp
input proc;输入函数
push ax
push cx
push dx
mov bx,0
mov dh,10
jmp i0
i0:
mov ah,1
int 21h
sub al,48
cmp al,0
jb i8
je i2
i2:
cmp al,9
ja i8
jb i3
i3:
mov cl,al
mov ch,0
mov ax,bx
mul dh
add cx,ax
mov bx,cx
jmp i0
i8:
pop ax
pop cx
pop dx
ret
input endp
outputfn proc;输出五个数
push bx
push ax
mov di,0
ofn1:
cmp di,5
je ofnover
mov ax,[si]
call output
add si,2
add di,1
jmp ofn1
ofnover:
pop bx
pop ax
ret
outputfn endp
output proc;输出函数
push bx
push cx
push dx
mov bh,0
mov cl,10
div cl
cmp al,0
ja o1
je o2
o1:
push ax
add bh,1
mov ah,0
div cl
cmp al,0
ja o1
je o2
o2:
mov dl,ah
add dl,48
mov ah,2
int 21h
cmp bh,0
ja o3
je o4
o3:
pop ax
sub bh,1
jmp o2
o4:
pop bx
pop cx
pop dx
ret ;返回
output endp;结束
paixu proc;排序函数
push ax
push bx
push cx
push dx
mov di,si;存si方便封装
add si,5
mov al,0;遍历元素1
pp1:
inc al
mov si,di
mov cl,6
cmp al,5
je pover
mov dh,0;遍历元素2
sub cl,al
sub si,2
pp2:
add si,2
inc dh
cmp dh,cl
je pp1
mov bl,[si]
mov bh,[si+2]
cmp bl,bh
ja swap
jmp pp2
swap:
mov [si+2],bl
mov [si],bh
jmp pp2
pover:
pop ax
pop bx
pop cx
pop dx
ret
paixu endp
outputstr proc;字符串输出函数
push dx
oscontinue:
mov dl,[si]
cmp dl,'!'
je osover
mov ah,2
int 21h
add si,1
jmp oscontinue
osover:
pop dx
ret
outputstr endp
wrap proc;换行函数
push dx
MOV DX,13
MOV AH,2
INT 21H
MOV DX,10
MOV AH,2
INT 21H
pop dx
ret
wrap endp
CODES ENDS
END START
运行效果: