A string defined with DB is a table of bytes in memory: the first character is at offset 0, the second at offset 1, and so on. A register holding an offset picks one entry.
What to build
Read a digit and print the letter at that position of the table ZYXWVUTSRQ.
Requirements
- The table is
ZYXWVUTSRQ; position 0 isZand position 9 isQ. - Read the digit with service 1 and turn it into a number by subtracting
'0'. - Print the one letter at that position.
Input
One digit, then Enter.
Output
One letter.
Constraints
The digit is from 0 to 9.
Example
Input:
0
Output:
Z
Why it works
A variable name followed by a register in brackets, such as LETTERS[BX], means the byte at the address of LETTERS plus BX. Because the digit is a number of bytes from the start, it works directly as an offset after the character code is turned into its value.
Examples
0
Z
9
Q
Hints
Hint 1
Define the table as bytes in the data segment: LETTERS DB 'ZYXWVUTSRQ'.
Hint 2
Read the digit and subtract '0' to get its value, then clear the high byte (MOV AH, 0) and copy AX into BX.
Hint 3
The d-th byte of the table is LETTERS[BX]: load it into DL and print it.
Solution
Show one correct solution (try it yourself first)
; A table in the data segment
.MODEL SMALL
.STACK 100H
.DATA
LETTERS DB 'ZYXWVUTSRQ'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AH, 1
INT 21H
SUB AL, '0'
MOV AH, 0
MOV BX, AX ; BX = the digit as a number
MOV DL, LETTERS[BX] ; the byte at LETTERS + BX
MOV AH, 2
INT 21H
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