输入几个数字请和最大最小值平均数
a:输入3,2,1show或者99,100,32,0,8show显示输出;
b:输入exit退出。
代码
DATAS SEGMENT
len = 128
count = 10
k_exit db 'exit'
k_show db 'show'
arrNum db count dup(0)
tmpBuf db 8 dup('0')
outBuf db 8 dup('0')
maxMsg db 'Max number is: $'
minMsg db 'Min number is: $'
sumMsg db 'Total is: $'
avrMsg db 'Average is: $'
errMsg1 db 'Input number must < 256',13,10,'$'
errMsg2 db 'Overflow while summary all number',13,10,'$'
msg1 db 'Please input: $'
msg2 db 'Your input message: $'
buf db len
db ?
db len dup('0')
db '$'
DATAS ENDS
STACKS SEGMENT
db 10 dup(0)
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
CLEARBUF PROC
push bx
mov cx,10
lea bx,arrNum
@@:
mov [bx],0
inc bx
loop @b
pop bx
ret
CLEARBUF ENDP
SHOWSTR PROC
mov ah,9
int 21h
ret
SHOWSTR ENDP
SHOWMAX PROC
lea dx,maxMsg
call SHOWSTR
ret
SHOWMAX ENDP
SHOWMIN PROC
lea dx,minMsg
call SHOWSTR
ret
SHOWMIN ENDP
SHOWSUM PROC
lea dx,sumMsg
call SHOWSTR
ret
SHOWSUM ENDP
SHOWAVER PROC
lea dx,avrMsg
call SHOWSTR
ret
SHOWAVER ENDP
NEWLINE PROC
push dx
push ax
mov dl,13
mov ah,2
int 21h
mov dl,10
mov ah,2
int 21h
pop ax
pop dx
ret
NEWLINE ENDP
ISEXIT PROC
xor dx,dx
mov dl,buf+1
sub dl,4
mov si,offset buf+2
add si,dx
lea di,k_exit
mov cx,4
xor ax,ax
cld
@@:
cmpsb
jne isexit2
inc ax
loop @b
isexit2:
ret
ISEXIT ENDP
ISSHOW PROC
xor dx,dx
mov dl,buf+1
sub dl,4
mov si,offset buf+2
add si,dx
lea di,k_show
mov cx,4
xor ax,ax
cld
@@:
cmpsb
jne isshow2
inc ax
loop @b
isshow2:
ret
ISSHOW ENDP
INTTOSTR PROC ;16bit
push si
push di
push bx
mov dl,10
lea bx,tmpBuf
xor si,si
@@:
test ax,ax
jz @@0
div dl
add ah,30H
mov [bx],ah
and ax,0FH
inc bx
inc si
jmp @b
@@0:
mov [bx],'$'
mov bx,si
mov cx,bx
dec bx
lea si,tmpBuf
lea di,outBuf
@@:
mov al,[bx+si]
mov [di],al
inc di
dec bx
loop @b
mov [di],'$'
pop bx
pop di
pop si
ret
INTTOSTR ENDP
INTTOSTREX PROC ;32bit
push si
push di
push bx
mov di,10
lea bx,tmpBuf
xor si,si
xor dx,dx
@@:
test ax,ax
jz @@1
div di
add dl,30H
mov [bx],dl
xor dx,dx
inc bx
inc si
jmp @b
@@1:
mov [bx],'$'
mov bx,si
mov cx,bx
dec bx
lea si,tmpBuf
lea di,outBuf
@@:
mov al,[bx+si]
mov [di],al
inc di
dec bx
loop @b
mov [di],'$'
pop bx
pop di
pop si
ret
INTTOSTREX ENDP
INTTOSTREX2 proc uses ax bx cx dx
test ax,ax
jz d4
mov bx,10
xor dx,dx
xor cx,cx
cld
d2:
test ax,ax
jz d3
div bx
add dl,30h
push dx
xor dx,dx
inc cx
jmp d2
d3:
pop ax
stosb
loop d3
mov [di],'$'
ret
d4:
mov [di],'0'
mov [di+1],'$'
ret
INTTOSTREX2 endp
GETMAXNUM PROC
push bx
push dx
xor ax,ax
lea bx,arrNum
mov al,[bx]
inc bx
max0:
mov dl,[bx]
cmp dl,al
ja max1
jmp max2
max1:
mov al,dl
max2:
inc bx
cmp [bx],'$'
jnz max0
call INTTOSTREX
pop dx
pop bx
ret
GETMAXNUM ENDP
GETMINNUM PROC
push bx
push dx
xor ax,ax
lea bx,arrNum
mov al,[bx]
test al,al
jz min3
inc bx
min0:
mov dl,[bx]
cmp dl,al
jb min1
jmp min2
min1:
mov al,dl
test al,al
jz min3
min2:
inc bx
cmp [bx],'$'
jnz min0
call INTTOSTREX
pop dx
pop bx
ret
min3:
lea bx,outBuf
mov [bx],'0'
inc bx
mov [bx],'$'
pop dx
pop bx
ret
GETMINNUM ENDP
GETAVERAGE PROC
push bx
push dx
xor ax,ax
xor cx,cx
xor dx,dx
lea bx,arrNum
AVER0:
mov dl,[bx]
cmp [bx],'$'
jz AVEREXIT
add ax,dx
inc cx
inc bx
jmp AVER0
AVEREXIT:
xor dx,dx
div cx
pop dx
pop bx
ret
GETAVERAGE ENDP
START:
MOV AX,DATAS
MOV DS,AX
MOV ES,AX
NEXT:
call CLEARBUF
lea dx,msg1
mov ah,9
int 21h
call NEWLINE
lea dx,buf
mov ah,10
int 21h
call NEWLINE
lea dx,msg2
mov ah,9
int 21h
call NEWLINE
xor dx,dx
mov dl,buf+1
mov al,'$'
mov bx,offset buf
add bx,dx
add bx,2
mov [bx],al
mov dx,offset buf+2
mov ah,9
int 21h
call NEWLINE
;
call ISSHOW
cmp al,4
je main0
jmp chkexit
main0:
xor dx,dx
mov dl,buf+1
mov al,'$'
mov bx,offset buf
add bx,dx
add bx,2
sub bx,4
mov [bx],al
;
mov bx,offset buf + 2
mov dx,bx
mov ah,9
int 21h
call newline
mov di,offset arrNum
xor dx,dx
xor ax,ax
mov cl,10
main1:
mov dl,[bx]
cmp dl,'$'
je main5
cmp dl,','
jne main2
jmp main3
main2:
mul cl
sub dl,30H
add al,dl
jc ERROR1
jmp main4
main3:
mov [di],al
inc di
xor dx,dx
xor ax,ax
main4:
inc bx
jmp main1
main5:
mov [di],al
inc di
mov [di],'$'
call SHOWMAX
xor ax,ax
xor dx,dx
call GETMAXNUM
lea dx,outBuf
mov ah,9
int 21h
call NEWLINE
call SHOWMIN
call GETMINNUM
lea dx,outBuf
mov ah,9
int 21h
call NEWLINE
xor ax,ax
xor dx,dx
mov cx,count
mov bx,offset arrNum
@@:
mov dl,[bx]
add ax,dx
jc ERROR2
inc bx
cmp [bx],'$'
jnz @b
call INTTOSTREX
call SHOWSUM
lea dx,outBuf
mov ah,9
int 21h
call NEWLINE
call GETAVERAGE
call INTTOSTREX
call SHOWAVER
lea dx,outBuf
mov ah,9
int 21h
call NEWLINE
chkexit:
call ISEXIT
cmp al,4
je EXIT
jmp NEXT
EXIT:
MOV AH,4CH
INT 21H
ERROR1:
lea dx,errMsg1
mov ah,9
int 21h
jmp NEXT
ERROR2:
lea dx,errMsg2
mov ah,9
int 21h
jmp NEXT
CODES ENDS
END START