CSC231 readFileProc.asm

From dftwiki3
Revision as of 08:47, 10 November 2008 by Thiebaut (talk | contribs) (New page: --~~~~ ---- <code><pre> ;;; readFile.asm ;;; D. Thiebaut ;;; Reads a file called data.txt and displays its contents ;;; on the screen. ;;; Uses macros for printing and functions for file o...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 13:47, 10 November 2008 (UTC)


;;; readFile.asm
;;; D. Thiebaut
;;; Reads a file called data.txt and displays its contents
;;; on the screen.
;;; Uses macros for printing and functions for file operations.

;;; ---------------------------------------------------------
;;; FILE-RELATED CONSTANTS
;;; ---------------------------------------------------------
%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
	
	;; -------------------------
	;; data segment
	;; -------------------------

	section	.data
fileName db     "data.txt",0
handle   dd     0
noRead   dd     0               ; to store # of chars read from file
        
        section .bss
MAXBUF  equ     100000
buffer  resb    MAXBUF          ; 100,000 bytes of storage

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

	section	.text
	global	_start

_start:
;;; open the file and put its contents in Buffer.
;;; keep track of # of bytes read in noRead

;;;     openFile( fileName,  [handle] )
        mov     eax, fileName
        push    eax
        mov     eax, handle
        push    eax
        call    openFile

;;;     readFile( [handle], buffer, MAXBUF, [noRead] )
        push    dword [handle]
        mov     eax, buffer
        push    eax
        mov     eax, MAXBUF
        push    eax
        mov     eax, noRead
        push    eax
        call    readFile

;;;     close( [handle] )
        push    dword [handle]
        call    close

        print   buffer, [noRead]
         
;;; exit()

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


;;; ---------------------------------------------------------
;;; sleep: assuming a 2 GHz processor, the instruction
;;;        for: loop for  should take 1 cycle to execute
;;;        since the processor goes through 2E09 cycles 
;;;        per second, loading ecx with 2000000000 before
;;;        the loop should yield something in the order of
;;;        a second delay.
sleep:  push    ecx
        mov     ecx, 2000000000
.for    loop    .for
        pop     ecx
        ret


;;; --------------------------------------------------------------
;;; openFile( filename, handle )
;;; opens a file for reading, puts handle in dword whose address
;;; is passed in stack.
;;; Does not modify the registers, but modifies flags.
openFile:
        push    ebp
        mov     ebp, esp
        pushad
        
%define of_fileName dword[ebp+12]
%define of_handle   dword[ebp+8]
        
	mov	eax,SYS_OPEN
	mov	ebx, of_fileName
	mov	ecx, O_RDONLY
	mov	edx, S_IRUSR|S_IWUSR|S_IXUSR
	int	0x80

	test	eax,eax
	jns	.fileOk
        print2  "Error opening file"
        mov     eax, SYS_EXIT
        mov     ebx, 1
        int     0x80

.fileOk:
        mov     ebx, of_handle
	mov	[ebx], eax	; save handle
        clc                     ; clear carry if ok
        
.done:  popad                   ; restore registers
        pop     ebp
        ret     8

	
;;; --- MACRO -----------------------------------------------
;;; --------------------------------------------------------------
;;; readFile( handle, buffer, buffer_length, number_bytes_read )
;;; reads bytes into buffer.  At most buffer_length bytes are
;;; read.  Number of bytes read are stored in dword number_bytes_read.

readFile:
        push    ebp
        mov     ebp, esp
        
%define rf_handle dword [ebp+20]
%define rf_buffer dword [ebp+16]
%define rf_len    dword [ebp+12]
%define rf_bytes  dword [ebp+8]
        
        pushad
        
	mov	eax,SYS_READ
        mov     ebx, rf_handle
        mov     ecx, rf_buffer
        mov     edx, rf_bytes
	int	0x80
        mov     ebx, rf_bytes   ; get the address of variable
        mov     [ebx], eax      ; put # bytes read in it
        
        popad
        pop     ebp
        ret     16

;;; --------------------------------------------------------------
;;; close( handle )
;;; close the file
close:  push    ebp
        mov     ebp, esp
        pushad
        
%define cl_handle dword [ebp+8]
        
        mov	eax,SYS_CLOSE
	mov	ebx,cl_handle
	int	0x80

        popad
        pop     ebp
        ret     4