Search code examples
loopsassemblyx86-16emu8086

Printing a triangle with incrementing digits instead of asterisks


I'm experimenting with my program by trying to build different kinds of pyramids and converting the values into different values. I managed to build one with asterisk sign and now I'm trying to find a way on how to change it into running numbers like "0123456789" and the next line is "012345678" and so on. Is there a way to do that without fully/less changes of my code?

.MODEL SMALL
.STACK 100H
.DATA
STAR DB ?
BLANK DB ?
.CODE 

MAIN PROC
      MOV AX,@DATA
      MOV DS,AX

      MOV CX,10
      MOV BH,10
      MOV BL,0

      MOV STAR,BH
      MOV BLANK,BL

      L1:
      CMP BLANK,0
      JE L2

      MOV AH,2
      MOV DL,32
      INT 21H
      DEC BLANK

      JMP L1

      L2:
      MOV AH,2
      MOV DL,'*'
      INT 21H
      DEC STAR
      CMP STAR,0
      JNE L2

      MOV AH,2
      MOV DL,0AH
      INT 21H
      MOV DL,0DH
      INT 21H
      DEC BH    
      MOV STAR,BH
      INC BL    
      MOV BLANK,BL  
      LOOP L1

      EXIT:
      MOV AH,4CH    
      INT 21H

MAIN ENDP
END MAIN

Solution

  • I'm trying to find a way on how to change it into running numbers like "0123456789" and the next line is "012345678" and so on. Is there a way to do that without fully/less changes of my code?

    While the other answer provides an alternative solution, my solution keeps most of your program intact as per request, only adding the digit sequence:

    L2:
      mov dl, '0'            <--- Setup 1st digit
    L3:                      <--- Extra label
      MOV AH,2
      INT 21H
      inc dl                 <--- To next digit
      DEC STAR
      CMP STAR,0             ; Tip: you can remove this instruction 
      JNE L3                 <--- To extra label
    

    Expected output (similar to your asterisks):

    0123456789
     012345678
      01234567
       0123456
        012345
         01234
          0123
           012
            01
             0