; Basic Assembly
; ==============
; 
; Structured branching
; --------------------
;
; Just2Divisors
; @@@@
;
; 0. Write a program that takes the number n as input. Then it prints all the
;   numbers x below n that have exactly 2 different integral divisors (Besides 1
 ;  and x). 
    ;  For example: 15 is such a number. It is divisible by 1,3,5,15. (Here 3 and 5
 ;  are the two different divisiors, besides 1 and 15).
;  However, 4 is not such a number. It is divisible by 1,2,4.

format PE console
entry start

include 'win32a.inc' 

; ===============================================
section '.text' code readable executable

start:
    
call    read_hex	; eax <- n
cmp eax,6h  	
jb exit_program	;(if n is below 6, which is the smallest number with only 2 divisors)
	
mov     esi,2h		; esi <- 2
dec 	eax			; x <- n-- (x is the biggest number below n)

check_integers:
	
	mov ebx, eax 		;ebx <- x
	mov edi, 0h			; 0 divisiors
  	div esi			; eax <- x/2 , edx <- remainder
	call print_eax	; print just to check the code
	mov ecx, eax	; ecx <- x/2
	mov eax, ebx	; eax <- x
	
count_divisors:
	div ecx		; eax <- x / (x/2) , edx <- remainder 
	call print_eax	; print just to check the code
	mov eax, ebx	; eax <- x
	cmp edx, 0h
	jnz skip_inc_divisors  ;(if remainder not equal 0)
	inc edi	; divisiors++

skip_inc_divisors:
	dec ecx 	; ecx--
	cmp ecx, 2h
	jnb count_divisors ;(if ecx not below 2)

	cmp edi, 2h;
	jne skip_print
	call print_eax	; print x (print x if equals 2)

skip_print:
	dec eax		; x--
	cmp eax,6h  	
	jnb check_integers	;(if eax not below 6, which is the smallest number with only 2 divisors)

exit_program:
    ; Exit the process:
	push	0
	call	[ExitProcess]

include 'training.inc'
