Search code examples
assemblyx86asciidosbios

Assembly increment and loop


I want to know how to display an output like this using loop and incrementation using ASCII codes.

Aa Bb Cc ... Zz

This is my working code below, and it seemss, there is / are many mistakes. It becomes an endless loop.

Please check and help me.

Thanks.

.model small
.stack 200h
.code

main proc

    mov ah, 0      
    mov al, 12h   ; Clear screen
    int 10h

    mov ah,3    
    mov bh,0     ; get cursor
    int 10h

    mov ah,2      
    mov bh,0      ;set cursor
    mov dl,12
    int 10h


    mov cx, 5      ; counter


t1:


    Mov dl, 65    ; A   
    mov ah, 2h
    int 21h

    add dl, 1


t2:

    Mov dl, 97    ; a
    mov ah, 2h
    int 21h

    add dl,1

loop t2

loop t1

    mov ah, 4ch
    mov al,00h
    int 21h



endp
end main

Solution

  •     mov cx, 5      ; counter
        Mov dl, 65    ; A   
    
    t1:
        mov ah, 2h
        int 21h
    
        add dl, 32    ; 97 - 65 - convert to LC
    
        mov ah, 2h
        int 21h
    
        sub dl,31     ;remove the 32 added, but increment
        push dx       ;save DX on stack
        mov dl, 32    ;space character
    
        mov ah, 2h
        int 21h
    
        pop DX        ;return DX from stack
    
        loop t1
    

    [Amended in the light of Michael's comment - Add dl,1 became sub dl,31]

    (I've omitted your initialisation and termination which should be fine)

    Your issues are:

    t1..t2 : load DL with 'A' and output it; then increment
    t2..loop t2 instruction: load DL with 'a' and output it; then increment
    - do this 5 (contents of CX) times. Note you are loading DL with 'a' each time
    - AND that CX will be decremented each loop, so the loop terminates when CX BECOMES 0
    loop t2: Next, loop back to t1 and repeat until CX BECOMES 0.

    So, at the loop t1, CX is already 0, and is thus decremented and the program loops back to t1, so A is output until CX once again becomes 0, 65534 loops later.