.global	main
main:
	xor	%eax,%eax
	dec	%ax
	lea	2(%eax,%eax),%ecx
	sub	%ecx,%esp
	or	%eax,%esp
	mov	%esp,%esi
	inc	%esi
	xor	%eax,%esp
	cld
	mov	%esp,%edi
	rep	stosb
	xor	%ebx,%ebx
	lea	3(%ebx),%eax
	lea	2(%esi),%ecx
	addr16 lea 16384(%bx),%edx
	lea	(%esi,%edx),%ebp
	int	$0x80
	push	%eax
	push	%ebp
	mov	%esi,%edi
	mov	%eax,%ecx
	pusha
	mov	$0x0a,%al
	stosb
	stosb
	lodsl
	mov	%eax,(%edi,%ecx)
	popa
	mov	$'S',%al
	repne	scasb
	dec	%edi
	mov	%edi,(%ebp)
	incl	4(%ebp)
	lea	8(%ebp),%ebx

loop2:
	mov	$0x030a,%ax
loop1:	
	mov	(%ebp),%edi
	call	x
	mov	%edi,%edx
	sub	%esi,%edx
	not	%edx
	lea	(%esi,%edx,4),%edx
	mov	(%edx),%ecx
	and	$~3,%cl
	cmp	4(%ebp),%ecx
	jbe	gonext
	mov	(%edi),%cl
	cmp	$'G',%cl
	je	1f
	cmpb	$' ',%cl
	jne	gonext
	mov	%edi,(%ebx)
	mov	4(%ebp),%ecx
	lea	4(%ecx),%ecx
	mov	%ecx,4(%ebx)
	add	$8,%bx
	jne	1f
	mov	(%esp),%ebx
1:
	mov	4(%ebp),%ecx
	or	%ah,%cl
	mov	%ecx,(%edx)
gonext:
	dec	%ah
	jns	loop1
	add	$8,%bp
	jne	1f
	mov	(%esp),%ebp
1:
	cmp	%ebp,%ebx
	jne	loop2

	cld
	pop	%ecx
	mov	(%esp),%ecx
	mov	%esi,%edi
	mov	$'G',%al
	repne	scasb

	mov	$0xa,%al
	dec	%edi
1:
	mov	%edi,%edx
	sub	%esi,%edx
	not	%edx
	mov	(%esi,%edx,4),%ah
	xor	$1,%ah
	call	x
	cmpb	$'S',(%edi)
	je	done
	movb	$'$',(%edi)
	jmp	1b
done:

	pop	%edx
	xor	%ebx,%ebx
	lea	4(%ebx),%eax
	inc	%ebx
	lea	2(%esi),%ecx
	int	$0x80
	mov	%ebx,%eax
	int	$0x80
x:
	xor	%ecx,%ecx
	dec	%ecx
	test	$2,%ah
	jne	1f
	std
	repne	scasb
	cmp	%edi,%esi
	je	2f
	mov	%ecx,%edx
	test	$1,%ah
	je	3f
	cld
	inc	%edi
	inc	%edi
	inc	%edx
	inc	%edx
3:
	repne	scasb
	sub	%edx,%edi
4:
	dec	%edi
2:
	ret
1:
	test	$1,%ah
	je	4b
	inc	%edi
	ret
