Factors of a number
A program that reads any number and prints all its factors (divisors).
Program source code
title .model small .stack 100h data segment public askUser db "Enter a number: \0" msgres db "\nFactors: \0" fmtinput db "%d" factors db "%d \0" number dw ? data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure lea ax, ds:[askUser] ; Load the address of the askUser variable into ax call PrintFormatString ; Call the PrintFormatString procedure lea ax, ds:[number] ; Load the address of the number variable into the ax register push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtinput] ; 1st: load the address of the fmtinput format string into ax call ScanFormatString ; Call the ScanFormatString procedure add sp, 2 ; Align the stack lea ax, ds:[msgres] ; 1st: Load the address of the msgres variable into the ax register call PrintFormatString ; Call the PrintFormatString procedure mov cx, 1 ; Set the counter to 1 MainLoop: mov ax, number ; Copy the value of the number variable into the ax register cwd ; Sign-extend dx. The dx register is cleared div cx ; Divide the value in the ax register by the value in cx test dl, dl ; Check the value in the dl register for 0 jnz Skip ; If ZF=0, jump to the Skip label push cx ; 2nd: Save the cx counter register value on the stack lea ax, ds:[factors] ; 1st: Load the address of the factors format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack Skip: inc cx ; Increment the counter register value by 1 cmp cx, number ; Compare the value of cx with the value in the number variable jbe MainLoop ; If the CX value is below or equal to number, then jump to MainLoop xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Enter a number: 63
Factors: 1 3 7 9 21 63
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".