There are 100 doors in a row that are all initially closed.

You make 100 passes by the doors.

The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it).

The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it.

The third time, visit every 3rd door   (door #3, #6, #9, ...), etc,   until you only visit the 100th door.



Task

Answer the question:   what state are the doors in after the last pass?   Which are open, which are closed?


Alternate:
As noted in this page's   discussion page,   the only doors that remain open are those whose numbers are perfect squares.

Opening only those doors is an   optimization   that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.




page:	equ	2	; Store doors in page 2
doors:	equ	100	; 100 doors
puts:	equ	9	; CP/M string output
	org	100h
	xra	a	; Set all doors to zero
	lxi	h,256*page
	mvi	c,doors
zero:	mov	m,a
	inx	h
	dcr	c
	jnz	zero
	mvi	m,'$'	; CP/M string terminator (for easier output later)
	mov	d,a	; D=0 so that DE=E=pass counter
	mov	e,a	; E=0, first pass
	mvi	a,doors-1	; Last pass and door
pass:	mov	l,e	; L=door counter, start at first door in pass
door:	inr	m	; Incrementing always toggles the low bit
	dad	d	; Go to next door in pass
	inr	l
	cmp	l	; Was this the last door?
	jnc	door	; If not, do the next door
	inr	e	; Next pass
	cmp	e	; Was this the last pass?
	jnc	pass	; If not, do the next pass
	lxi	h,256*page
	mvi	c,doors	; Door counter
	lxi	d,130h	; D=1 (low bit), E=30h (ascii 0)
char:	mov	a,m	; Get door	
	ana	d	; Low bit gives door status
	ora	e	; ASCII 0 or 1
	mov	m,a	; Write character back
	inx	h	; Next door
	dcr	c	; Any doors left?
	jnz	char	; If so, next door
	lxi	d,256*page
	mvi	c,puts	; CP/M system call to print the string
	jmp	5