CSC231 FileIO2.inc

From dftwiki3
Jump to: navigation, search

;;; fileio2.inc
;;; D. Thiebaut
;;; a collection of useful macros to deal with files
;;; and read the contents of a file specified as the first
;;; argument on the command line.


%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 O_RDWR          2


;;; --- MACRO -----------------------------------------------
;;;     writeStr message, messageLength
%macro  writeStr2 2
        mov     eax,SYs_WRITE
        mov     ebx,STDOUT
        mov     ecx,%1
        mov     edx,%2
        int     0x80
%endmacro


;;; --- MACRO -----------------------------------------------
;;;     dumpStr "literal string"
%macro  dumpStr 1
        section .data
%%string db     %1
         db     0x0A
%%strLen equ    $-%%string
        section .text
        writeStr2 %%string, %%strLen
%endmacro


;;; --- MACRO -----------------------------------------------
;;;     openFile filename, [handle], errorMsg, errorLen
%macro  openFile 4
        mov     eax,SYs_OPEN
        mov     ebx,%1
        mov     ecx,O_RDWR
        mov     edx,0
        int     0x80
        test    eax,eax
        jns     %%readFile
        writeStr2 %3, %4                ; there was an error.  Stop!
        mov     eax,SYs_EXIT
        mov     ebx,0
        int     0x80            ; final system call

%%readFile:
        mov     %2,eax
%endmacro


;;; --- MACRO -----------------------------------------------
;;;     readFile [handle], buffer, buffer-length, number-bytes-read
%macro  readFile 4
        mov     eax,SYs_READ
        mov     ebx,%1          ; file descriptor in bx
        mov     ecx,%2          ; buffer address
        mov     edx,%3          ; max number of bytes to read
        int     0x80
        mov     [%4],eax        ; save number bytes read
%endmacro


;;; --- MACRO -----------------------------------------------
;;;     writeBuf [handle], buffer, noBytesToWrite
%macro  writeBuf 3
        mov     eax,SYs_WRITE
        mov     ebx,%1
        mov     ecx,%2
        mov     edx,[%3]
        int     0x80
%endmacro


;;; --- MACRO -----------------------------------------------
;;;     seek0   [handle]
%macro  seek0   1
        mov     eax,SYs_LSEEK
        mov     ebx,%1
        mov     ecx,0
        mov     edx,SEEKSET
        int     0x80
%endmacro


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