Search code examples
assemblytasm

assembly language count all 'a' in character input


my code supposed to count all character 'a' in every user input i use cmp if equal then my program jump to 'incre:' that increment the value of bl.the output is always this>¶< .i don't know where the problem is

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

ENTER 9 CHARACTERS:aaadfasfg
NUMBER OF a: ¶

ENTER 9 CHARACTERS:fffffffff
NUMBER OF a: ¶

ENTER 9 CHARACTERS:dasdawdaf
NUMBER OF a: ¶


Solution

  • You've got a typo in your code:

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

    The character should be placed in dl, not in dh.

    Another problem is that you're incrementing bl one time too many:

     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
    

    That should be changed to something like:

     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:
    

    Or even simpler:

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