内存自source开始有20个数字、大写字母、小写字母的混合串(比如:1234567890abceFGHIJ)
编程将其中的数字符号挑选出来存储到dst开始的内存中,并将结果显示到CRT上。
data segment
source db "1234567890abceFGHIJ"
dst db 20 dup(?)
data ends
code segment
assume cs:code,ds:data,es:data
start:
mov ax, data
mov ds, ax
num:
mov si, offset source
mov di, offset dst
s1:
mov al, [si]
cmp al, 30h
jb next1
cmp al, 39h
ja next1
mov [di], al
inc di
next1:
inc si
cmp si, offset dst
jb s1
mov byte ptr[di], '$'
mov dx, offset dst
mov ah, 9
int 21h
exit:
mov ah, 4ch
int 21h
code ends
end start
扩展A:做一个菜单,选1,2,3,4分别输出数字、大写、小写及退出
扩展B:同时输出数字、大写、小写的个数
data segment
menu db "1 print num", 0dh, 0ah ;定义打印菜单
db "2 print upper case", 0dh, 0ah
db "3 print lower case", 0dh, 0ah
db "4 exit", 0dh, 0ah, '$' ;0dh回车,0ah换行
choice db "please choice 1 2 3 4: ", 0dh,0ah,'$'
NumOut db 0dh, 0ah,"The number of Num is :$"
UpperOut db 0dh, 0ah,"The number of Upper is :$"
LowerOut db 0dh, 0ah,"The number of Lower is :$"
source db "1234567890abcdeFGHIJ"
count equ $-source ;count为混合串长度
dst db 20 dup(?) ;dst内存开始存放结果
Nonum db 0 ;统计数字的个数
Noupper db 0 ;统计大写字母的个数
Nolower db 0 ;统计小写字母的个数
data ends
code segment
assume cs:code, ds:data, es:data
start:
mov ax, data ;将数据段地址装入段寄存器
mov ds, ax
mov Nonum,0
mov Noupper,0
mov Nolower,0
mov dx, offset menu ;调用9功能打印字符串
mov ah, 9
int 21h
mov dx, offset choice
mov ah, 9
int 21h
mov ah, 8 ;调用8功能,输入
int 21h
cmp al, '1'
jz num
cmp al, '2'
jz up
cmp al, '3'
jz low1
jmp exit
;选出数字符号并显示
num:
mov bx, offset source
mov si, offset dst ;si存放结果,从dst开始存放
mov cl, count
next1:
mov al, [bx]
cmp al, 30h ;大于39h小于30h不是数字跳转到l1
jb l1
cmp al, 39h
ja l1
mov [si], al
add Nonum, 1
inc si
l1:
inc bx
dec cl
jnz next1
mov dx,offset NumOut
mov ah,9
int 21h
mov al,Nonum
CALL B_TO_D16 ;调用子程序打印统计个数
jmp dsp
;选出大写字母符号并显示
up:
mov bx, offset source
mov si, offset dst
mov cl, count
next2:
mov al, [bx]
cmp al, 41h
jb l2
cmp al, 5ah
ja l2
mov [si], al
add Noupper, 1
inc si
l2:
inc bx
dec cl
jnz next2
mov dx,offset UpperOut
mov ah,9
int 21h
mov al,Noupper
CALL B_TO_D16
jmp dsp
;选出小写字母符号并显示
low1:
mov bx, offset source
mov si, offset dst
mov cl, count
next3:
mov al, [bx]
cmp al, 61h
jb l3
cmp al, 7ah
ja l3
mov [si], al
add Nolower, 1
inc si
l3:
inc bx
dec cl
jnz next3
mov dx,offset LowerOut
mov ah,9
int 21h
mov al, Nolower
CALL B_TO_D16
dsp: mov byte ptr[si],'$' ;输出si的内容
mov dl,0ah
mov ah,2
int 21h
mov dl,0dh
mov ah,2
int 21h
mov dx,offset dst
mov ah,9
int 21h
jmp start
B_TO_D16 PROC NEAR;16位二进制转十进制子程序
AND AH,0 ;ah存放余数,初始化为0
MOV BL,100
DIV BL ;ax/100得到百位数,存放al中
MOV CL,AH ;余数暂存于cl中
ADD AL,30H ;输出百位数
MOV DL,AL
MOV AH,02H
INT 21H
MOV AL,CL
MOV BL,10
AND AH,0
DIV BL ;ax/10得到十位数
ADD AL,30H ;输出十位数
MOV DL,AL
MOV CL,AH
MOV AH,02H
INT 21H
MOV AL,CL ;输出个位数
ADD AL,30H
MOV DL,AL
MOV AH,02H
INT 21H
RET
B_TO_D16 ENDP
exit:
mov ah, 4cH
int 21H
code ends
end start