TechTorch

Location:HOME > Technology > content

Technology

Writing an 8086 Assembly Language Program to Check for Strong Numbers

May 14, 2025Technology2243
Writing an 8086 Assembly Language Program to Check for Strong Numbers

Writing an 8086 Assembly Language Program to Check for Strong Numbers

Understanding Strong Numbers

A strong number is a number where the sum of the factorials of its digits equals the number itself. For example, 145 is a strong number because 1! 4! 5! 1 24 120 145. This article will guide you through writing an 8086 assembly language program to check if a given number is a strong number, using the console as the input source.

Steps to Write the Program

Input the number: Read a number from the console. Extract digits: Extract each digit of the number. Calculate factorials: Calculate the factorial of each digit. Sum the factorials: Sum all the factorials of the digits. Compare the sum: Check if the sum equals the original number.

Sample 8086 Assembly Program

Below is a simple implementation of the above logic in 8086 assembly language:

#35; Model small
.stack 100h
.data
    num db 5 dup(0)        ; Buffer to store the input number
    msg db 'Enter a number:#39;, 0Dh, 0Ah 
    result_msg db 'The number is a strong number.', 0Dh, 0Ah
    not_strong_msg db 'The number is not a strong number.', 0Dh, 0Ah
    newline db 13, 10
.code
main proc
    mov ax, @data
    mov ds, ax
    ; Prompt for input
    lea dx, msg
    mov ah, 09h
    int 21h
    ; Read the number
    lea dx, num
    mov ah, 0Ah
    int 21h
    ; Convert input string to number
    mov si, offset num   1   ; Point to the first digit
    xor cx, cx                ; Clear digit counter
    mov cl, [num]             ; Number of digits
    xor ax, ax                ; Clear AX for the number
    .convert_loop:
        movzx bx, byte ptr [si]  ; Get digit
        sub bx, '0'              ; Convert from ASCII to integer
        mov dx, 10               ; Multiply by 10
        mul dx                   ; AX  AX * 10
        add ax, bx               ; AX  digit
        inc si                   ; Move to next digit
        loop .convert_loop
    ; Store original number
    mov bx, ax                ; BX  original number
    xor cx, cx                ; Clear CX for factorial sum
    ; Calculate sum of factorials
    lea si, num   1           ; Point to first digit
    .factorial_loop:
        movzx bx, byte ptr [si]  ; Get digit
        sub bx, '0'              ; Convert from ASCII to integer
        call factorial           ; Calculate factorial
        add cx, ax               ; Sum the factorials
        inc si                   ; Move to next digit
        cmp si, num   1          ; Check if we reached the end
        jl .factorial_loop
    ; Compare the sum of factorials with the original number
    cmp cx, bx                ; Compare sum with original number
    je .is_strong            ; If equal, it's a strong number
    ; Output not strong message
    lea dx, not_strong_msg
    mov ah, 09h
    int 21h
    jmp .done
    .is_strong:
    ; Output strong message
    lea dx, result_msg
    mov ah, 09h
    int 21h
    .done:
    ; New line
    lea dx, newline
    mov ah, 09h
    int 21h
    ; Exit program
    mov ax, 4C00h
    int 21h
factorial proc
    ; Input: BX  digit (0-9)
    ; Output: AX  factorial of BX
    mov ax, 1                  ; AX  factorial result
    cmp bx, 0
    je .factorial_done
    .factorial_loop:
        mul bx                  ; AX  BX
        dec bx                  ; BX--
        jnz .factorial_loop
    .factorial_done:
    ret
factorial endp
main endp
end main

Explanation of the Program

The program comprises several main components:

Data Segment: Declares strings for messages and a buffer to store the input number. Input Handling: Prompts the user for a number and reads it, converting it from a string to an integer. Factorial Calculation: Calculates the factorial of each digit using a separate factorial procedure. Sum Calculation: Sums the factorials of the digits and compares the sum with the original number. Output: Depending on the comparison, it prints whether the number is a strong number or not.

Running the Program

To run this program, you will need an 8086 emulator or assembler like MASM or TASM. Assemble the code, link it, and run the resulting binary. The program will prompt you to enter a number and then determine if it is a strong number.

About the Keywords

The keywords for this article include 8086 assembly, which refers to the assembly language used for programming the Intel 8086 microprocessor, and strong numbers, which are numbers where the sum of the factorials of its digits equals the number itself. The third keyword is factorial calculation, which plays a crucial role in determining if a number is a strong number.