Having trouble in figuring out how to properly loop in DOSBox

32 Views Asked by At

I am trying to display:

A123*B123*C123*

in DOSBox using loop but instead I'm getting:

A1*2*3*B*1*2*3*C*1*2*3*D

Here is my current code:

.model small
.stack
.data

.code
main    proc
        mov cx,3
        mov ah, 2
        mov dl, 'A'
Letter: int 21h
        push cx
        push dx
        mov cx, 3
        mov ah, 2
        mov dl, '1'
Number: int 21h
        push cx 
        push dx
        mov ah, 2
        mov cx, 1
        mov dl, '*'
Aste:   int 21h
        Loop Aste
        pop dx
        pop cx
        add dl,1
        Loop Number
        pop dx
        pop cx
        add dl, 1
        Loop Letter
        int 21h

        mov ah,4ch
        int 21h
main    endp
end     main

I tried switching the way I would run the loops but whenever I put the loop "aste" at the end it will print '*' nonstop.

1

There are 1 best solutions below

0
Sep Roland On
        push cx
        push dx
        mov ah, 2
        mov cx, 1
        mov dl, '*'
Aste:   int 21h
        Loop Aste
        pop dx
        pop cx

Why would you create a loop if all you need to output is a single character? It also made you write these annoying pushes and pops.

Solve the task from the inside out

The task requires the use of two loops. We will call these 'nested loops' as the 'inner loop' will be embedded in the 'outer loop'.

Begin with the inner loop that simply prints 123

  mov  dl, '1'       ;
InnerLoop:           ;
  mov  ah, 02h       ;
  int  21h           ;
  inc  dl            ;
  cmp  dl, '4'       ;
  jb   InnerLoop     ;
  • Below the inner loop we print the single character *
  mov  dl, '1'
InnerLoop:
  mov  ah, 02h
  int  21h
  inc  dl
  cmp  dl, '4'
  jb   InnerLoop

  mov  dl, '*'       ;
  mov  ah, 02h       ;
  int  21h           ;
  • Above the inner loop we print the single character A that we retrieve from an available register like BL
  mov  dl, bl        ; BL is one of 'A', 'B', or 'C'
  mov  ah, 02h       ;
  int  21h           ;

  mov  dl, '1'
InnerLoop:
  mov  ah, 02h
  int  21h
  inc  dl
  cmp  dl, '4'
  jb   InnerLoop

  mov  dl, '*'
  mov  ah, 02h
  int  21h

Now we enclose this by the outer loop

  • At the top we initialize the BL register to A
  mov  bl, 'A'       ;
OuterLoop:           ;

  mov  dl, bl
  mov  ah, 02h
  int  21h

  mov  dl, '1'
InnerLoop:
  mov  ah, 02h
  int  21h
  inc  dl
  cmp  dl, '4'
  jb   InnerLoop

  mov  dl, '*'
  mov  ah, 02h
  int  21h
  • At the bottom we put our iteration logic that increments the BL register and that repeats the outer loop for the characters B and C
  mov  bl, 'A'
OuterLoop:

  mov  dl, bl
  mov  ah, 02h
  int  21h

  mov  dl, '1'
InnerLoop:
  mov  ah, 02h
  int  21h
  inc  dl
  cmp  dl, '4'
  jb   InnerLoop

  mov  dl, '*'
  mov  ah, 02h
  int  21h

  inc  bl            ;
  cmp  bl, 'D'       ;
  jb   OuterLoop     ;

Insert it in your skeleton program and enjoy the view!

A123*B123*C123*