Prime number check
The program asks for a number and determines whether it is prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Program source code
title .model small .stack 100h data segment public askUser db "Enter a number to check: \0" fmtinput db "%d" prime db "\nPrime number!\0" notPrime db "\nNot a prime number!\0" inputNumber dw ? 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 WriteString ; Call the WriteString procedure lea ax, ds:[inputNumber] ; Load the address of the inputNumber 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 cmp ds:[inputNumber], 1 ; Compare the returned value with 1 jle NotPrimeNumber ; If ax is less than or equal to 1, jump to the label mov cx, 1 ; Set the counter to 1 MainLoop: mov ax, inputNumber ; Copy the value of the inputNumber variable into the ax register cwd ; Sign-extend dx. The dx register is cleared div cx ; Divide the ax register by the value of the counter test dl, dl ; Check whether the value in the dl register is 0 jnz Skip ; If ZF=0, jump to the Skip label inc number ; Increment the value of the number variable Skip: cmp number, 2 ; Compare the value of the number variable with the number 2 jz NotPrimeNumber ; If ZF=1, jump to the NotPrimeNumber label inc cx ; Increment the value of the counter register by 1 cmp cx, inputNumber ; Compare cx with the value in the inputNumber variable jnz MainLoop ; If CX != inputNumber, jump to MainLoop lea ax, ds:[prime] ; 1st: load the address of the prime variable into ax call PrintFormatString ; Call the PrintFormatString procedure jmp Done ; Jump unconditionally to the Done label NotPrimeNumber: lea ax, ds:[notPrime] ; 1st: load the address of the notPrime variable into ax call PrintFormatString ; Call the PrintFormatString procedure Done: 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 to check: 2
Prime number!
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".