X86汇编统计字母大小写个数并以字符串形式输出

  1 data    segment 
  2         str_source  db "Hello ** World ** 09aAzZ$"
  3         str1        db 20 dup(0)                ;大写字母
  4         str2        db 20 dup(0)                ;小写字母
  5         int_caption dw 0                        ;大写字母个数
  6         int_lower   dw 0                        ;小写字母个数
  7 data    ends 
  8 
  9 code    segment                         ;设置代码段s
 10         assume   cs:code,ds:data
 11 
 12 main    proc    near
 13 start: 
 14         mov ax , data                   ;初始化ds
 15         mov ds , ax
 16         mov si , offset str_source      ;offset str_source-->si 变址
 17         xor ax , ax
 18         push ax                         ;利用堆栈存储str1,str2当前指针
 19         push ax                         ;str1,str2指针入栈
 20 
 21 read_loop:
 22         mov al , [si]                   ;取[si]中的内容,即偏移量
 23         cmp al , '$'
 24         je  exit
 25 
 26         cmp al , 'A'
 27         jb  loop_next                   ;<
 28         cmp al , 'z'
 29         ja  loop_next                   ;>
 30         cmp al , 'a'
 31         jnl  lower_case                 ;>=
 32         cmp al , 'Z'
 33         ja  loop_next                   ;>
 34 
 35         pop di                          ;大写
 36         mov str1[di] , al
 37         inc di
 38         push di
 39         inc int_caption                
 40         jmp loop_next  
 41 lower_case:                             ;小写
 42         pop bx                          ;str1指针暂存
 43         pop di
 44         mov str2[di] , al
 45         inc di
 46         push di
 47         push bx
 48         inc int_lower
 49 loop_next:
 50         inc si
 51         jmp read_loop  
 52 
 53 exit:   
 54         pop di                          ;将str1指针弹栈,切勿在子程序中弹栈-->子程序中为新栈
 55         call print_caption
 56 
 57         call print_crlf
 58 
 59         pop di
 60         call print_lowercase
 61 
 62         mov ax , 4c00h                  ;返回DOS
 63         int 21h 
 64 main    endp
 65 
 66 print_ax_dec    proc    near
 67         or  ax , ax
 68         jz  zero
 69         mov bx , -1                     ;余数栈底标志
 70         push bx
 71         mov bx , 10                     ;除数
 72 
 73 repeat: xor dx , dx
 74         div bx
 75         mov cx , ax                     ;商
 76         or  cx , dx
 77         jz  print_exit                  ;商与余数全零则结束
 78         push dx
 79         jmp repeat
 80 
 81 zero:   mov dl , 30h
 82         mov ah , 02h
 83         int 21h
 84 
 85 print_exit:
 86         pop dx
 87         cmp dx , -1
 88         je  proc_exit
 89         add dx , 30h
 90         mov ah , 02h
 91         int 21h
 92         jmp print_exit
 93 
 94 proc_exit:   
 95         ret
 96 print_ax_dec    endp
 97 print_caption   proc    near
 98         mov al , '$'
 99         mov str1[di],al
100         mov dx , offset str1            ;一定要为dx,偏移地址
101         mov ah , 09h
102         int 21h
103 
104         call print_space
105         mov ax , int_caption
106         call print_ax_dec
107         ret
108 print_caption   endp
109 print_lowercase proc    near
110         mov al , '$'
111         mov str2[di],al
112         mov dx , offset str2            ;一定要为dx,偏移地址为16位
113         mov ah , 09h
114         int 21h
115 
116         call print_space
117         mov ax , int_lower
118         call print_ax_dec
119         ret
120 print_lowercase endp
121 print_space     proc    near
122         mov dl , 20h                    ;空格
123         mov ah , 02h
124         int 21h   
125         ret
126 print_space     endp
127 print_crlf      proc    near
128         mov dl , 0ah                    ;回车换行
129         mov ah , 02h
130         int 21h
131         mov dl , 0dh
132         mov ah , 02h
133         int 21h      
134         ret
135 print_crlf      endp 
136 
137 code    ends                            ;代码段结束
138 end     start                           ;汇编语言源程序结束

 

posted on 2014-12-25 15:43  Karma_wjc  阅读(1018)  评论(0编辑  收藏  举报