// This may look like C code, but it is really -*- Asm -*-
/*
 *  smoke16/src16/f16.s - Forth-like interpreter...
 *  $Revision: 1.5 $
 *  Copyright 1999, 2000, Benjamin Sittler
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <smoke16/asm.h>

#define BUFSIZE 80

	.lcomm buf, BUFSIZE

	.text
	.align 1
	FUNCTION(_main)
	push %t0
	mov _write, %t0
	push %t1
	mov [_stdout], %t1
	push BUFSIZE		; // sp[3] = BUFSIZE;
	push '\n'		; // sp[2] = '\n';
	push -1			; // sp[1] = -1;
	mov %0, %a2		; // a2 = 0;
main0:	mov [%sp + 6], %a1	; // main0:
	cmpl %a1, %a2		; // if (a2 == BUFSIZE)
	be main1		; //   goto main1;
	push %a2
	call _getchar		; // a0 = getchar();
	pop %a2
	mov [%sp + 2], %a1
	cmp %a0, %a1		; // if (a0 == -1)
	be main2		; //   goto main2;
	mov buf, %a1
	add %a2, %a1
	movb %a0, [%a1]		; // buf[a2] = a0;
	add 1, %a2		; // a2++;
	mov [%sp + 4], %a1
	cmp %a0, %a1		; // if (a0 == '\n')
	be main1		; //   goto main1;
	b main0			; // goto main0;
main1:	mov buf, %a1		; // main1:
	mov [%t1], %a0		; // a0 = fileno(stdout); /* HACK!!! */
	call %t0		; // write (a0, buf, a2);
	mov %0, %a2		; // a2 = 0;
	b main0			; // goto main0;
main2:	mov buf, %a1		; // main2: a1 = buf;
	mov [%t1], %a0		; // a0 = fileno(stdout); /* HACK!!! */
	call %t0		; // write (a0, buf, a2);
	mov %0, %a0
	mov [%sp + 10], %t0
	mov [%sp + 8], %t1
	RET			; // return 0;
