汇编语言统计字符输入中的所有 'a'

assembly language count all 'a' in character input

我的代码应该计算每个用户输入中的所有字符 'a' 我使用 cmp 如果相等则我的程序跳转到 'incre:' 增加 bl.the 输出的值总是这个>¶< .我不知道问题出在哪里

title sample.prog
cstack segment para stack 'stack'
dw 200h
cstack ends

cdata segment para 'data'
msg1 db 'ENTER 9 CHARACTER: $',10,13
msg2 db 10,13,'NUMBER OF a: $'
cdata ends

ccode segment para 'code'
assume cs:ccode,ds:cdata,ss:cstack
main:
 mov ax,cdata
 mov ds,ax

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

 mov cl,0
 mov bl,30h

input:
 mov ah,01
 int 21h
 inc cl

 cmp al,61h
 je incre


 cmp cl,9
 je incre
 jmp input

incre:
 inc bl

 cmp cl,9
 jne input

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

 mov ah,02h
 mov dh,bl
 int 21h

 mov ah, 4ch
 int 21h

ccode ends
end main

输入 9 CHARACTERS:aaadfasfg
数量:¶

输入 9 CHARACTERS:fffffffff
数量:¶

输入 9 CHARACTERS:dasdawdaf
数量:¶

您的代码有错别字:

 mov ah,02h
 mov dh,bl    <-- HERE
 int 21h

字符应该放在dl,而不是dh

另一个问题是你递增 bl 一次太多了:

 cmp al,61h
 je incre

 cmp cl,9
 je incre  <-- Wrong. al didn't equal 'a', so we shouldn't jump to incre.
 jmp input

应该改为:

 cmp al,61h
 je incre

 cmp cl,9
 je done  ; We're done. Jump past the end of the loop without incrementing bl
 jmp input

incre:
 inc bl

 cmp cl,9
 jne input
done:

或者更简单:

cmp al,61h
jne no_inc
inc bl
no_inc:
cmp cl,9
jne input