;	
; rta_tst0_eratosthenes.rta
; =========================
;
; The Sieve of Eratosthenes.
;
; Declarations
;
	_name	Eratosthenes		; Program name
	_var	maxa			; Last numer of prime arrayPrimzahlenfeldes
	_var	maxp			; Last prime
	_var	prime			; Actual Prime
	_dim	ARRAY	50000		; Declare an Array witht 50000 Elements			
;
; Initializations
; 
	pause	The~Sieve~of~Eratosthenes~...
	mov	maxa	50000		; Init Numb
	mov	maxp	maxa		; Last prime
	root	maxp	2 		; Is the root from 50000
	mul	maxp	2		; Plus reserve.
	clr	r1			; A pointer to ARRAY. 
;
; Fill the ARRAY
;
$lab0:
	put	ARRAY	r1	r1	; Fill the Elements with their adresses
	inc	r1			; Increment the pointer 
	cmple	r1	maxa	$lab0	; If Pointer less/equal maxa, jump
;
; Init the "Sieb"
;
	put	ARRAY	1	0	; 1 ist no prime
	mov	prime	2		; 1st primzahl is 2
;
; Cycles
;
$lab1:
	mov	r1	prime		; Fill pointer
	mov	r0	prime		; Fill increment register
	add	r1	r0		; Add increment to pointer
$lab2:
	put	ARRAY	r1	0	; Clear pointed array element
	add	r1	r0		; Next no prime number
	cmple	r1	maxa	$lab2	; Next step until array end
$lab3:
	add	prime	1		; Inc the prime
	cmpgt	prime	maxp	$lab6	; Abort jump if last test number
	get	r2	ARRAY	prime	; If no abort read the next array element
	tsteq	r2	$lab3		; If zero it isnt a prime. Jump to label 3
	mov	prime	r2		; No zero: Its a prime
	jump	$lab1			; to continue the test with it.
;
; Generate output text
;
$lab6:
	cls				; Clear output text area
	prints	\Prime~numbers~1~...~	; print the title
	printn	maxa	5	0	; 
	prints  \\			; Thats 2 Newlines
;
; Generate output text
;
	clr	r3			; Clear the counter
	mov	r1	1		; Pointer 1
$lab7:
	get	prime	ARRAY	r1	; Read form array
	tsteq	prime	$lab8		; If Zero no prime. Jump
	inc	r3	1		; Increment the counter
	printn	prime	5	0	; Prime output. 5 digits pre, 0 after decimal point
	mov	r4	r3		; Copy the counter
	cmod	r4	0	10	; r4 modulo 10
	tstne	r4	$lab8		; If zero continue
	prints  \			; If no zero a Newline output
$lab8:
	inc	r1			; Pointer increment
	cmple	r1	maxa	$lab7	; If less/equal: next output cycle
;
; End
;
	save	rta_eratosthenes	; Write text to file "eratostenes.txt"
	pause	Computation~finished.~Result~is~written~in~file~rta_eratosthenes.txt.
	exit				; Exit
	_end				
