; Write/create a file

%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

			;Create file
	mov eax,8	;sys_creat
	mov ebx,[esp]	;argv[1] = filename
	mov ecx,644h	;mode
	int 80h
	mov [fd],eax
	printf "sys_creat returned %d\n", eax
	;Returns fd of new file (open for writing)

	mov edx,buf.1 - buf	;Write buffer to file
	mov ecx,buf
	mov ebx,[fd]
	mov eax,4		;sys_write
	int 80h
	printf "sys_write returned %d\n", eax
	;Returns # of bytes written

	mov ebx,[fd]		;Close file
	mov eax,6		;sys_close
	int 80h

halt:
	call flush
	mov eax,1
	int 80h

SECTION .bss
fd	resd 1

SECTION .data
buf	db "Blahhahaha...",10
.1
