Search code examples
assemblyx86dosx86-16

How can i print new line 3 times by using new line code only once in-spite of typing same code 3 times


How can i print new line 3 times by using new line code only once in-spite of typing same code 3 times

include emu8086.inc

ORG    100h

    PRINT 'ENTER THREE INITIALS: '

    MOV AH,1
    INT 21H

    MOV BL,AL
    INT 21H
    MOV CL,AL
    INT 21H
    MOV BH,AL

   MOV AH,2
    MOV DL,10
    INT 21H     ;NEW LINE
    MOV DL,13
    INT 21H   

    MOV AH,2

    MOV DL,BL

    INT 21h

    MOV AH,2
    MOV DL,10
    INT 21H     ;NEW LINE
    MOV DL,13
    INT 21H


    MOV DL,CL

    INT 21h 

    MOV AH,2
    MOV DL,10
    INT 21H     ;NEW LINE
    MOV DL,13
    INT 21H

    MOV DL,BH

    INT 21h

   RET               
END  

Solution

  • create first a subroutine/function which you can call from main code, for example after your main code put this:

    PRINT_NEW_LINE:
        MOV AH,2
        MOV DL,10
        INT 21H     ;NEW LINE  (that's really amazing comment... not)
        MOV DL,13   ; and now I realized you do 10,13 output
        INT 21H     ; but correct DOS <EOL> is 13,10
        RET         ; time to fix all that in next version below...
    

    And now I will use some ugly trickery to create also 2x and 3x variants not just by simply calling the subroutine above, but letting the CPU to fall through its code, try it in debugger how it works (and what return addresses in stack do), then the whole new subroutines code will be:

    PRINT_NEW_LINE_THRICE:
        CALL PRINT_NEW_LINE ; do 1x EOL, and then fall into "twice" code
    PRINT_NEW_LINE_TWICE:
        CALL PRINT_NEW_LINE ; do 1x EOL, and then fall into it again
    PRINT_NEW_LINE:
        PUSH AX
        PUSH DX     ; store original ax, dx values
        MOV AH,2
        MOV DL,13
        INT 21H     ; output NL (new line)
        MOV DL,10
        INT 21H     ; output CR (carriage return)
        POP DX      ; restore original ax, dx value
        POP AX
        RET
    

    Now in your main code simply do:

        CALL PRINT_NEW_LINE_THRICE
    

    to get 3x new line outputted.


    The less confusing and tricky variant of "thrice" subroutine would be of course:

    PRINT_NEW_LINE_THRICE:
        CALL PRINT_NEW_LINE
        CALL PRINT_NEW_LINE
        CALL PRINT_NEW_LINE
        RET