A table of squares

A table in memory can hold answers that are already worked out. A register that holds the index picks the answer, which is often faster than calculating it.

What to build

Read a digit d and print d × d, by looking it up in a table of squares.

Requirements

  • Define a table of the ten squares in the data segment.
  • Use the digit as an index into the table.
  • Print the result in decimal without a leading zero.

Input

One digit, then Enter.

Output

One number, from 0 to 81.

Constraints

0 ≤ d ≤ 9.

Example

Input:

7

Output:

49

Why it works

SQ[BX] is the byte at the address of SQ plus BX, which is the element number BX. The table replaces a multiplication with one memory read. Printing a number below 100 needs AAM to get the tens digit and the ones digit, and the tens digit is left out when it is 0.

Examples

Example 1 · Input
7
Expected output
49
Example 2 · Input
0
Expected output
0

Hints

Hint 1

Define the table: SQ DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81.

Hint 2

Read the digit, subtract '0', clear AH, and move AX to BX. Then MOV AL, SQ[BX] reads the square.

Hint 3

A square below 10 is printed as one digit. Otherwise AAM splits it into tens and ones. Test for a tens digit of 0 and skip it.

Solution

Show one correct solution (try it yourself first)
; A table of squares
.MODEL SMALL
.STACK 100H
.DATA
    SQ DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
    MOV AH, 1
    INT 21H
    SUB AL, '0'
    MOV AH, 0
    MOV BX, AX
    MOV AL, SQ[BX]      ; the square
    AAM                 ; AH = tens, AL = ones
    MOV BX, AX
    CMP BH, 0
    JE ONES
    MOV DL, BH
    ADD DL, '0'
    MOV AH, 2
    INT 21H
ONES:
    MOV DL, BL
    ADD DL, '0'
    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

Lesson: Arrays and addressing modes →