P286N
IDEAL
MODEL small
STACK 100H

DATASEG
  strWelcome DB "Welcome user, enter a two digits number: $"
  strBeginPrime DB 13, 10, "The number you entered is $"
  strNot DB "not $"
  strEndPrime DB "prime!", 13, 10, "$"
  strError DB 13, 10, "Invalid input, quiting.", 13, 10, "$"
  strExit DB 13, 10, "Press any key to continue... $"
  
  intNumber DB 0 ; The number the user entered

CODESEG
; Maman 13, Question 1 Lior Tubi
; This program reads a two digit number (n) and prints if he is prime.
; The program goes through all numbers (x) from 2 to n-1
; and checks if: n mod x = 0, if it is, n is not prime.

MACRO printStr string ; Will be used to print messages
  mov ah, 9
  mov dx, offset string
  int 21H
ENDM

start:
  mov ax, @data
  mov ds, ax
  
  printStr strWelcome ; Instructions for the user
  
  mov ah, 01H ; Get first digit
  int 21H
  cmp al,'9'
  jg @@wrongInput
  cmp al,'0'
  jl @@wrongInput ; Input check
  push ax ; Save number
  
  mov ah, 01H ; Get second digit
  int 21H
  cmp al,'9'
  jg @@wrongInput
  cmp al,'0'
  jge @@rightInput

@@wrongInput:
  printStr strError ; Input is invalid, print error and exit
  jmp exit

@@rightInput:
  sub al, '0' ; Convert from ASCII to Decimal
  mov [byte intNumber], al
  pop ax ; Recover the first digit
  sub al, '0' ; Convert from ASCII to Decimal
  xor ah, ah ; Initialize ah to zero
  mov cl, 10
  imul cl ; Multiply first digit by ten
  add [byte intNumber], al ; Now intNumber equales to the number the user entered
  
  mov bl, 1 ; bl = 1 if the number entered is prime
  mov cl, [byte intNumber]
  xor ch, ch
  dec cx ; cx = intNumber-1
@@loopPrime:
  cmp cx, 1
  jle @@outOfLoop ; cx<2
  mov al, [byte intNumber]
  xor ah, ah
  idiv cl
  cmp ah, 0 ; number mod cl = 0 (means we found a divisor)
  je @@notPrime  
  loop @@loopPrime
  
@@notPrime:
  mov bl, 0 ; Set bl flag to false

@@outOfLoop:
  printStr strBeginPrime
  
  cmp bl, 1
  je @@isPrime
  
  printStr strNot

@@isPrime:  
  printStr strEndPrime
  
exit:
  printStr strExit ; "Press any key to continue..."
  mov ah, 0
  int 16H ; Wait for press
  
  mov ax, 4C00H ; End program.
  int 21H
END start