1. 程式人生 > 其它 >彙編 字串統計 大寫 小寫 數字 其他

彙編 字串統計 大寫 小寫 數字 其他

字串統計

DATAS SEGMENT
  buf db '12ADdf#gh592HKL*','$'
  tp1 db 0;大寫字母個數
  tp2 db 0;小寫字母個數
  tp3 db 0;數字的個數
  tp4 db 0;其他字元的個數

  str1 db 'the number of big is:','$'
  str2 db 'the number of small is:','$'
  str3 db 'the number of number is:','$'
  str4 db 'the number of other is:','$'
  str5 db 0dH,0aH,'$';換行

DATAS ENDS

STACKS SEGMENT
  ;此處輸入堆疊段程式碼
STACKS ENDS

CODES SEGMENT
  ASSUME CS:CODES,DS:DATAS,SS:STACKS
  START:
    MOV AX,DATAS
    MOV DS,AX

    lea si, buf
    mov cx, 16;設定迴圈次數

     again:
      ;字串結尾,結束程式
      cmp byte ptr[si],'&'
      je exit

      ;0-9
      cmp byte ptr[si],30h;小於30,其他字元加1
      jb L1
      cmp byte ptr[si],39h;大於39進一步比較
      jbe L2

      cmp byte ptr[si],41h
      jb L1
      cmp byte ptr[si],5AH
      jbe L3

      cmp byte ptr[si],61h
      jb L1
      cmp byte ptr[si],7AH
      jbe L4


    L1:
      inc tp4
      jmp L5
    L2:
      inc tp3
      jmp L5
    L3:
      inc tp1
      jmp L5
    L4:
      inc tp2
      jmp L5
    L5:
      add si,1
      loop again



    ;顯示大寫字母
    lea dx,str1
    mov ah,09h
    int 21h

    mov bl,tp1
    call disp ;呼叫子程式

    mov ah,09h
    lea dx,str5
    int 21h

    ;顯示小寫字母
    lea dx,str2
    mov ah,09h
    int 21h

    mov bl,tp2
    call disp ;呼叫子程式

    mov ah,09h
    lea dx,str5
    int 21h

    ;顯示數字
    lea dx,str3
    mov ah,09h
    int 21h

    mov bl,tp3
    call disp ;呼叫子程式

    mov ah,09h
    lea dx,str5
    int 21h

    ;顯示其他
    lea dx,str4
    mov ah,09h
    int 21h

    mov bl,tp4
    call disp ;呼叫子程式

    mov ah,09h
    lea dx,str5
    int 21h


    exit:
      MOV AH,4CH
      INT 21H

  disp PROC ;顯示BX中的數
    mov ch,4
    roll:

      mov cl,4
      rol bx,cl
      mov dl,bl
      and dl,0fh
      cmp dl,9
      jbe next1
      add dl,07h
    next1:

       add dl,30h
      mov ah,02h
      int 21h
      dec ch
      jnz roll
    RET
  disp ENDP

CODES ENDS
  END START

2

datas   segment
  letter   db   ?
  digit    db   ?
  other    db   ?
  string   label  byte
           max db 80 
           act db ?  
           str db 80 dup(?)
print   db  13,10,'Please enter the string:','$'
mess1   db  13,10, 'The total number of letter : ','$'
mess2   db  13,10,'The total number of digit  : ','$'
mess3   db  13,10,'The total number of other character : ','$'
datas   ends

code  segment
      assume  cs:code,ds:datas
start: push  ds
       sub  ax,ax
       push  ax
       mov   ax,datas
       mov   ds,ax
       mov   es,ax
       mov   letter,0
       mov   digit,0
       mov   other,0
       lea   dx,print
       mov   ah,09h  
       int   21h
       lea   dx,string
       mov   ah,0ah
       int   21h
       sub   ch,ch
       mov   cl,[string+1]
       lea   si,string+2
	   
digitseg: 
       mov   al,[si] ;數字判斷,小於0為其他,0到9為數字
       cmp   al,'0'
       jb    otherseg
       cmp   al,'9'
       ja    letterseg    
       inc   digit
       jmp   loop1
	   
letterseg: 
       cmp   al,'A' ;大寫字母判斷,9之後,大於9,小於A為其他,A到Z為字母
       jb    otherseg   						
       cmp   al,'Z'
       ja    letter2seg 
       inc   letter
       jmp   loop1  
	   
letter2seg:  
       cmp   al,'a' ;小寫字母判斷,大於Z,小於a為其他,a到z為字母
       jb    otherseg
       cmp   al,'z'
       ja    otherseg
       inc   letter
       jmp   loop1
otherseg: 
       inc   other ;上文中未被識別出的字元均為其他
	   
loop1:  
       inc   si
       dec   cl
       cmp   cl,0
       jz    print1 
       jne   digitseg 
	   
print1:   
       lea   dx,mess1 ;輸出mess1,字母
       mov   ah,09h
       int   21h
	   
       mov   al,letter 
       call  disp
	   
       lea   dx, mess2 ;輸出mess2,數字
       mov   ah,09h
       int   21h
       mov   al,digit
       call  disp
	   
       lea   dx, mess3 ;輸出mess3,其他
       mov   ah,09h
       int   21h
       mov   al,other
       call  disp
exit:
       mov  ah, 4ch
       int  21h
disp:                 						;十進位制數形式顯示AL中的內容.
       mov  ah, 0
       mov  bl, 10
       div  bl        						;div 無符號:div src 16位操作:商ax=(dx,ax)/src,餘數dx
       add  al, 30h   						 ;比如說al=15h,即21,表示letter數量,然後,這個過程就是,ax=0015h(21),除以bl,bl值為10
       mov  dl, al     						 ;則除完了的結果為2餘1,則ah=01,al=02,即ax=0102h;,那麼al+30h即為表示該數字的ASCII碼值,因為0的ASCII值為30h
       mov  bh, ah      					 ;則ax=0132h,dl=32,bh=01;
       mov  ah, 02h     					 ;顯示輸出dx,則顯示32碼對應的數字,2
       int  21h           
       mov  al, bh      						  ;把01給al,然後算出ASCII碼,然後給dx,然後顯示
       add  al, 30h
       mov  dl, al        
       mov  ah, 02h
       int  21h     
       ret
code   ends
       end   start

程式碼倉庫:https://github.com/SKPrimin/HomeWork/tree/main/Assembly/StringStatistics

程式執行

1 2 3 A B c , . ?
letter 1 2 3
digit 1 2 3
other 1 2 3

與事實相符。