.global	main
main:
	xor	%eax,%eax
	dec	%ax
	lea	2(%eax,%eax),%ecx
	sub	%ecx,%esp
	or	%eax,%esp
	mov	%esp,%esi
	xor	%eax,%esp
	cld
	mov	%esp,%edi
	rep	stosb
	xor	%eax,%eax
	cdq
	.byte	0x80,0xc6	# 80 c6 (40) add $0x40,%dh
write:
	.byte	0x40		# (40        inc %eax)
	lea	2(%eax),%ebx
	lea	3(%esi),%ecx
exit:
	xchg	%ebx,%eax
	inc	%eax
	int	$0x80
	push	%eax
	push	%ebx	# zero
	push	%ebx	# zero
	dec	%ebx
	je	exit
	lea	1(%esi),%ebx
	mov	%ebx,%edi
	mov	%eax,%ecx
	mov	$0x0a,%al
	stosb
	stosb
	mov	%al,(%edi,%ecx)
	mov	$'S',%al
	repne	scasb
	dec	%edi
loop3:
	sub	$8,%bx
	mov	%edi,0(%ebx)
	popl	4(%ebx)
	pop	%edi
	test	%edi,%edi
	jne	loop3
	push	%edi
loop2:
	mov	$0x04,%ah
loop1:	
	mov	(%ebx),%edi
	stc
	dec	%ah
	jns	x
	add	$8,%bx
	jne	loop2
	pop	%edi
	test	%edi,%edi
	jne	loop3
	mov	-3(%esi),%edi
	jmp	goalstart
goalend:
	pop	%edx
	xor	%eax,%eax
	jmp	write
goalloop:
	cmp	$' ',%al
	jne	goalend
	movb	$'$',(%edi)
goalstart:
	mov	%esi,%edx
	sub	%edi,%edx
	mov	(%esi,%edx,4),%ah
	xor	$2,%ah		# clears CF
x:
	pushf
	push	%eax
	mov	$0x0a,%al
	shr	%ah
xinc:
	sbb	%ecx,%ecx
	std
	repne	scasb
	mov	%ecx,%edx
	shr	%ah
	jc	x1
	cld
	inc	%edi
	inc	%edi
	jecxz	x1
	inc	%edx
	inc	%edx
x1:
	repne	scasb
	cmp	%edi,%esi
	je	xinc
	sbb	%edx,%edi
	pop	%eax
	popf
	mov	(%edi),%al
	jnc	goalloop
	mov	%esi,%edx
	sub	%edi,%edx
	lea	(%esi,%edx,4),%edx
	mov	(%edx),%ecx
	and	$~3,%ecx
	mov	4(%ebx),%ebp
	cmp	%ecx,%ebp
jmploop1:
	jnc	loop1
	cmp	$'G',%al
	je	isgoal
	cmpb	$' ',%al
	jne	loop1
	lea	8(%ebp),%ecx
	push	%ecx
	push	%edi
	.byte	0x3c		# 3c       cmp $0x89,%al; jle ????
isgoal:
	mov	%edi,-3(%esi)	# 89 7e fd
	mov	%ebp,(%edx)
	or	%ah,(%edx)	# clears CF
	jmp	jmploop1
