汇编语言(二十)之分类统计字符个数

输入一串字符串,分别统计英文字符,数字字符和其他字符的个数

程序运行:

 

代码:


datas segment

     line_max_length    db 0ffh
     line               db  0, 100h  dup(?)

	 letter_count       dw  0
	 digit_count        dw  0
	 other_count        dw  0
	 
	 input                     db 'input a line:$'
	 output_letter_count       db 0dh,0ah,'letter count:$'
	 output_digit_count        db 0dh,0ah,'digit count:$'
	 output_other_count        db 0dh,0ah,'other count:$'
	 
datas ends

stacks segment stack

    db  100h dup(?)

stacks ends

codes segment

assume cs:codes,ds:datas,ss:stacks
main   proc  far
start:
       push ds
	   mov ax,0h
	   push ax
       mov ax,datas          ;初始化ds
	   mov ds,ax
       ;输入一行字符提示
       lea dx,input
	   mov ah,9
	   int 21h
	   ;输入一行字符
	   lea dx,line_max_length
	   mov ah,10 
	   int 21h 
	   
	   ;对输入的一行字符进行统计
	   mov cl,line
	   xor ch,ch
	   mov si,1
	   cmp cx,0         ;判断字符串的长度是否为0
	   jz break          ;若为0,跳转
	 s:
       mov al,line[si]
	   cmp al,'0'             ;判断字符是否小于'0'
	   jb other               ;若小于'0',则字符为其他字符
	   cmp al,'9'             ;判断字符是否大于'9'
	   ja letter_or_other     ;若大于'9',则字符为字母或其他字符
	   inc digit_count        ;否则,字符为数字字符,数字记录器加1
	   jmp next               ;跳转
	 letter_or_other:
	    and al,11011111b      ;将字符转为大写字符
		cmp al,'A'            ;判断字符是否大于'A'
		jb other               ;若小于,则字符是其他字符,跳转
		cmp al,'Z'            ;判断字符是否大于'Z'
		ja other               ;若大于,则字符为其他字符,跳转
		inc letter_count       ;否则,字符为字母字符,字母记录器加1
		jmp next
	 other:
	   inc other_count         ;字符为其他字符,其他记录器加1 
	 next:
	   inc si
	   loop s
     
    break:	 
        ;输出记录器的值
		lea dx,output_letter_count
		mov ah,9
		int 21h	
		mov ax,letter_count
		call decimal
		
		lea dx,output_digit_count
		mov ah,9
		int 21h
		mov ax,digit_count
		call decimal
		
		lea dx,output_other_count
		mov ah,9
		int 21h
		mov ax,other_count
		call decimal
       ret
	   
main endp

decimal proc near 

        push ax
		push cx 
		push dx
		push bx 
		
		cmp ax,0
		jge no_negative 
		mov bx,ax 
		mov dl,'-'
		mov ah,2 
		int 21h 
		neg bx 
		mov ax,bx 
		
	no_negative:
	   mov cx,0
	   mov bx,10 
	  de:
	   xor dx,dx 
	   div bx 
	   push dx 
	   inc cx
	   cmp ax,0 
	   jnz de 
	   
	  de1:
	   pop dx
	   add dl,30h
	   mov ah,2 
	   int 21h
	   loop de1 
	   
	  pop bx 
	  pop dx 
	  pop cx 
	  pop ax 
	  
	  ret 
	  
decimal endp
codes ends

end main

 

  • 7
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值