8086 assembly language nested loop edit button Edit

Murugan Andezuthu Dharmaratnam | calendar 10 February 2021 | 5911

in this article, we will understand how to create a nested loop in 8086. which means a loop inside another loop. We will try to print * character 10 times in the first line then print the * 10 times in the next 6 lines. I started writing this article when someone asked for help on how to display an inverted pyramid using * character.

Problem Statement

We are trying to print some characters on the first line using a loop and then using a nested loop we will print the characters 6 times.


Sample code to display rectangle using * character

.model tiny
org 100h

main proc near

mov cx,6
loop2:  ; Loop 2 outer loop
push cx ; you have to push the cx and pop it out before loop instruction to prevent the cx being over written by the inner loop cx 
    mov cx,10
    loop1:  ; inner loop
        mov ah,02h  ; Dos function 02h to Write character to STDOUT
        mov dl,'*'  ; character to write
        int 21h     ; dos interrupt 21h
    loop loop1

; Code to print in next line    
    mov ah,02h
    mov dl,0ah      ; carriage return
    int 21h
    mov ah,02h
    mov dl,0dh      ; line feed
    int 21h
pop cx
loop loop2

mov ah,4ch  ; Dos function to terminate the return to dos
mov al,00   ; return code
int 21h     ; dos interrupt 21

end main