;; ----===*******===----
	%INCLUDE 'PROLOG.NSM' 
;; --== NASM vers. 26-Mar-2010 ==--
	;; ***---- Program Body ----***
	  [SECTION .text]
;/* File: tst_1.c
;** Last: Sun 21 Aug 2011 10:40:29 PM CDT 
;** Prev: Sat 20 Aug 2011 12:48:59 PM CDT
;** Init: Sat 20 Aug 2011 07:15:48 AM CDT
;** Vers: 0r4
;** Tgts: compile with ccna32
;** Goal: this is a test to check puts()
;** NASM: -@tst_1.mak
;	-f elf
;	-l tst_1.lst
;	-o tst_1.o
;	-Z tst_1_err.txt
;	tst_1.nsm
;** Link: ld --dynamic-linker /lib/ld-linux.so.2 -lc -o tst_1 tst_1.o
;*/
;#define FILE int
;/*	Define the input line			*/
;#define	LINESIZE	255
;#define	LINEMAX		254	/** LINESIZE-1 **/
;#define EOF		-1	/** these are changed to intenal labels _EOF **/
;#define EOL		10
;#define CR		10
;#define FP_STD_OUT	1	/** stdin=0, stdout=1, stderr=2 **/
;FILE *fp_output, *fp_input, *fp_stdout;
;int chr, v1, v2, desc1;
;char *c_fmt, *d_fmt, chr2;
;/** char *pbufr; **/
;int result; /** v.1, does work **/
;char	linebufr[LINESIZE];	/** parsing buffer **/
;int	ln_ndx,			/** index into buffer **/
;	linecnt,
;	k,			/** chr **/
;	eof_flg;		/** set non-zero on final input eof **/
;main()
_main:
	PUSH EBP
	SUB  ESP, AutoC1
	MOV  EBP, ESP

;{
;/**	int result; ** v.0, doesn't work. **/
;	c_fmt = "%c";
	MOV  EBX, L_1+0
	MOV  DWord [_c_fmt], EBX
;	d_fmt = " %d";
	MOV  EBX, L_1+3
	MOV  DWord [_d_fmt], EBX
;	chr2 = '^';
	MOV  EBX, +94
	MOV  Byte [_chr2], BL
;	result = 1;
	MOV  EBX, +1
	MOV  DWord [_result], EBX
;/**	pbufr = &linebufr[0]; **/
;	puts("Testing puts.");
	MOV  EBX, L_1+7
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;	if ((fp_output = fopen("mytst.txt","w")) == NULL) /* if given, open */
	MOV  EBX, L_1+21
	PUSH EBX
	MOV  EBX, L_1+31
	PUSH EBX
	CALL _fopen
	ADD  ESP, +8
	MOV  DWord [_fp_output], EBX
	PUSH EBX
	MOV  EBX, _NULL
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_2
	JMP  L_3
L_2:
;		{
;		fp_output = NULL;	/* can't open */
	MOV  EBX, _NULL
	MOV  DWord [_fp_output], EBX
;		puts("Open output-file failure!");
	MOV  EBX, L_1+33
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;		}
;	else puts("output-file opened.");
	JMP  L_4
L_3:
	MOV  EBX, L_1+59
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
L_4:
;	if ((fp_input = fopen("tst_1.c","r")) == NULL) /* if given, open */
	MOV  EBX, L_1+79
	PUSH EBX
	MOV  EBX, L_1+87
	PUSH EBX
	CALL _fopen
	ADD  ESP, +8
	MOV  DWord [_fp_input], EBX
	PUSH EBX
	MOV  EBX, _NULL
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_5
	JMP  L_6
L_5:
;		{
;		fp_input = NULL;	/* can't open */
	MOV  EBX, _NULL
	MOV  DWord [_fp_input], EBX
;		puts("Open input-file failure!");
	MOV  EBX, L_1+89
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;		}
;	else puts("input-file opened.");
	JMP  L_7
L_6:
	MOV  EBX, L_1+114
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
L_7:
;/**  get stdio FILE * value so that we can fputs() to it. **/
;/**  fdopen (int filedes, const char *opentype) **/
;	
;	if ((fp_stdout = fdopen (FP_STD_OUT, "w")) == NULL)
	MOV  EBX, +1
	PUSH EBX
	MOV  EBX, L_1+133
	PUSH EBX
	CALL _fdopen
	ADD  ESP, +8
	MOV  DWord [_fp_stdout], EBX
	PUSH EBX
	MOV  EBX, _NULL
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_8
	JMP  L_9
L_8:
;		puts ("fdopen() error. ");
	MOV  EBX, L_1+135
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;	printf(d_fmt,fp_stdout);
L_9:
	MOV  EBX, [DWord _d_fmt]
	PUSH EBX
	MOV  EBX, [DWord _fp_stdout]
	PUSH EBX
	CALL _printf
	ADD  ESP, +8
;	putchar(10);
	MOV  EBX, +10
	PUSH EBX
	CALL _putchar
	ADD  ESP, +4
;	if ((desc1 = fileno(fp_stdout)) == -1)
	MOV  EBX, [DWord _fp_stdout]
	PUSH EBX
	CALL _fileno
	ADD  ESP, +4
	MOV  DWord [_desc1], EBX
	PUSH EBX
	MOV  EBX, +1
	NEG  EBX
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_10
	JMP  L_11
L_10:
;		puts ("fileno() error. ");
	MOV  EBX, L_1+152
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;	printf(d_fmt, desc1);
L_11:
	MOV  EBX, [DWord _d_fmt]
	PUSH EBX
	MOV  EBX, [DWord _desc1]
	PUSH EBX
	CALL _printf
	ADD  ESP, +8
;	putchar(10);
	MOV  EBX, +10
	PUSH EBX
	CALL _putchar
	ADD  ESP, +4
;/**	pbufr = "fputs string."; **/
;/***
;	fputs (" fdopen fputs", fp_stdout);
;	putchar(10);
;***/
;	puts("Do file operations here.., and close.");
	MOV  EBX, L_1+169
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;/**	while (result == 1) ** works
;		{
;		putchar('.');
;		chr = fgetc(fp_input);** ** works **/
;/**		if (chr == EOF)
;			{
;			result = 0;
;			break;
;			}
;		else fputc(chr,fp_output); ** works **
;		}
;**/
;/**	while ((chr = fgetc(fp_input)) != EOF)  fputc(chr,fp_output); ** works **/
;/**	printf(c_fmt, chr2); ** works **/
;/** 	while ((chr = fgetc(fp_input)) != EOF) putchar(chr); ** works **/
;/**	while ((chr = fgetc(fp_input)) != EOF) printf(c_fmt,chr); ** works **/
;	putchar(10);
	MOV  EBX, +10
	PUSH EBX
	CALL _putchar
	ADD  ESP, +4
;	processfile();
	CALL _processfile
;	putchar(10);
	MOV  EBX, +10
	PUSH EBX
	CALL _putchar
	ADD  ESP, +4
;	if ((result = fclose(fp_output)) == 0) puts("Output file closed.");
	MOV  EBX, [DWord _fp_output]
	PUSH EBX
	CALL _fclose
	ADD  ESP, +4
	MOV  DWord [_result], EBX
	PUSH EBX
	MOV  EBX, +0
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_12
	JMP  L_13
L_12:
	MOV  EBX, L_1+207
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;	else puts("Output file close error.");
	JMP  L_14
L_13:
	MOV  EBX, L_1+227
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
L_14:
;	if ((result = fclose(fp_input)) == 0) puts("Input file closed.");
	MOV  EBX, [DWord _fp_input]
	PUSH EBX
	CALL _fclose
	ADD  ESP, +4
	MOV  DWord [_result], EBX
	PUSH EBX
	MOV  EBX, +0
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_15
	JMP  L_16
L_15:
	MOV  EBX, L_1+252
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;	else puts("Input file close error.");
	JMP  L_17
L_16:
	MOV  EBX, L_1+271
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
L_17:
;}
	POP  EBP
	RET

AutoC1	EQU 0

	  [SECTION .data]
L_1	DB "%c",0
	DB " %d",0
	DB "Testing puts.",0
	DB "mytst.txt",0
	DB "w",0
	DB "Open output-file failure!",0
	DB "output-file opened.",0
	DB "tst_1.c",0
	DB "r",0
	DB "Open input-file failure!",0
	DB "input-file opened.",0
	DB "w",0
	DB "fdopen() error. ",0
	DB "fileno() error. ",0
	DB "Do file operations here.., and close.",0
	DB "Output file closed.",0
	DB "Output file close error.",0
	DB "Input file closed.",0
	DB "Input file close error.",0

	  [SECTION .text]
; /** main input function, chrs accumulate into static linebufr[], **/
; /** also echoes to file commentary tokens, if desired by user. **/
; /** a byte value > 7Fh (sign set) is read as eof in this function **/
; /** On return, linebufr[] holds string of k's, where k is of the set **/
; /** of bytes {1..7Fh} with a null termination, 0. **/
;processfile()
_processfile:
	PUSH EBP
	SUB  ESP, AutoC18
	MOV  EBP, ESP

;{
;	ln_ndx = 0;
	MOV  EBX, +0
	MOV  DWord [_ln_ndx], EBX
;	eof_flg = 0;
	MOV  EBX, +0
	MOV  DWord [_eof_flg], EBX
;	linecnt = 0;
	MOV  EBX, +0
	MOV  DWord [_linecnt], EBX
;	while(1)
L_19:
	MOV  EBX, +1
	OR   EBX, EBX
	JNZ  L_20
	JMP  L_21
L_20:
;		{
;		
;		if (eof_flg) break;
	MOV  EBX, [DWord _eof_flg]
	OR   EBX, EBX
	JNZ  L_22
	JMP  L_23
L_22:
	JMP  L_21
;		
;		clr_ln();					/** clear line index **/
L_23:
	CALL _clr_ln
;		while ((k = fgetc (fp_input)) != EOF) 		/** main fetch fn **/
L_24:
	MOV  EBX, [DWord _fp_input]
	PUSH EBX
	CALL _fgetc
	ADD  ESP, +4
	MOV  DWord [_k], EBX
	PUSH EBX
	MOV  EBX, +1
	NEG  EBX
	POP  EDX
	CALL ccne
	OR   EBX, EBX
	JNZ  L_25
	JMP  L_26
L_25:
;			{
;			if (ln_ndx >= 254)			/** linemax **/
	MOV  EBX, [DWord _ln_ndx]
	PUSH EBX
	MOV  EBX, +254
	POP  EDX
	CALL ccge
	OR   EBX, EBX
	JNZ  L_27
	JMP  L_28
L_27:
;				{
;				puts ("Error, line length exceeds line buffer size.");
	MOV  EBX, L_18+0
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;				clr_ln();
	CALL _clr_ln
;				continue;
	JMP  L_24
;				}
;			if (k == EOL)
L_28:
	MOV  EBX, [DWord _k]
	PUSH EBX
	MOV  EBX, +10
	POP  EDX
	CALL cceq
	OR   EBX, EBX
	JNZ  L_29
	JMP  L_30
L_29:
;				{
;		/**		linebufr[ln_ndx++] = k;	** store input in static char array **/
;				linebufr[ln_ndx] = 0;	/** null terminate line to make string **/
	MOV  EBX, _linebufr
	PUSH EBX
	MOV  EBX, [DWord _ln_ndx]
	POP  EDX
	ADD  EBX, EDX
	PUSH EBX
	MOV  EBX, +0
	POP  EDI
	MOV  Byte [EDI], BL
;		/**		putchar(k); **/	/** test **/
;				linecnt++;
	MOV  EBX, [DWord _linecnt]
	INC  EBX
	MOV  DWord [_linecnt], EBX
	DEC  EBX
;				process_ln();
	CALL _process_ln
;				clr_ln();
	CALL _clr_ln
;				}
;			linebufr[ln_ndx++] = k;	/** store input char in static char array **/
L_30:
	MOV  EBX, _linebufr
	PUSH EBX
	MOV  EBX, [DWord _ln_ndx]
	INC  EBX
	MOV  DWord [_ln_ndx], EBX
	DEC  EBX
	POP  EDX
	ADD  EBX, EDX
	PUSH EBX
	MOV  EBX, [DWord _k]
	POP  EDI
	MOV  Byte [EDI], BL
;			} /** eo while **/
	JMP  L_24
L_26:
;		/** EOF **/
;		eof_flg = TRUE;
	MOV  EBX, _TRUE
	MOV  DWord [_eof_flg], EBX
;		
;		} /** eo while **/
	JMP  L_19
L_21:
;	putchar(10);
	MOV  EBX, +10
	PUSH EBX
	CALL _putchar
	ADD  ESP, +4
;	puts("-eof-");
	MOV  EBX, L_18+45
	PUSH EBX
	CALL _puts
	ADD  ESP, +4
;}
	POP  EBP
	RET

AutoC18	EQU 0

	  [SECTION .data]
L_18	DB "Error, line length exceeds line buffer size.",0
	DB "-eof-",0

	  [SECTION .text]
;/*
;** process line in the buffer as a string.
;*/
;process_ln()
_process_ln:
	PUSH EBP
	SUB  ESP, AutoC31
	MOV  EBP, ESP

;{
;/**	fputs (linebufr, stdout); ** need to find stdout's ptr value **/
;	fputs (linebufr, fp_stdout);
	MOV  EBX, _linebufr
	PUSH EBX
	MOV  EBX, [DWord _fp_stdout]
	PUSH EBX
	CALL _fputs
	ADD  ESP, +8
;	fputs (linebufr, fp_output);
	MOV  EBX, _linebufr
	PUSH EBX
	MOV  EBX, [DWord _fp_output]
	PUSH EBX
	CALL _fputs
	ADD  ESP, +8
;/**	puts (linebufr);	**/		/** FILE output, fp_output **/
;/**	fputc (EOL, fp_output);  */
;}
	POP  EBP
	RET

AutoC31	EQU 0
;/*
;** reindex & null terminate input line.
;*/
;clr_ln()
_clr_ln:
	PUSH EBP
	SUB  ESP, AutoC32
	MOV  EBP, ESP

;{
;	ln_ndx = 0;
	MOV  EBX, +0
	MOV  DWord [_ln_ndx], EBX
;	linebufr[ln_ndx] = 0;
	MOV  EBX, _linebufr
	PUSH EBX
	MOV  EBX, [DWord _ln_ndx]
	POP  EDX
	ADD  EBX, EDX
	PUSH EBX
	MOV  EBX, +0
	POP  EDI
	MOV  Byte [EDI], BL
;}
	POP  EBP
	RET

AutoC32	EQU 0
;/*
;** eo tst_1.c
;*/

	  [SECTION .data]
_fp_output:	TIMES +1	DD 0
_fp_input:	TIMES +1	DD 0
_fp_stdout:	TIMES +1	DD 0
_chr:	TIMES +1	DD 0
_v1:	TIMES +1	DD 0
_v2:	TIMES +1	DD 0
_desc1:	TIMES +1	DD 0
_c_fmt:	TIMES +1	DD 0
_d_fmt:	TIMES +1	DD 0
_chr2:	TIMES +1	DB 0
_result:	TIMES +1	DD 0
_linebufr:	TIMES +255	DB 0
_ln_ndx:	TIMES +1	DD 0
_linecnt:	TIMES +1	DD 0
_k:	TIMES +1	DD 0
_eof_flg:	TIMES +1	DD 0

	  [SECTION .text]

	%INCLUDE 'EPILOG.NSM' 
; --- End of Compilation ---