参考思路如下:
首先对若个数按从小到大排序,然后第一个元素就是最小的,最后一个元素就是最大的 ;
参考代码如下:
data segment
mes1 db 'the max:$'
mes2 db 'the min:$'
a dw 42,20,100,456,99,120,89,12334,90,1239
data ends
code segment
main proc far
assume cs:code,ds:data
start:
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
;以下是起泡排序核心代码
mov cx,10
dec cx
loop1:
mov di,cx
mov bx,0
loop2:
mov ax,a[bx]
cmp a[bx+2],ax
jge cotinue
xchg ax,a[bx+2]
mov a[bx],ax
cotinue:
add bx,2
loop loop2
mov cx,di
loop loop1
mov dx,offset mes1
mov ah,09h
int 21h
call crlf
mov bx,a[18]
call bini ;转换为十进制的子程序
call crlf ;回车换行子程序
mov dx,offset mes2
mov ah,09h
int 21h
call crlf
mov bx,a[0]
call bini
ret
main endp
bini proc near
mov cx,10000d
call bin
mov cx,1000d
call bin
mov cx,100d
call bin
mov cx,10d
call bin
mov cx,1d
call bin
ret
bini endp
bin proc near
mov ax,bx
mov dx,0
div cx
mov bx,dx
mov dl,al
add dl,30h
mov ah,02h
int 21h
ret
bin endp
crlf proc near
mov dl,0ah
mov ah,02h
int 21h
mov dl,0dh
mov ah,02h
int 21
ret
crlf endp
code ends
end start
DATAS SEGMENT
s db '1234567890'
s1 db 'MAX: '
max db 0,'$'
s2 db 'MIN: '
min db 0,'$'
DATAS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS
START:
MOV AX,DATAS
MOV DS,AX
mov si,offset s ;初始化si,cx
mov cx,9 ;cx为定义的个数减一
mov bh,[si] ;第一个字符放入bh,bl
mov bl,bh
ss1: ;开始比较
inc si
mov al,[si]
cmp al,bh
jb ss2
mov bh,al ;al>bh 写入bh
jmp ss3
ss2:
cmp al,bl
ja ss3
mov bl,al ;al
loop ss1
mov max,bh ;写入最大值
mov min,bl ;写入最小值
mov dx,offset s1 ;显示最大值
mov ah,9
int 21h
mov dl,' ' ;显示空格
mov ah,2
int 21h
mov dx,offset s2 ;显示最小值
mov ah,9
int 21h
mov ah,0 ;等待键盘输入
int 16h
MOV AH,4CH
INT 21H
CODES ENDS
END START
STACK1 SEGMENT STACK
DW 64 DUP(?)
STACK1 ENDS
CODE SEGMENT
ASSUME CS:CODE
START: MOV SI,3000H
MOV CX,0008H
CALL branch
INT 03H
branch: JCXZ A4
PUSH SI
PUSH CX
PUSH BX
MOV BH,[SI]
MOV BL,BH
A1: LODSB
CMP AL,BH
JBE A2
MOV BH,AL
JMP A3
A2: CMP AL,BL
JAE A3
MOV BL,AL
A3: LOOP A1
MOV AX,BX
POP BX
POP CX
POP SI
A4: RET
CODE ENDS
END START
;从键盘接收任意字符。
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
MSG1 DB 'Please input some chars: $'
MSG2 DB 13, 10, 'The biggest char is: <'
BIG DB ' ', '>'
DB 13, 10, 'The smallest char is: <'
MIN DB 127, '>$'
NNN DB 0
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
;------------------------------
MOV DX, OFFSET MSG1
MOV AH, 9
INT 21H
M_LOOP:
MOV AH, 1
INT 21H
CMP AL, 13
JE DISP
INC NNN
CMP AL, DS:[BIG]
JB NEXT
MOV DS:[BIG], AL
NEXT:
CMP AL, DS:[MIN]
JNB M_LOOP
MOV DS:[MIN], AL
JMP M_LOOP
DISP:
CMP NNN, 0 ;输入 0 个吗 ?
JE EXIT ;是则退出,不予显示
MOV DX, OFFSET MSG2
MOV AH, 9
INT 21H
;------------------------------
EXIT:
MOV AH, 4CH
INT 21H
CODE ENDS
END START
本程序执行后,显示如下:
Please input some chars: 1357924680
The biggest char is: <9>
The smallest char is: <0>
本程序,没有使用 BH、BL,而是使用了存储器变量 BIG、MIN。
效果是相同的。
因为,BH 等8位的寄存器,仅仅有八个,经常不够用。
存储器变量,则可以定义 65535 个。
这样,程序就更有通用性。