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

时间:2015-01-27 07:54:16

标签: assembly tasm

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

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个字符:aaadfasfg
数字:¶

输入9个字符:fffffffff
数字:¶

输入9个字符:dasdawdaf
数字:¶

1 个答案:

答案 0 :(得分:2)

您的代码中出现了拼写错误:

 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
相关问题