1

perhaps this is a question that has already been answered. I am trying to create a parallelogram pattern in assembly with hearts, it works but sadly whenever I try to add the spaces between the characters; most of them disappear. I'm thinking that it's because of the CL register or maybe I have overlooked some parts. I have tried different trials and errors to get the expected output. Unfortunately, I am still a beginner in this push-and-pop method so please bear with the comments. Also, this was a code for a right triangle that I've tweaked to become a parallelogram.

I emulated this using TASM-TLINK on DOSBOX.

Here's the code below:

.MODEL TINY
.386
.DATA
.CODE
    ORG 100H

BEGIN:
    MOV BL,4
    MOV CL,4
    
    ;    * * * *
    ;   * * * *
    ;  * * * *          ; THE EXPECTED PATTERN
    ; * * * *
    
    ;PUSH BX 
    
    L1:
    PUSH CX
    MOV AH,2
    MOV DL,20H ;INCREMENT SPACES
    
    ;SPACE:
    ;MOV AH,02
    ;MOV DL,20H
    ;INT 21H
    
    ;LOOP SPACE 
    ;POP CX 
       
    L2:
     INT 21H
     LOOP L2 
     
     MOV CL,BL 
     DEC CL
     MOV AH,2 ; PRINT THE CHARACTERS
     MOV DL,03H
     INT 21H
     ;MOV AH,02
     ;MOV DL,20H
     ;INT 21H
     
    L3:
     INT 21H
     LOOP L3 
     
     MOV AH,2 ; GENERATE NEW LINES
     MOV DL,10
     INT 21H
     MOV DL,13
     INT 21H

    DEC BH ; DECREMENT BH REGISTER TO TRIM PARALLELOGRAM 
    ;INC BH?
    POP CX
    ;PUSH BX 
    
    LOOP L1
       
    
    EXIT:
    MOV AH,4CH
    INT 21H
    INT 20H

END BEGIN

Thank you to anyone who takes the time to answer this. All good wishes!

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Serena
  • 23
  • 4
  • `DEC BH` should of course be `DEC BL`. Also the `POP CX` will overwrite the decremented `CX` value so that needs to go (along with the `PUSH CX`). There may be more errors, the code is hard to follow. – Jester Jul 14 '22 at 14:21
  • 2
    Thank you so much! I had to reorganize most of the code actually and finally was able to put spaces in between the characters. I had push through a lot of infinite loops just to finally get the one that I've needed. Thank you for the hint. – Serena Jul 14 '22 at 15:49

1 Answers1

1

Some fine points

MOV BL,4
MOV CL,4

Because your program will be using the loop instruction that depends on the whole 16 bits of the CX register, you should setup via mov cx, 4 and not rely on your environment starting from an emptied register!

DEC BH ; DECREMENT BH REGISTER TO TRIM PARALLELOGRAM 
;INC BH?

You don't need this instruction at all. There is nothing to trim here! The dimension of the parallelogram that you have set in the BL register remains constant.

EXIT:
MOV AH,4CH
INT 21H
INT 20H

The mov ah, 4Ch int 21h DOS.TerminateWithReturncode already ended your program. The int 20h DOS.TerminateProgram will never execute and so is totally useless here.


Your comment indicates that you have succesfully rewritten your program. Nonetheless, please take a look at my solution below:

    MOV  AH, 02h   ; DOS.PrintCharacter
    MOV  BX, 4     ; Dimension of the parallelogram, 2 or more
    MOV  CX, BX    ; Leading whitespace
L1: PUSH CX        ; (1)
    MOV  DL, ' '
L2: INT  21h
    DEC  CX
    JNZ  L2

    MOV  DL, 3     ; Heart
    INT  21h
    LEA  CX, [BX-1]
L3: MOV  DL, ' '
    INT  21h
    MOV  DL, 3     ; Heart
    INT  21h
    DEC  CX
    JNZ  L3

    MOV  DL, 10
    INT  21h
    MOV  DL, 13
    INT  21h

    POP  CX        ; (1)
    DEC  CX
    JNZ  L1

So you don't have to un-learn it latter, you refrain from using the loop instruction. See Why is the loop instruction slow? Couldn't Intel have implemented it efficiently??

Expected output (4 spaces start first line):

    ♥ ♥ ♥ ♥
   ♥ ♥ ♥ ♥
  ♥ ♥ ♥ ♥
 ♥ ♥ ♥ ♥
Sep Roland
  • 33,889
  • 7
  • 43
  • 76