The sequence is generated by following this pseudo-code:

A:  The first term is zero.
    Repeatedly apply:
        If the last term is *new* to the sequence so far then:
B:          The next term is zero.
        Otherwise:
C:          The next term is how far back this last term occured previously.



Example

Using A:

0
Using B:

0 0
Using C:

0 0 1
Using B:

0 0 1 0
Using C: (zero last occurred two steps back - before the one)

0 0 1 0 2
Using B:

0 0 1 0 2 0
Using C: (two last occurred two steps back - before the zero)

0 0 1 0 2 0 2 2
Using C: (two last occurred one step back)

0 0 1 0 2 0 2 2 1
Using C: (one last appeared six steps back)

0 0 1 0 2 0 2 2 1 6
...



Task

 Create a function/procedure/method/subroutine/... to generate the Van Eck sequence of numbers.
 Use it to display here, on this page:
 The first ten terms of the sequence.
 Terms 991 - to - 1000 of the sequence.


References

 Don't Know (the Van Eck Sequence) - Numberphile video.
 Wikipedia Article: Van Eck's Sequence.
  OEIS sequence: A181391.


LIMIT:	equ	1000
	cpu	8086
	org	100h
section	.text
	mov	di,eck		; Zero out the memory
	xor	ax,ax
	mov	cx,LIMIT
	rep	stosw
	mov	bx,eck		; Base address
	mov	cx,LIMIT	; Limit
	xor	ax,ax
	mov	si,-1		; Outer loop index
outer:	inc	si		
	dec	cx
	jcxz	done
	mov	di,si		; Inner loop index
inner:	dec	di
	js	outer
	shl	si,1		; Shift the loop indices (each entry is 2 bytes)
	shl	di,1
	mov	ax,[si+bx]	; Find a match?
	cmp	ax,[di+bx]
	je	match
	shr	si,1		; If not, shift SI and DI back and keep going
	shr	di,1
	jmp	inner
match:	mov	ax,si		; Calculate the new value
	sub	ax,di
	shr	ax,1		; Compensate for shift
	mov	[si+bx+2],ax	; Store value
	shr	si,1		; Shift SI back and calculate next value
	jmp	outer
done:	xor	si,si		; Print first 10 elements
	call	p10
	mov	si,LIMIT-10	; Print last 10 elements⌈
p10:	mov	cx,10		; Print 10 elements starting at SI
	shl	si,1		; Items are 2 bytes wide
	add	si,eck
.item:	lodsw			; Retrieve item
	call	printn		; Print it 
	loop	.item
	mov	dx,nl		; Print a newline afterwards
	jmp	prints
printn:	mov	bx,buf		; Print AX
	mov	bp,10
.digit:	xor	dx,dx		; Extract digit
	div	bp
	add	dl,'0'		; ASCII digit
	dec	bx	
	mov	[bx],dl		; Store in buffer
	test	ax,ax		; Any more digits?
	jnz	.digit
	mov	dx,bx
prints:	mov	ah,9		; Print string in buffer
	int	21h
	ret 
section	.data
nl:	db	13,10,'$'
	db	'.....'
buf:	db	' $'
section	.bss
eck:	resw	LIMIT