hasanzahidshuvo
hasanzahidshuvo

Reputation: 1

How to print 10 to 19 in the console using loop instruction in assembly language?

I can print 0 to 9 using loop but unable to print 10 to 19. How do I do that?

I used this code to print 0 to 9:

.model small
.stack 100h
.code
main proc
    
    mov cx, 10
    mov dl, '0'
    
    lbl1:
        mov ah, 2
        int 21h
        inc dl
        loop lbl1

    main endp
end main

Upvotes: 0

Views: 1498

Answers (2)

Sep Roland
Sep Roland

Reputation: 39191

If all you need is printing the numbers 10 to 19, then Jester's suggestion to prepend the "1" character is excellent. Care will have to be taken because outputting that "1" character will clobber your loop variable in the DL register.

    mov  cx, 10
    mov  dl, '0'
lbl1:

*   push dx        ; Preserve our loop variable
*   mov  dl, "1"
*   mov  ah, 02h   ; DOS.PrintCharacter
*   int  21h
*   pop  dx        ; Restore our loop variable

    mov  ah, 02h   ; DOS.PrintCharacter
    int  21h
    inc  dl
    loop lbl1

If you print the numbers above each other, it will be nicer to look at. We add a carriage return and linefeed pair to the code:

    mov  cx, 10
    mov  dl, '0'
lbl1:
    push dx        ; Preserve our loop variable
    mov  dl, "1"
    mov  ah, 02h   ; DOS.PrintCharacter
    int  21h
    pop  dx        ; Restore our loop variable
    mov  ah, 02h   ; DOS.PrintCharacter
    int  21h

*   push dx        ; Preserve our loop variable
*   mov  dl, 13    ; Carriage return
*   mov  ah, 02h   ; DOS.PrintCharacter
*   int  21h
*   mov  dl, 10    ; Linefeed
*   mov  ah, 02h   ; DOS.PrintCharacter
*   int  21h
*   pop  dx        ; Restore our loop variable

    inc  dl
    loop lbl1

Upvotes: 1

0x777C
0x777C

Reputation: 1047

You'll need to implement a full integer to string conversion algorithm such as: https://codereview.stackexchange.com/a/142910/178169

Current you're simply printing all the numeric ASCII characters, of which only 0-9 exist. Instead you need to combine them to form a string of characters that corresponds to the number you want to print.

Upvotes: 0

Related Questions