Skip to main content

8086 ALP: multiplication table of a number

Short answer

The program reads a decimal number using READ_NUM (accumulates digits until Enter), then loops i=1 to 10, printing "N x i = N*i" using MUL and PRINT_DEC. Output example: for 7, it displays 7 x 1 = 7 through 7 x 10 = 70.

Write a program in 8086 to read a number and display the multiplication table of that number.

Method: READ_NUM reads a multi-digit decimal number (digits are accumulated as value = value × 10 + digit until Enter is pressed). The program then prints N x i = N*i for i = 1 to 10. MUL CX gives N × i in DX:AX, and PRINT_DEC prints any value in AX in decimal.

.MODEL SMALL
.STACK 100H

DISPSTR MACRO MSG            ; display a '$'-terminated string
        LEA  DX,MSG
        MOV  AH,09H
        INT  21H
        ENDM

CLRSCR  MACRO                ; clear screen and home the cursor
        MOV  AX,0600H        ; scroll whole window (AL=0 -> blank it)
        MOV  BH,07H          ; normal attribute
        MOV  CX,0000H        ; upper-left corner (row 0, col 0)
        MOV  DX,184FH        ; lower-right corner (row 24, col 79)
        INT  10H
        MOV  AH,02H          ; set cursor position
        MOV  BH,00H          ; page 0
        MOV  DX,0000H        ; row 0, col 0
        INT  10H
        ENDM

.DATA
    MSG1  DB 'Enter a number: $'
    TMS   DB ' x $'
    EQS   DB ' = $'
    CRLF  DB 13,10,'$'
    NUM   DW ?

.CODE
MAIN PROC
    MOV  AX,@DATA
    MOV  DS,AX

    DISPSTR MSG1
    CALL READ_NUM          ; AX = number
    MOV  NUM,AX
    DISPSTR CRLF
    MOV  CX,1              ; multiplier i = 1
TBL:
    MOV  AX,NUM
    CALL PRINT_DEC         ; N
    DISPSTR TMS          ; " x "
    MOV  AX,CX
    CALL PRINT_DEC         ; i
    DISPSTR EQS            ; " = "
    MOV  AX,NUM
    MUL  CX                ; DX:AX = N * i
    CALL PRINT_DEC         ; product
    DISPSTR CRLF
    INC  CX
    CMP  CX,10
    JBE  TBL               ; repeat for i = 1..10
    MOV  AH,4CH            ; return to DOS
    INT  21H
MAIN ENDP

; ---- read a decimal number from the keyboard, result in AX
READ_NUM PROC
    PUSH BX
    PUSH CX
    PUSH DX
    XOR  BX,BX             ; BX = value accumulated so far
RN1:MOV  AH,01H            ; read a character with echo
    INT  21H
    CMP  AL,0DH            ; Enter ends the number
    JE   RN2
    SUB  AL,'0'            ; ASCII -> digit
    XOR  AH,AH
    MOV  CX,AX             ; CX = digit
    MOV  AX,10
    MUL  BX                ; AX = value * 10
    ADD  AX,CX             ; add the new digit
    MOV  BX,AX
    JMP  RN1
RN2:MOV  AX,BX             ; return value in AX
    POP  DX
    POP  CX
    POP  BX
    RET
READ_NUM ENDP

; ---- print the unsigned number in AX in decimal (registers preserved)
PRINT_DEC PROC
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX
    MOV  BX,10
    XOR  CX,CX             ; digit counter
PD1:XOR  DX,DX
    DIV  BX                ; AX = quotient, DX = remainder (digit)
    PUSH DX
    INC  CX
    OR   AX,AX
    JNZ  PD1
PD2:POP  DX                ; digits come out in correct order
    ADD  DL,30H            ; to ASCII
    MOV  AH,02H
    INT  21H
    LOOP PD2
    POP  DX
    POP  CX
    POP  BX
    POP  AX
    RET
PRINT_DEC ENDP

END MAIN

Output for 7: 7 x 1 = 7 ... 7 x 10 = 70.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 3 questions →