+ -
当前位置:首页 → 问答吧 → 【求救】(汇编)编程,对输入的一串字符串进行统计

【求救】(汇编)编程,对输入的一串字符串进行统计

时间:2010-12-11

来源:互联网


;程序如下:
;
data segment
 inmsg db 'please input a string:',0dh,0ah,'$' ;输入提示信息
 indat db 0ffh,0,0ffh dup(0) ;输入缓冲区
 cmsg db 0dh,0ah,'the count of Capital is: $'
 nmsg db 'the count of Number is: $'
 lmsg db 'the count of Lowercase is: $'
 omsg db 'the count of Other char is: $'
 csum db 0,0,0dh,0ah,'$' ;大写计数器
 nsum db 0,0,0dh,0ah,'$' ;数字计数器
 lsum db 0,0,0dh,0ah,'$' ;小写计数器
 osum db 0,0,0dh,0ah,'$' ;其它字符计数器
data ends
code segment
  assume cs:code,ds:data
start:mov ax,data
  mov ds,ax
  mov dx,offset inmsg
  mov ah,9
  int 21h
  mov dx,offset indat ;输入字符串
  mov ah,10  
  int 21h
  mov si,offset indat+1
  mov cl,[si] ;字符串长度存CL
compare:
  inc si
  cmp byte ptr [si],'0'
  jb other
  cmp byte ptr [si],'9' ;检查是否数字
  jbe number
  cmp byte ptr [si],'A'
  jb other
  cmp byte ptr [si],'Z' ;是否大写字母
  jbe capital
  cmp byte ptr [si],'a'
  jb other
  cmp byte ptr [si],'z' ;是否小写字母
  ja other
;下面这个代码段看不太懂,相加的高低位和显示的高低位刚好相反的话,也就是说
;mov ax,word ptr [lsum]这里边AH里装的是要显示的低位,AL...那之后又为什么又XCHG了呢
;这样不是又回到原来原来高BITE装要显示输出的低位了?lowercase:
  mov ax,word ptr [lsum] ;小写字母次数送AX,准备加1
  xchg al,ah ;交换是因为相加的高低位和显示的高低位刚好相反
  add al,1
  aaa ;低位加1后调整
  mov bl,al
  mov al,ah
  add al,0
  aaa ;高位调整,不用ADC指令是因为AAA指令已经把进位送进AH中了。
  mov bh,al
  xchg bl,bh
  mov word ptr [lsum],bx ;加1后送回计数器单元
  dec cl
  jnz compare
  jmp disp
number:
  mov ax,word ptr nsum ;步骤同上。
  xchg al,ah
  add al,1
  aaa
  mov bl,al
  mov al,ah
  add al,0
  aaa
  mov bh,al
  xchg bl,bh
  mov word ptr nsum,bx
  dec cl
  jnz compare
  jmp disp
capital:
  mov ax,word ptr csum
  xchg al,ah
  add al,1
  aaa
  mov bl,al
  mov al,ah
  add al,0
  aaa
  mov bh,al
  xchg bl,bh
  mov word ptr csum,bx
  dec cl
  jnz compare
  jmp disp
other:
  mov ax,word ptr osum
  xchg al,ah
  add al,1
  aaa
  mov bl,al
  mov al,ah
  add al,0
  aaa
  mov bh,al
  xchg bl,bh
  mov word ptr osum,bx
  dec cl
  jnz compare
  jmp disp
   
disp: add word ptr csum,3030h ;4个种类计数器的内容变成ASCII码,准备显示
  add word ptr nsum,3030h
  add word ptr lsum,3030h
  add word ptr osum,3030h
  mov dx,offset cmsg ;以下分别显示各种类次数。
  mov ah,9
  int 21h
  mov dx,offset csum
  mov ah,9
  int 21h
  mov dx,offset nmsg
  mov ah,9
  int 21h
  mov dx,offset nsum
  mov ah,9
  int 21h
  mov dx,offset lmsg
  mov ah,9
  int 21h
  mov dx,offset lsum
  mov ah,9
  int 21h
  mov dx,offset omsg
  mov ah,9
  int 21h
  mov dx,offset osum
  mov ah,9
  int 21h
  mov ah,4ch
  int 21h
code ends
  end start

作者: lgh0223   发布时间: 2010-12-11

解释一下lowercase:代码段就可以,3Q3Q!!!!!

作者: lgh0223   发布时间: 2010-12-11