Technology
How to Write a 8086 Program to Check for Armstrong Numbers
Introduction to Armstrong Numbers and 8086 Programming
Armstrong numbers, also known as narcissistic numbers, are a fascinating class of numbers where the sum of the digits raised to the power of the number of digits equals the number itself. In this article, we will explore how to write an 8086 assembly program to check if a given number is an Armstrong number. We will break down the process, provide the necessary code, and explain the logic behind the algorithm.
Understanding Armstrong Numbers
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. For example, the number 153 is an Armstrong number because:
13 53 33 153
Algorithm to Check for Armstrong Numbers
Here is a step-by-step breakdown of the algorithm for checking an Armstrong number:
Start the program. Declare the variables. We will use ntemp, d, and rem for temporary storage and calculations. Read the number from the user. Store the value of n in a temporary variable for later use. Calculate the sum of the individual digits using a while loop. Check whether the number is an Armstrong number using an if condition. If true, print “The number is Armstrong.” If false, print “The number is not Armstrong.” End the program.8086 Assembly Program to Check for Armstrong Numbers
Here is the 8086 assembly code to implement the above algorithm:
; Preprocessor directive for utilities NAME_IMAGE ISARMSTRONG ; Main section _C0T_P_0 PROC _start mov bx, 0 ; Initialize BX to 0 for storing the temporary number _read_num mov ax, 0 ; Clear AX register _read_input mov ah, 01h ; Input function int 21h ; Call DOS interrupt sub al, 30h ; Convert ASCII to numerical value _temp_store mov bx, ax ; Store the input number in BX _sum_digits mov cx, 0 ; Initialize CX (number of digits) to 0 _loop_sum mov ax, cx ; Move count of digits to AX cmp ax, 0 ; Check if CX is still greater than 0 jle _exit_sum ; If not, exit the loop inc cx ; Increment the digit count _temp_proc mov ax, bx ; Move the temp value to AX mov dx, 0 ; Clear DX to prepare for division _divide mov cx, 10 ; Divisor is 10 _while_div div cx ; Divide AX by 10 mov bx, dx ; Store remainder in BX _sum_ax add al, 0001b ; Add remainder to the sum loop _while_div ; Repeat until all digits are processed _exit_sum mov ax, _temp_store ; Restore the original value of the input number _sum_original mov bx, al ; Store the sum of digits in BX _compare mov ax, _temp_store ; Move the original input to AX for comparison _check_arm strong cmp ax, bx ; Compare sum of cubes with the original number je _is_armstrong ; If equal, it's an Armstrong number jmp _is_not_armstrong _is_armstrong mov ah, 02h ; Move 1 to AX (ARMSTRONG) int 21h ; Output 1 jmp _end _is_not_armstrong mov ah, 02h ; Move 0 to AX (NOT ARMSTRONG) int 21h ; Output 0 _end ret ; End of program _read_num db '$', 0 ; Input buffer _C0T_P_0 ENDP END _C0T_P_0
Sample Outputs
Let's test the program with a few sample numbers:
Case 1:
Input number: 153
Output: 1 (The number is Armstrong)
Case 2:
Input number: 360
Output: 0 (The number is not Armstrong)
Case 3:
Input number: 912985153
Output: 1 (The number is Armstrong)
Case 4:
Input number: 912985152
Output: 0 (The number is not Armstrong)
These sample outputs show that the program correctly identifies Armstrong numbers and non-Armstrong numbers.
Conclusion
Writing an 8086 program to check for Armstrong numbers involves a combination of arithmetic operations, loops, and conditional checks. Understanding the algorithm and the corresponding assembly instructions is key to implementing this functionality. This program provides a clear and concise way to identify Armstrong numbers in an 8086 environment.
Happy coding!
-
How to Effectively Correct Errors and Engage with Content Creators
How to Effectively Correct Errors and Engage with Content Creators As a valued r
-
Why Dont More Penetration Testers Choose Bug Bounty Programs as Their Primary Source of Income?
Why Dont More Penetration Testers Choose Bug Bounty Programs as Their Primary So