// This may look like C code, but it is really -*- Asm -*-
/*
 *  smoke16/src16/echo.s - A minimal "echo" command.
 *  $Revision: 1.8 $
 *  Copyright 1997, 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>
	.text
	.align 1
	// int main(int argc, char * argv[])
	FUNCTION(_main)

	// push %t0 - %t5
	sub 12, %sp
	mov %t0, [%sp + 2]
	mov %t1, [%sp + 4]
	mov %t2, [%sp + 6]
	mov %t3, [%sp + 8]
	mov %t4, [%sp + 10]
	mov %t5, [%sp + 12]

	add %a0, %a0, %t0
	add %a1, %t0, %t0
	mov %a1, %t1
	mov [_stdout], %t2
	mov ' ', %t3
	mov _putchar, %t4
	mov _fputs, %t5
	
	cmp %t0, %t1
	be done
	add 2, %t1 // argv++
	cmp %t0, %t1
	be done

	mov [%t1], %a0 // argv[0]
	mov %t2, %a1 // stdout
	call %t5 // fputs()

loop:	add 2, %t1 // argv++
	cmp %t0, %t1
	be done

	mov %t3, %a0 // ' '
	call %t4 // putchar()

	mov [%t1], %a0 // argv[0]
	mov %t2, %a1 // stdout
	call %t5 // fputs()

	b loop
	
done:	mov '\n', %a0
	call %t4 // putchar()

	// pop %t0 - %t5
	mov [%sp + 2], %t0
	mov [%sp + 4], %t1
	mov [%sp + 6], %t2
	mov [%sp + 8], %t3
	mov [%sp + 10], %t4
	mov [%sp + 12], %t5
	add 12, %sp

	mov %0, %a0 // return 0
	RET
