专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

汇编:统计输入字符串内的数字个数(最多三位数0—256)

作者:佚名   来源:本站原创   点击数:  更新时间:2013年12月01日   【字体:

;This is made by wq 
;编程从键盘读入不超过256个字符的字符串;
;编程统计其中数字的个数,并将结果在屏幕上输出。
;经进一步完善(能输出三位数啦)


DATAS SEGMENT
    sum    db 00h    ;累加器
    string db 256 dup(0dh);存放字符
    tital  db  'This program is made by wq.',0dh,0ah
           db  'It is used to calculater the amout of the number in the string ',0dh,0ah,'$'
    tital1 db  'PLease input the string: ','$'
    overs  db  'Amout of the nember is : ','$'
    tital2 db  'Do you want to have a try again(if yes,input "y" else "n") : ',0dh,0ah, '$'
    tital3 db  'Welcome to use this program again','$'
    kongzi db 0dh,0ah,'$'
    result db 256 dup(?)  
DATAS ENDS
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS
START:
    MOV AX,DATAS
    MOV DS,AX
  
    mov ax,offset tital;输出抬头
    mov dx,ax
    mov ah,09h
    int 21h
main:
       lea dx,kongzi
       mov ah,09h
       int 21h
       lea dx,tital1   ;输出抬头1
       mov ah,09h
       int 21h
  
    mov si,0
    mov ax,offset string
    mov dx,ax
again:           ;输入字符
    mov ah,01h
    int 21h
    cmp al,0dh
    jz then
    mov string[si],al
    inc si
    jmp again    ;输入完毕
then:          ;传输
    mov di,0    
    mov si,0
    mov bl,0
exchang:              ;判断字符的性质
     lea dx,string
     cmp string[si],0dh   
     jz over
     cmp string[si],30h
     jnb next
     inc si
    dec cx
    jmp exchang
next:                     
    mov al,string[si]
    cmp string[si],39h
    jbe calculator
    inc si
    dec cx
    jmp exchang
 
calculator:
     lea dx,result
     mov result[di],al
    inc si
    inc sum
    inc di
    dec cx
    jmp exchang  
over:                   ;处理并输出数字个数
   lea dx,overs
   mov ah,09h
   int 21h
  mov ax,0000h
  mov al,sum[0]         ;判断是否为三位数
  cmp al,64h
  jae san
  mov bl,10
  div bl
  mov dx,ax
  add dx,3030h
   mov ah,02h
   int 21h
   mov dl,dh
   mov ah,02h
    int 21h
    jmp a
san: mov bl,10          ;如果是三位数则多处理一位
     div bl
     mov cl,ah          ;把第一位给cl
     add cl,30h
     mov ah,00h
     div bl              ;第二次除十
     mov dx,ax      
     add dx,3030h
     mov ah,02h
     int 21h
     mov dl,dh
     mov ah,02h
     int 21h
     mov dl,cl
     mov ah,02h
     int 21h   
   
a:  lea dx,kongzi    
    mov ah,09h            ;判断是否循环操作
    int 21h
    lea dx,tital2
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
  
    cmp al,'y'
    je main
    lea dx,tital3
    mov ah,09h
    int 21h
       
    MOV AH,4CH
    INT 21H
CODES ENDS
    END START

关闭窗口

相关文章