An x86 Assembly program that demonstrates the iterative process of reaching Kaprekar's constant (6174) from any four-digit number.
This project showcases an x86 Assembly implementation that takes a four-digit number and applies Kaprekar's routine until it converges to the constant 6174. The program also tracks the number of iterations required to reach this constant.
Kaprekar's routine involves the following steps:
- Arrange the digits of the number in descending and ascending order to form two new numbers.
- Subtract the smaller number from the larger number.
- Repeat the process with the result until the number 6174 is obtained.
For example, starting with the number 3524:
- 5432 (descending) - 2345 (ascending) = 3087
- 8730 - 378 = 8352
- 8532 - 2358 = 6174
Kaprekar/
βββ README.md # Project documentation
βββ DATA.TXT # Output file: the number of iterations for each 4-digit number
βββ maclib.asm # Assembly macros
βββ main.asm # Main program logic
βββ proclib.asm # Procedure library- Assembler: NASM
- Debugger/Emulator: DOSBox or EMU8086
- Operating System: Windows/Linux/MacOS (with DOS emulator)
- Clone the repository:
git clone https://github.com/ralu2004/Kaprekar.git cd Kaprekar - Assemble the program:
nasm -f bin main.asm -o kaprekar.com
- Run the program using a DOS emulator:
dosbox kaprekar.com
- Input a four-digit number (e.g., 3524) when prompted.
- Open the project in VS Code.
- Assemble main.asm with NASM (either in the terminal or using an extension):
nasm -f bin main.asm -o kaprekar.com-
Run the resulting .com file: Directly in the terminal, or Using an x86 emulator/extension inside VS Code.
-
Enter a four-digit number when prompted. The program will iterate until it reaches Kaprekarβs constant 6174.
This project is licensed under the MIT License.
GitHub profile: https://github.com/ralu2004