The assembler can count the length of a string for you: LEN EQU $ - MSG is the number of bytes between the start of MSG and the current position $. A name defined with EQU takes no memory.
What to build
Read a digit n and print the message StepVioz split after its first n characters, with a bar | between the two parts.
Requirements
- Define the message
StepViozwith DB and its length with EQU. - Print the first
ncharacters, then|, then the other characters. - When
nis 0 the first part is empty, and whennis 8 the second part is empty.
Input
One digit n, from 0 to 8, then Enter.
Output
One line, such as Step|Vioz for n = 4.
Constraints
0 ≤ n ≤ 8.
Example
Input:
4
Output:
Step|Vioz
Why it works
SI walks through the message from offset 0, and the same loop is used twice, with a different count each time: n characters for the first part, and LEN - n for the second. JCXZ guards against a count of 0, because LOOP with CX = 0 would repeat 65,536 times.
Examples
4
Step|Vioz
0
|StepVioz
Hints
Hint 1
Define MSG DB 'StepVioz' and LEN EQU $ - MSG right after it: the assembler counts the 8 characters for you.
Hint 2
Print the first n characters with a loop that reads MSG[SI] and increases SI. Use JCXZ so that n = 0 prints nothing.
Hint 3
Print the bar, then the remaining LEN - n characters with the same kind of loop.
Solution
Show one correct solution (try it yourself first)
; Split a message
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 'StepVioz'
LEN EQU $ - MSG
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 1
INT 21H
SUB AL, '0'
MOV AH, 0
MOV BX, AX ; n
MOV SI, 0
MOV CX, BX ; first part: n characters
JCXZ BAR
FIRST:
MOV DL, MSG[SI]
MOV AH, 2
INT 21H
INC SI
LOOP FIRST
BAR:
MOV DL, '|'
MOV AH, 2
INT 21H
MOV CX, LEN
SUB CX, BX ; second part: LEN - n characters
JCXZ DONE
SECOND:
MOV DL, MSG[SI]
MOV AH, 2
INT 21H
INC SI
LOOP SECOND
DONE:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
▶ Open this solution in the compiler, then press Visualize to watch it run step by step