A new-line procedure

A procedure is a piece of code that you write once and run from several places with CALL. RET brings the program back to the instruction after the CALL.

What to build

Read three keys and print each one on its own line, using a procedure for the new line.

Requirements

  • Write a procedure called NEWLINE and call it three times.
  • Print the first key, a new line, the second key, a new line, and so on.
  • The procedure must leave the registers as it found them.

Input

Three characters, then Enter.

Output

Three lines, one character each.

Constraints

The characters are letters or digits.

Example

Input:

abc

Output:

a
b
c

Why it works

CALL pushes the address of the next instruction and RET pops it again, so the same three instructions can be used from anywhere in the program. The procedure saves and restores AX and DX, which it changes, so the main program can keep using them as if nothing had happened.

Examples

Example 1 · Input
abc
Expected output
a
b
c
Example 2 · Input
123
Expected output
1
2
3

Hints

Hint 1

Write a procedure NEWLINE that prints 0Dh and 0Ah, and ends with RET. Put it after MAIN ENDP.

Hint 2

A procedure that changes AX and DX should save them with PUSH and restore them with POP, in reverse order, so the caller does not lose its values.

Hint 3

Read each key into BL, print it, and CALL NEWLINE.

Solution

Show one correct solution (try it yourself first)
; A new-line procedure
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
    MOV CX, 3
AGAIN:
    MOV AH, 1
    INT 21H
    MOV DL, AL
    MOV AH, 2
    INT 21H             ; print the key
    CALL NEWLINE
    LOOP AGAIN
    MOV AH, 4CH
    INT 21H
MAIN ENDP

NEWLINE PROC
    PUSH AX             ; save what the caller had
    PUSH DX
    MOV AH, 2
    MOV DL, 0DH
    INT 21H
    MOV DL, 0AH
    INT 21H
    POP DX              ; restore in reverse order
    POP AX
    RET
NEWLINE ENDP
END MAIN

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Procedures: CALL and RET →