CSC231 createFile.asm

From dftwiki3
Revision as of 11:02, 5 November 2008 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 16:02, 5 November 2008 (UTC)



;;; createNewFile.asm
;;; D. Thiebaut
;;; Create a new file with name data.txt and stores a
;;; string in it.
;;; 
%assign SYS_EXIT	1
%assign	SYS_WRITE	4
%assign SYS_READ	3
%assign SYS_LSEEK	19
%assign SEEKSET		0
%assign	STDOUT		1
%assign SYS_OPEN        5
%assign SYS_CLOSE	6
%assign SYS_CREATE	8


%assign O_RDONLY	000000q
%assign O_WRONLY        000001q	
%assign O_RDWR          000002q
%assign O_CREAT		000100q

%assign S_IRUSR		00400q
%assign S_IWUSR		00200q
%assign S_IXUSR		00100q

;;; ; --- MACRO -----------------------------------------------
;;; ;     print           msg,length
%macro  print           2 ; %1 = address %2 = # of chars
        pushad            ; save all registers
        mov             edx,%2
        lea             ecx,[%1]
        mov             eax,SYS_WRITE
        mov             ebx,STDOUT
        int             0x80
        popad           ; restore all registers
%endmacro

;;; ; --- MACRO -----------------------------------------------
;;; ;     print2        "quoted string"
%macro  print2        1 ; %1 = immediate string,
        section .data
%%str   db              %1
%%strL  equ             $-%%str
        section .text
        print           %%str, %%strL
%endmacro
        
;;; --- MACRO -----------------------------------------------
;;; 	createFile filename,  handle
%macro	createFile 2
	mov	eax,SYS_CREATE
	mov	ebx,%1
	mov	ecx, S_IRUSR|S_IWUSR|S_IXUSR
	int	0x80

	test	eax,eax
	jns	%%createfile
	print2  "Could not open file"
	mov	eax,SYS_EXIT
	mov	ebx,0
	int	0x80		; final system call

%%createfile:
	mov	%2,eax		; save handle
%endmacro	


	
;;; --- MACRO -----------------------------------------------
;;; 	writeFile handle, buffer, noBytesToWrite
%macro  writeFile 3
	mov	eax,SYS_WRITE
	mov	ebx,%1
	mov	ecx,%2
	mov	edx,%3
	int	0x80	
%endmacro

	
;;; --- MACRO -----------------------------------------------
;;;	close   handle
%macro	close   1
	mov	eax,SYS_CLOSE
	mov	ebx,%1
	int	0x80
%endmacro
	

        
	;; -------------------------
	;; data segment
	;; -------------------------

	section	.data
fileName db     "data.txt",0
handle   dd     0
buffer   db     "A fine romance with no kisses", 0x0a
         db     "A fine romance my friend this is.", 0x0a
LEN      equ    $-buffer        
        

	;; -------------------------
	;; code area
	;; -------------------------

	section	.text
	global	_start

_start:
;;; write contents of buffer to file
        
        createFile      fileName, [handle]
        writeFile       [handle], buffer, LEN
        close           [handle]
         
	;; exit()

	mov	eax,SYS_EXIT
	mov	ebx,0
	int	0x80		; final system call