七评委
OUTPUTASTR MACRO STRADDR ;输出一个字符串,STRADDR是字符串的首地址
PUSH AX
PUSH DX
LEA DX, STRADDR
MOV AH, 9
INT 21H
POP DX
POP AX
ENDM
DATAS SEGMENT
cue1 db "Please the $"
cue2 db " judges grades: $"
NEWLINE DB 13, 10, '$'
INP DB 30H DUP('$')
NUM DB 10H DUP(0)
RESULT DW 0
ILG DB "illegal! Try again!$"
CUE3 DB "Averange is: $"
DATAS ENDS
STACKS SEGMENT PARA STACK
DW 30H DUP(?)
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
RESTART:
xor cx, cx
mov cl, 30h
XOR SI, SI
INPUT:
mov ah, 9h
lea dx, cue1
int 21h
INC CL
mov ah, 02h
mov dl, cl
int 21h
mov ah, 9h
lea dx, cue2
int 21h
PUSH CX; CX = 3XH
SUB CL, 31H
MOV DI, CX
ADD DI, CX
ADD DI, CX
ADD DI, CX
ADD DI, CX
LEA DX, INP[DI]
MOV AH, 0AH
INT 21H
MOV CL, INP[DI+1]
CMP CL, 3
JA ILLEGAL
;EXCHANGE TO TRUE VALUE
XOR AH, AH
MOV BL, 10
XOR DL, DL
INC DI
EXCHANGE:
PUSH CX; CX <= 3
INC DI
MOV AL, INP[DI]
CMP AL, 30H
JB ILLEGAL
CMP AL, 39H
JA ILLEGAL
SUB AL, 30H
ADD AL, DL
CMP CL, 1
JE MULDONE
MUL BL
MULDONE:
MOV DL, AL
CMP DL, 100
JA ILLEGAL
MOV NUM[SI], DL
POP CX
LOOP EXCHANGE
; PREPARE FOR NEXT INPUTMENT
INC SI
LEA DX, NEWLINE
MOV AH, 09H
INT 21H
POP CX
CMP CL, 37H
JNE input
;INPUT PART HAS DONE
;SORT
LEA SI, NUM
CALL SORT
;SORT DONE
;GET sum of the five numbers
LEA DI, RESULT
LEA SI, NUM
CALL SUM
;SHOW SUM
;MOV AX, RESULT
;CALL B2D
OUTPUTASTR NEWLINE
;SHOW AVERANGE
OUTPUTASTR CUE3
MOV AX, RESULT
XOR BX, BX
MOV BL, 5
CALL AVERANGE
EXIT:
MOV AH,4CH
INT 21H
ILLEGAL:
OUTPUTASTR NEWLINE
OUTPUTASTR ILG
OUTPUTASTR NEWLINE
JMP RESTART
AVERANGE PROC;求平均值
PUSH DX
PUSH CX
PUSH BX
PUSH AX
XOR CX, CX
MOV CL, 3
AVE:
DIV BL
PUSH BX
XOR DX, DX
MOV DL, AH
AND AX, 00FFH
CMP CL, 1
JE ROUNDING; 四舍五入
CALL B2D
CMP CL, 3
JE PRINTPOINT
CTN:
MOV AX, DX
MOV BL, 10
MUL BL
POP BX
LOOP AVE
RT:
POP AX
POP BX
POP CX
POP DX
RET
ROUNDING: ;四舍五入
PUSH DX
PUSH AX
PUSH BX
MOV AX, DX
MOV BL, 10
MUL BL
POP BX
DIV BL
CMP AH, 4
JBE B
POP AX
ADD AX, 1
CALL B2D
JMP A
B:
POP AX
CALL B2D
A:
POP DX
POP BX
JMP RT
PRINTPOINT:
PUSH AX
PUSH DX
MOV AH, 02H
MOV DL, '.'
INT 21H
POP DX
POP AX
JMP CTN
AVERANGE ENDP
SUM PROC; get sum
; SI SHOULD HAVE BEEN ASSIGNED SCORSE ADDRESS
; DI SHOULD HAVE BEEN ASSIGNED TARGET ADDRESS
PUSH AX
PUSH BX
PUSH CX
PUSH DX
XOR CX, CX
XOR AX, AX
XOR DX, DX
MOV CL, 5; the numbers which are about to sum are 5
INC SI ;jump to the second number
SUMLOOP:
MOV DL, [SI]
INC SI
ADD AX, DX
LOOP SUMLOOP
MOV [DI], AX
POP DX
POP CX
POP BX
POP AX
RET
SUM ENDP
B2D PROC ; true value to decimal and output
PUSH BX
PUSH CX
PUSH DX
PUSH AX
XOR BX, BX
MOV BL, 10
xor CX, CX
LOP1:
DIV BL ;AX / BL = AL ... AH
PUSH AX
inc CL
AND AX, 00FFH
CMP AX, 0
JNE LOP1
lop2:
pop ax
MOV DL, AH
ADD DL, 30H
MOV AH, 2
INT 21H
loop lop2
POP AX
pop DX
pop CX
pop BX
RET
B2D ENDP
;SORT FUNCTION
SORT PROC; SI SHOULD HAVE BEEN ASSIGNED
PUSH AX
PUSH BX
PUSH CX
PUSH DX
XOR CX, CX
MOV CL, 7 ;SET COUNTING ELEMENT NUMBER, CAN BE CHANGED
OUTERLOOP:
PUSH CX
SUB CL, 1
PUSH SI
INTERLOOP:
MOV AL, [SI]
MOV BL, [SI+1]
CMP AL, BL
JA ALTB
CONTINUE1:
INC SI
CMP CL, 0
JE CONTINUE2
DEC CL
JNE INTERLOOP
CONTINUE2:
POP SI
POP CX
DEC CX
CMP CX, 0
JNE OUTERLOOP
POP DX
POP CX
POP BX
POP AX
RET
ALTB: ; exchange two unsort numbers
MOV DL, AL
MOV AL, BL
MOV BL, DL
MOV [SI], AL
MOV [SI+1], BL
JMP CONTINUE1
SORT ENDP
CODES ENDS
END START
统计字符串数字、字母、其他符号个数
DATAS SEGMENT
;此处输入数据段代码
input1 db 80,?,80 dup(?) ;定义输入字符串的缓冲区,数目最大为80
print db 'Input the sentence: ','$'
msg1 db 'The number of letter is ','$' ;输出letter的数目
msg2 db 'The number of digit is ','$' ;输出digit的数目 数字
msg3 db 'The number of other is ','$' ;输出other的数目
crlf1 db 13,10,'$' ;回车换行
letter db ? ;存letter数目
digit db ? ;存储digit数目
other db ? ;存储other数目
DATAS ENDS
STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
;此处输入代码段代码
xor ax,ax
mov letter,al
mov digit,al
mov other,al ;初始化0
LEA DX,PRINT
MOV AH,09H
INT 21H ;打印print
lea dx,input1
mov ah,0ah
int 21h ;输入字符串
xor cx,cx
mov cl,[input1+1] ;取得输入字符串个数
lea SI,input1+2 ;获得第一个字符的地址
lea dx,crlf1
mov ah,09h
int 21h ;换行
;mov dl,cl
;add dl,30h
;mov ah,02h
;int 21h
call count ;处理字符串
count proc
;push cx ;得到循环次数
go: ;0035
mov bl,[SI] ;得到SI处的字符
cmp bl,'0'
jb other1 ;比0小就是other
cmp bl,'9'
ja letter1 ;判断是否是字母
jmp digit1 ;否则就是数字
letter1:
cmp bl,'A'
jb other1 ;比A小就是other
cmp bl,'Z'
ja letter2 ;判断是否是小写
jmp letter3 ;否则就是字母
letter2:
cmp bl,'a'
jb other1 ;比a小就是other
cmp bl,'z'
ja other1 ;比a大就是other
jmp letter3 ;否则就是字母
digit1: ;数字的情况 005b
mov al,digit
inc al
dec cl
mov digit,al ;digit [00b4]
cmp cl,0
jz output1 ;0091
jnz rego
other1: ;other的情况
mov al,other
inc al
dec cl
mov other,al
cmp cl,0
jz output1
jnz rego
letter3: ;字母的情况
mov al,letter
inc al
dec cl
mov letter,al
cmp cl,0
jz output1
jnz rego
rego:
inc SI ;下一个位置
jmp go
output1: ;0091
call output
count endp
output proc ;0095
mov bl,10
lea dx,msg1 ;msg1[00b0]
mov ah,09h
int 21h ;显示msg1
;多位数输出
xor ax,ax
;看看需不需要清空al
mov al,letter ;现在dl里面是真值
div bl ;al是商 ah是余数
push ax
cmp al,0
jz out1 ;如果al商是0,不要输出
mov dl,al
add dl,30h;输出商
mov ah,02h
int 21h
out1:
pop ax
mov dl,ah
add dl,30h
mov ah,02h
int 21h
lea dx,crlf1
mov ah,09h
int 21h
lea dx,msg2
mov ah,09h
int 21h
;多位数输出
xor ax,ax
;看看需不需要清空al
mov al,digit ;现在dl里面是真值
div bl ;al是商 ah是余数
push ax
cmp al,0
jz out2 ;如果al商是0,不要输出
mov dl,al
add dl,30h;输出商
mov ah,02h
int 21h
out2:
pop ax
mov dl,ah
add dl,30h
mov ah,02h
int 21h
lea dx,crlf1
mov ah,09h
int 21h
lea dx,msg3
mov ah,09h
int 21h
;多位数输出
xor ax,ax
;看看需不需要清空al
mov al,other ;现在dl里面是真值
div bl ;al是商 ah是余数 ax / bl = al ... ah
push ax
cmp al,0
jz out3 ;如果al商是0,不要输出
mov dl,al
add dl,30h;输出商
mov ah,02h
int 21h
out3:
pop ax
mov dl,ah
add dl,30h
mov ah,02h
int 21h
output endp
MOV AH,4CH
INT 21H
CODES ENDS
END START
七评委(2)
;;;;;;;;;;;;;;;;七评委
DATAS SEGMENT
;此处输入数据段代码
inp db 80h,?,80 dup('$')
num db 10h dup (0)
msg1 db 'Input the No.$'
msg2 db ' judge score: $'
crlf db 13,10,'$'
warning db 'Error, try again.$'
result dw 0
msg3 db 'Average is : $'
DATAS ENDS
STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
re:
xor cx,cx
mov cl,30h
xor SI,SI
input:
lea dx,msg1
mov ah,09h
int 21h ;显示字符串msg1
inc cl
mov dl,cl
mov ah,02h
int 21h
lea dx,msg2
mov ah,09h
int 21h ;显示字符串msg2
push cx;保存第几个评委 这个是ascii码
sub cl,31h ;评委开始放了
mov DI,cx
add DI,cx
add DI,cx
add DI,cx
add DI,cx
lea dx,inp[DI]
mov ah,0ah
int 21h ;开始输入字符
mov cl,inp[DI+1] ;取得输入的字符个数
cmp cl,3 ;超过三位数报错
ja illegal
xor ah,ah
mov bl,10
inc DI
xor dl,dl
exchange:
push cx ;<=3
inc DI
mov al,inp[DI] ;将字符串里的字符取出来放到al中
cmp al,'0'
jb illegal
cmp al,'9'
ja illegal ;不是数字就报错
sub al,30h ;转换成真值
add al,dl
cmp cl,1 ;看是不是个位数了
je mult
mul bl ;*10 字节乘法 乘积放到ax里面
mult:
mov dl,al
cmp dl,100 ;分数超过100报错
ja illegal
mov num[SI],dl
pop cx
loop exchange
;下一次输入
inc SI
lea dx,crlf
mov ah,09h
int 21h
pop cx
cmp cl,37h
jne input
;输入结束 数字存放在num中
lea SI,num
call bubble_sort
;排序后,存放在num里
lea DI,result
lea SI,num
call sum
lea dx,crlf
mov ah,09h
int 21h ;换行
lea dx,msg3
mov ah,09h
int 21h
mov ax,result
xor bx,bx
mov bl,5 ;5个数的平均值 可更改
call average
exit:
MOV AH,4CH
INT 21H
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
illegal:
lea dx,crlf
mov ah,09h
int 21h
lea dx,warning
mov ah,09h
int 21h
lea dx,crlf
mov ah,09h
int 21h
jmp re
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sum proc ;求和
push ax ;保护现场
push bx
push cx
push dx
xor cx,cx
xor ax,ax
xor dx,dx
mov cl,5 ;需要求和的数是5个
inc SI
sumloop:
mov al,[SI]
inc SI
add dx,ax
loop sumloop
mov [DI],dx
pop dx
pop cx
pop bx
pop ax
ret
sum endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
bubble_sort proc
push dx ;保护现场
push cx
push bx
push ax
xor cx,cx
mov cl,7 ;排序的数字个数 可以更改
outerloop:
push cx
sub cl,1
push SI
interloop:
mov al,[SI]
mov bl,[SI+1]
cmp al,bl
ja change ;升序排列
c1:
inc SI
cmp cl,0
je c2 ;如果为0 跳转到c2
dec cl
jne interloop
c2:
pop SI ;重新返回到第一个数
pop cx
dec cx
cmp cx,0
jne outerloop
pop ax
pop bx
pop cx
pop dx
ret
change:
mov dl,al
mov al,bl
mov bl,dl
mov [SI],al
mov [SI+1],bl
jmp c1
bubble_sort endp
average proc
push dx ;保护现场
push cx
push bx
push ax
xor cx,cx
mov cl,2 ;控制小数位数 2->1位 3->2
ave:
div bl ;ax / bl = al ... ah
push bx ;这里的bl是5
xor dx,dx
mov dl,ah ;将余数放到dl里保存起来
and ax,00FFH
cmp cl,1
je rounding ;相等就四舍五入
call B2D
cmp cl,2
je printpoint
ctn:
mov ax,dx
mov bl,10
mul bl ;余数 *10
pop bx ;把5拿出来
loop ave
rt:
pop ax
pop bx
pop cx
pop dx
rounding:
push dx
push ax
push bx
mov ax,dx
mov bl,10
mul bl
pop bx ;把5提出来
div bl ;余数再/5
cmp ah,4
jbe b ;<=4
pop ax
add ax,1
call B2D
jmp a
b:
pop ax
call B2D
a:
pop dx
pop bx
jmp rt
printpoint:
push ax
push dx
mov dl,'.'
mov ah,02h
int 21h
pop dx
pop ax
jmp ctn
average endp
B2D proc ;真值转换成十进制输出
;要把转换的数放在 ax 里面
push dx ;保护现场
push cx
push bx
push ax
xor bx,bx
mov bl,10
xor cx,cx
loop1:
div bl ; ax / bl = al ... ah
push ax ;把余数存起来
inc cl
and ax,00FFH ;把余数丢掉 继续除 得到下一个余数
cmp ax,0
jne loop1
loop2:
pop ax ;先进后出
mov dl,ah
add dl,30h
mov ah,2
int 21h
loop loop2
pop ax
pop bx
pop cx
pop dx
ret
B2D endp
CODES ENDS
END START