RendoJack
RendoJack

Reputation: 366

Assembly 8086: problems with counter

I want to output a string without using a $ sign. Whether I get it right, i need to count the characters of my string and put the value in cx register. I tried to do so, but the program simply prints the whole buffer out and not the string I need.

This is Turbo assembler, btw.

.model small
    .stack 200h             
    .data
                text1   db 0ah, 0dh, 'Your text  :  $'      
                text2   db 0ah, 0dh, 'Text in lowercase:  $'
                string  db 255 dup(?)
    .code

    begin:      
            mov ax, @data   
            mov ds, ax

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

            lea si, string  
            mov ah, 01h     

    read:       
            int 21h 

            cmp al, 0dh     
            je print

            cmp al, 'A'      
            jl not_upper    

            cmp al, 'Z'     
            jg not_upper    

            add al, 20h     
            mov [si], al
            inc si          

            ;****************************
            inc cx
            ;****************************

            jmp read        

    not_upper:
            mov [si], al    
            inc si
            jmp read        

    print:
            mov al, 00h 
            mov [si], al

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

            ;****************************
            lea dx, string
            mov ah, 40h
            mov bx, 1
            int 21h
            ;****************************   

            mov ah, 4ch     
            int 21h

        .exit

    end begin

Upvotes: 3

Views: 1202

Answers (1)

Sep Roland
Sep Roland

Reputation: 39256

  • You forgot to clear the CX register prior to using it as a counter.
  • You only increment the counter when you convert an uppercase to lowercase but you forget to increment it when you store the other characters in the buffer.
  • You should always treat comparisons for ASCII codes as unsigned. Better change jl/jg into jb/ja.
  • At the label print you terminate the buffer with a NULL. This is not needed since you want to use a counter to quantify the contents.

Upvotes: 3

Related Questions