+ -
当前位置:首页 → 问答吧 → 汇编语言实验题:分类统计字符个数,出现问题,急需解决!!!

汇编语言实验题:分类统计字符个数,出现问题,急需解决!!!

时间:2011-06-19

来源:互联网

;这是我写的代码,个人认为没有错误,但出来的结果是错的!希望高手能帮我解决
;*********************************************************************
data segment
  string1 label byte
  max1 db 80
act1 db ?
  str1 db 80 dup(?)
  numbers db ?
  letters db ?
  others db ?
  mess1 db 'The numbers of number:','$'
mess2 db 'The numbers of letter:','$'
mess3 db 'The numbers of others:','$'
  mess4 db 13,10,'$'
  mess5 db 'Enter Sentence:',13,10,'$'
data ends
;*********************************************************************
code segment
main proc far
  assume cs:code,ds:data,es:data

start: push ds
  sub ax,ax
  push ax

  mov ax,data
  mov ds,ax
  mov es,ax

  mov numbers,0
  mov letters,0
  mov others,0
  mov ch,act1

  lea dx,mess5
mov ah,09h
  int 21h
lea dx,string1
  mov ah,0ah
int 21h
lea dx,mess4
mov ah,09h
  int 21h

  lea si,str1
p5: mov dx,[si]
  cmp dx,'0'
jl p1 ;dx<'0'
cmp dx,'9'
  jle p2 ;'0'<=dx<='9'
  cmp dx,'A'
  jl p1 ;'9'<dx<'A'
  cmp dx,'Z'
  jle p3 ;'A'<=dx<='Z'
  cmp dx,'a'
  jl p1 ;'Z'<dx<'a'
  cmp dx,'z'
  jle p3 ;'a'<=dx<='z'
  jmp p1 ;dx>'z'
p1: inc others ;bh中存放其他字符的个数
  jmp p4 
p2: inc numbers ;al中存放数字的个数
  jmp p4
p3: inc letters ;bl中存放字母的个数
p4: inc si
  dec ch
  cmp ch,0
  jnz p5
  mov cl,4

p8: lea dx,mess1
  mov ah,09h
  int 21h
  mov ch,2
  jmp p7
p9: lea dx,mess2
  mov ah,09h
  int 21h
  mov ch,2
  jmp p12
p10: lea dx,mess3
mov ah,09h
  int 21h
  mov ch,2
  jmp p13  
   
p7: rol numbers,cl
  mov bh,numbers
  and bh,0fh
  jmp p14
p12: rol letters,cl
  mov bh,letters
  and bh,0fh
  jmp p16
p13: rol others,cl
  mov bh,others
  and bh,0fh
  jmp p18

p14: add bh,30h
  cmp bh,3ah
  jb p15
  add bh,07h
p15: mov dl,bh
mov ah,2
int 21h
  dec ch
cmp ch,0
jnz p7
  lea dx,mess4
  mov ah,09h
  int 21h
  jmp p9

p16: add bh,30h
  cmp bh,3ah
  jb p17
  add bh,07h
p17: mov dl,bh
mov ah,2
int 21h
  dec ch
cmp ch,0
jnz p12
  lea dx,mess4
  mov ah,09h
  int 21h
jmp p10

p18: add bh,30h
  cmp bh,3ah
  jb p19
  add bh,07h
p19: mov dl,bh
mov ah,2
int 21h
  dec ch
cmp ch,0
jnz p13
  lea dx,mess4
  mov ah,09h
  int 21h
jmp exit
exit: ret
main endp
code ends
  end start

此程序在编译和连接时都没有错误,只是结果有问题
运行结果如下:
Enter Sentence:
we are 123
按回车键出现如下结果:
The numbers of number:00
The numbers of letter:34
The numbers of others:FD
 

作者: aomi98   发布时间: 2011-06-19

Assembly code
;
;This Program Compiled Sucess by Masm 6.15
;
assume    cs:code,ds:data
data    segment
msg    db    'Enter a string:$'
buffer    db    80,0,80 dup (0)
_number    db    0
_cap    db    0
_low    db    0
_other    db    0
nummsg    db    'numbers of digit:$'
capmsg    db    'numbers of cap-letter:$'
lowmsg    db    'numbers of low-letter:$'
othmsg    db    'numbers of other-char:$'
data    ends
code    segment
start:
    mov ax,data
    mov ds,ax

    mov dx,offset msg
    mov ah,9
    int 21h

    mov dx,offset buffer
    mov ah,10
    int 21h
    
    call crlf
    
    mov bx,offset buffer+2
    mov cl,buffer+1
s:
    mov al,[bx]
    cmp al,'0'
    jae sz
    jmp oth
sz:
    cmp al,'9'
    jbe num
    jmp zm
num:
    inc _number
    jmp jx
zm:
    cmp al,'A'
    jae zm0
    jmp oth
zm0:
    cmp al,'Z'
    jbe zm1
    jmp zm2
zm1:
    inc _cap
    jmp jx
zm2:
    cmp al,'a'
    jae zm3
    jmp oth
zm3:
    cmp al,'z'
    jbe zm4
oth:
    inc _other
    jmp jx
zm4:
    inc _low
jx:
    inc bx
    loop s
    

    mov dx,offset nummsg
    mov ah,9
    int 21h

    mov dl,_number
    call disp
    
    call crlf

    mov dx,offset capmsg
    mov ah,9
    int 21h
    
    mov dl,_cap
    call disp

    call crlf

    mov dx,offset lowmsg
    mov ah,9
    int 21h

    mov dl,_low
    call disp

    call crlf

    mov dx,offset othmsg
    mov ah,9
    int 21h

    mov dl,_other
    call disp

    mov ah,4ch
    int 21h
;crlf
crlf    proc uses ax dx
    mov ah,2
    mov dl,13
    int 21h
    mov dl,10
    int 21h
    ret
crlf    endp
;
;input:dx
disp    proc uses dx
    mov ax,dx
    xor dx,dx
    mov bx,10
    mov cx,0
d2:    cmp ax,10
    jb ok2
    div bx
    add dl,30h
    push dx
    xor dx,dx
    inc cx
    jmp d2
ok2:    add al,30h
    push ax
    inc cx
d3:    pop dx
    mov ah,2
    int 21h
    loop d3
    ret
disp    endp
;
code    ends
end    start

作者: masmaster   发布时间: 2011-06-19