汇编:从键盘录入20个数(-128~127),求最大值
include 一套工具.mac
data segment
show db "Please Input twenty numbers:$"
iit1 db "The $"
iit2 db " number is:$"
buf db 20 dup(?)
x db ?,?,?,? ;x[0]用来存负号
w db ? ;用于显示提示信息
max db ?
boundary db "128$"
display db "Max:$"
error1 db "Input error!",13,10,"Please Input again:$"
data ends
stack segment
stack ends
code segment
assume ds:data,cs:code,ss:stack
start:
mov ax,data
mov ds,ax
mov ax,stack
mov ss,ax
mov bx,0
push bx
mov bx,1
;显示提示输入信息
oustring show
ld
inpu1: ;显示提示输入信息
oustring iit1
inc w
cmp w,9
jg www
mov dl,w
add dl,30h
mov ah,2
int 21h
oustring iit2
set1: ;输入
input
cmp al,1bh ;ESC退出
jz exit
cmp al,13 ;回车
jz outpu1
cmp al,'-'
jz minus
;判断是否在0-9
cmp al,30h
jl error0
cmp al,39h
jg error0
cmp bx,4
jg error0
sub al,30h
mov x[bx],al ;输入的字符合法,从x[1]开始存
inc bx
jmp set1
minus:
cmp x[0],0
jnz error0 ;负号已存在则输出错误信息
mov x[0],al ;把负号存入x[0]中
jmp set1
outpu1:
cmp bx,1
jz error0
cmp x[0],'-'
jz negativedealwith
cmp bx,2
jz onedigit
cmp bx,3
jz twodigit
mov al,x+1
mov cl,100
imul cl
mov bl,al
mov al,x+2
mov cl,10
imul cl
add al,x+3
mov bh,0
cbw
add ax,bx
cmp ax,127
jg error0
pop bx
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
onedigit:
pop bx
mov al,x+1
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
twodigit:
mov al,x+1
mov cl,10
imul cl
add al,x+2
pop bx
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
negativedealwith:
cmp bx,2
jz onebit
cmp bx,3
jz twobit
mov al,x+1
mov cl,100
imul cl
mov bl,al
mov al,x+2
mov cl,10
imul cl
add al,x+3
mov bh,0
cbw
neg ax
neg bx
add ax,bx
cmp ax,-128
jl error0
pop bx
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
onebit:
mov al,x+1
neg al
pop bx
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
twobit:
mov al,x+1
mov cl,10
imul cl
add al,x+2
neg al
pop bx
mov buf[bx],al
inc bx
cmp bx,19
jg outpu2
push bx
;清0
jmp clear
clear:
;清0操作
mov bx,0
mov cx,4
re2:
mov x[bx],0
inc bx
loop re2
mov bx,1
ld
jmp inpu1
error0: ;输入错误
ld
oustring error1
;关键的一步,清0操作
mov bx,0
mov cx,4
re1:
mov x[bx],0
inc bx
loop re1
mov bx,1
jmp set1
www: ;显示提示输入信息
mov al,w
cbw
mov cl,10
idiv cl
mov cl,ah
add al,30h
output al
add cl,30h
output cl
oustring iit2
jmp set1
outpu2:
ld
oustring display
mov cx,19
mov bx,0
mov al,buf[bx]
set2:
cmp al,buf[bx+1]
jl exchange
inc bx
loop set2
jmp outpu3
exchange:
mov al,buf[bx+1]
inc bx
loop set2
outpu3:
mov max,al
cmp max,0
jl minusout
outpu4:
cmp max,10
jl oneout1
cmp max,100
jl twoout1
mov al,max
cbw
mov cl,100
idiv cl
mov cl,ah
add al,30h
output al
mov al,cl
cbw
mov cl,10
idiv cl
mov cl,ah
add al,30h
output al
add cl,30h
output cl
jmp exit
oneout1:
add max,30h
output max
jmp exit
twoout1:
mov al,max
cbw
mov cl,10
idiv cl
mov cl,ah
mov dl,al
add dl,30h
mov ah,2
int 21h
add cl,30h
output cl
jmp exit
minusout:
output '-'
cmp max,-128
jz outpu5
neg max
jmp outpu4
outpu5:
oustring boundary
exit:
mov ah,4ch
int 21h
code ends
end start
运行结果如下:
1.边界检测:
2.三位数之间的比较检测:
3.两位数之间比较检测:
4.一位数比较检测:
5.综合检测: