A loop can make a number grow by adding the same amount at every step. Here each step adds n, so the numbers are the multiples of n.
What to build
Read a digit n and print the first nine multiples of n on one line, separated by spaces.
Requirements
- Print
n × 1,n × 2, up ton × 9. - Separate the numbers with one space, with no space at the end.
- Print each number in decimal without leading zeros.
Input
One digit n from 1 to 9, then Enter.
Output
One line, such as 3 6 9 12 15 18 21 24 27 for n = 3.
Constraints
1 ≤ n ≤ 9.
Example
Input:
3
Output:
3 6 9 12 15 18 21 24 27
Why it works
The biggest number is 9 × 9 = 81, so every multiple fits in a byte and below 100, and AAM can split it in a single instruction. Adding n at every step avoids the multiplication altogether, and a loop of nine steps with CX = 9 prints the whole line.
Examples
3
3 6 9 12 15 18 21 24 27
1
1 2 3 4 5 6 7 8 9
Hints
Hint 1
Keep n in BL and the multiple in BH. Start BH at n and add n at every step: ADD BH, BL.
Hint 2
A multiple is at most 81, so AAM splits it into its tens (AH) and ones (AL). Print the tens digit only if it is not 0.
Hint 3
Print a space between numbers but not after the last one: print the space before every number except the first (for instance by testing whether the multiple still equals n).
Solution
Show one correct solution (try it yourself first)
; A row of the times table
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 1
INT 21H
SUB AL, '0'
MOV BL, AL ; n
MOV BH, AL ; the multiple
MOV CX, 9
NEXT:
CMP BH, BL
JE NOSPACE ; the first multiple has no space before it
MOV DL, ' '
MOV AH, 2
INT 21H
NOSPACE:
MOV AL, BH
MOV AH, 0
AAM ; AH = tens, AL = ones
MOV DX, AX
CMP DH, 0
JE ONES
PUSH DX
ADD DH, '0'
MOV DL, DH
MOV AH, 2
INT 21H
POP DX
ONES:
ADD DL, '0'
MOV AH, 2
INT 21H
ADD BH, BL
LOOP NEXT
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