; Read files

%include "asmmac.inc"
%include "io.inc"

SECTION .text
	global _start
	align 4
_start:
	call init_io

	pop eax		;expect 1 arg: filename
	cmp eax,2
	jne halt
	pop eax

			;Open file
	mov eax,5	;sys_open
	pop ebx		;argv[1] = filename
	mov ecx,0	;0=read-only, 1=write-only, 2=r/w, 3="special"
	mov edx,0	;"mode"
	int 80h
	mov [fd],eax

.1	mov edx,80		;Read 80 bytes from file to buffer
	mov ecx,buf
	mov ebx,[fd]
	mov eax,3		;sys_read
	int 80h
	;Returns # of bytes read (0 means EOF or waiting for input)
	or eax,eax		;end of file?
	jz halt

	mov [buf+eax], byte 0	;Write buffer to stdout
	mov eax,buf
	call _puts
	jmp short .1

halt:
	call flush
	mov eax,1
	int 80h

SECTION .bss
fd	resd 1
buf	resb 81
