Daniel Moreno
Daniel Moreno

Reputation: 128

Print the number of each character inside a string in assembly

Hey guys im trying to learn assembly and im having this issue right now, so im trying to get the number each characters inside a string.

for example if I type in "hi" it should output: a0 b0 .. h1 i1 .. z0

but instead I get random chars, I'll apreciate any help.

 data segment
            msg1 db 10,13,': $'
            msg3 db 10,13,' $'
            msg4 db 10,13,'no, character found in the given string $'
            msg5 db ' character(s) found in the given string $'
            char db 97
            count db 0
            p1 label byte
            m1 db 0ffh
            l1 db ?
            auxsi db 0
            p11 db 0ffh dup ('$')
        data ends
        display macro msg
            mov ah,9
            lea dx,msg
            int 21h
        endm
        code segment
            assume cs:code,ds:data
        start:
                mov ax,data
                mov ds,ax

                display msg1

                lea dx,p1
                mov ah,0ah
                int 21h



                lea si,p11


                mov cl,l1
            mov ch,0

    check:
            mov al,[si]
            cmp char,al
            jne skip
            inc count
    skip:
            inc si
            loop check

            cmp count,0
            je notfound



            display msg3

            mov ah,02h
            mov dl,char
            int 21h


            mov dl,count
            add dl,30h
            mov ah,2
            int 21h

            inc char
            mov count,0

            cmp char,123
            jne check

            jmp exit
    notfound:
            display msg4

    exit:   mov ah,4ch
            int 21h
    code ends
    end 

start

Upvotes: 2

Views: 612

Answers (1)

Jester
Jester

Reputation: 58822

For starters, it will never print zeroes since you have explicit check to not do that in the code. Is this even your own code? Also, that very check will stop at the first character that's not present, so if your string does not start with an a it will stop immediately. Furthermore, after printing a count, you loop back to check which does not reload cx or si so it will continue after the buffer. Finally, the apparently unused auxsi label in the buffer will make the code skip the first character of the input.

Learn to use a debugger and comment your code especially if you want others to help.

Upvotes: 1

Related Questions