Skip to main content
Engineering LibreTexts

13.8: File Operations Examples

  • Page ID
    58404
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This section contains some simple example programs to demonstrate very basic file I/O operations. The more complex issues regarding file I/O buffering are addressed in a subsequent chapter.

    Example, File Write

    This example program writes a short message to a file. The file created contains a simple message, a URL in this example. The file name and message to be written to the file are hard-coded. This helps simplify the example, but is not realistic.

    Since the open/create service is used, the file will be created (even if an old version must be overwritten).

    ;  Example program to demonstrate file I/O.  This example
    ;  will open/create a file, write some information to the
    ;  file, and close the file. Note, the file name and
    ;  write message are hard-coded for the example.
    
    section .data
    
    ; -----
    ;  Define standard constants.
    
    LF             equ     10             ; line feed
    NULL           equ     0              ; end of string
    TRUE           equ     1 
    FALSE          equ     0
    EXIT_SUCCESS   equ     0              ; success code
    STDIN          equ     0              ; standard input
    STDOUT         equ     1              ; standard output
    STDERR         equ     2              ; standard error
    
    SYS_read       equ     0              ; read
    SYS_write      equ     1              ; write
    SYS_open       equ     2              ; file open
    SYS_close      equ     3              ; file close
    SYS_fork       equ     57             ; fork
    SYS_exit       equ     60             ; terminate 
    SYS_creat      equ     85             ; file open/create
    SYS_time       equ     201            ; get time
    
    O_CREAT        equ     0x40 
    O_TRUNC        equ     0x200 
    O_APPEND       equ     0x400 
    
    O_RDONLY       equ     000000q        ; read only
    O_WRONLY       equ     000001q        ; write only
    O_RDWR         equ     000002q        ; read and write
    
    S_IRUSR        equ     00400q 
    S_IWUSR        equ     00200q 
    S_IXUSR        equ     00100q 
    
    ; -----
    ;  Variables for main.
    
    newLine        db       LF, NULL
    header         db       LF, "File Write Example."
                   db       LF, LF, NULL
    fileName       db       "url.txt", NULL  
    url            db       "http://www.google.com"
                   db       LF, NULL
    len            dq       $-url-1
    
    writeDone      db       "Write Completed.", LF, NULL
    fileDesc       dq       0
    errMsgOpen     db       "Error opening file.", LF, NULL
    errMsgWrite    db       "Error writing to file.", LF, NULL
    
    ;--------------------------------------------------------
    
    section     .text 
    global _start 
    _start:
    
    ; -----
    ;  Display header line...
    
        mov     rdi, header 
        call    printString
    
    ; -----
    ; Attempt to open file.
    ; Use system service for file open
    
    ;  System Service - Open/Create
    ;      rax = SYS_creat (file open/create)
    ;      rdi = address of file name string
    ;      rsi = attributes (i.e., read only, etc.)
    
    ; Returns:
    ;      if error -> eax < 0
    ;      if success -> eax = file descriptor number
    
    ; The file descriptor points to the File Control
    ; Block (FCB).  The FCB is maintained by the OS.
    ; The file descriptor is used for all subsequent
    ; file operations (read, write, close).
    
    openInputFile:
        mov     rax, SYS_creat                ; file open/create
        mov     rdi, fileName                 ; file name string
        mov     rsi, S_IRUSR | S_IWUSR        ; allow read/write
        syscall                               ; call the kernel
        
        cmp     rax, 0                        ; check for success
        jl      errorOnOpen
    
        mov qword [fileDesc], rax             ; save descriptor
    ; -----
    ;  Write to file.
    ;  In this example, the characters to write are in a
    ;  predefined string containing a URL.
    
    ; System Service - write
    ;  rax = SYS_write
    ;  rdi = file descriptor
    ;  rsi = address of characters to write
    ;  rdx = count of characters to write ; Returns:
    ;  if error -> rax < 0
    ;  if success -> rax = count of characters actually read
    
        mov     rax, SYS_write
        mov     rdi, qword [fileDesc] 
        mov     rsi, url
        mov     rdx, qword [len] 
        syscall
    
        cmp     rax, 0
        jl      errorOnWrite
           
        mov     rdi, writeDone 
        call    printString
    
    ; -----
    ;  Close the file.
    ;  System Service - close
    ;  rax = SYS_close
    ;  rdi = file descriptor
    
        mov     rax, SYS_close
        mov     rdi, qword [fileDesc] 
        syscall
        
        jmp     exampleDone
    
    ; -----
    ; Error on open.
    ; note, rax contains an error code which is not used 
    ;     for this example.
    
    errorOnOpen:
        mov     rdi, errMsgOpen 
        call    printString
    
        jmp         exampleDone
    
    ; -----
    ; Error on write.
    ; note, rax contains an error code which is not used 
    ;     for this example.
    
    errorOnWrite:
        mov     rdi, errMsgWrite 
        call    printString
    
        jmp     exampleDone
    
    ; -----
    ;  Example program done.
    
    exampleDone:
        mov     rax, SYS_exit 
        mov     rdi, EXIT_SUCCESS syscall
    
    ; **********************************************************
    ;  Generic function to display a string to the screen.
    ;  String must be NULL terminated.
    ;  Algorithm:
    ;      Count characters in string (excluding NULL)
    ;      Use syscall to output characters
    
    ; Arguments:
    ;     1) address, string 
    ; Returns: nothing 
    global printString 
    printString:
        push     rbp
        mov      rbp, rsp 
        push     rbx
        
    ;  Count characters in string.
    
        mov      rbx, rdi
        mov      rdx, 0 
    strCountLoop:
        cmp     byte [rbx], NULL 
        je      strCountDone 
        inc     rdx
        inc     rbx
        jmp     strCountLoop 
    strCountDone:
        cmp     rdx, 0 
        je      prtDone
    
    ;  Call OS to output string.
    
        mov     rax, SYS_write             ; code for write ()
        mov     rsi, rdi                   ; addr of characters
        mov     rdi, STDOUT                ; file descriptor
                                           ; count set above
        syscall                            ; system call
    
    ;  String printed, return to calling routine.
    
    prtDone:
        pop    rbx
        pop    rbp
        ret
    
    ; *******************************************************
    

    This example creates the file which is read by the next example.

    13.8.2 Example, File Read

    This example will read a file. The file to be read contains a simple message, the URL from the previous example. The file name is hard-coded which helps simplify the example, but is not realistic. The file name used matches the previous file write example. If this example program is executed prior to the write example program being executed, it will generate an error since the file will not be found. After the file write example program is executed, this file read example program will read the file and display the contents.

    ;  Example program to demonstrate file I/O.
    
    ;  This example will open/create a file, write some
    ;  information to the file, and close the file.
    
    ;  Note, the file name is hard-coded for this example.
    
    ;  This example program will open a file, read the
    ;  contents, and write the contents to the screen.
    ;  This routine also provides some very simple examples
    ;  regarding handling various errors on system services.
    
    ; ------------------------------------------------------- 
    
    section .data
    
    ; -----
    ;  Define standard constants.
    
    LF                 equ    10            ; line feed
    NULL               equ    0             ; end of string
    
    TRUE               equ    1
    FALSE              equ    0
    EXIT_SUCCESS       equ    0             ; success code
    STDIN              equ    0             ; standard input 
    STDOUT             equ    1             ; standard output
    STDERR             equ    2             ; standard error
    
    SYS_read           equ    0             ; read
    SYS_write          equ    1             ; write
    SYS_open           equ    2             ; file open
    SYS_close          equ    3             ; file close
    SYS_fork           equ    57            ; fork
    SYS_exit           equ    60            ; terminate
    SYS_creat          equ    85            ; file open/create
    SYS_time           equ    201           ; get time
    
    O_CREAT            equ    0x40   
    O_TRUNC            equ    0x200   
    O_APPEND           equ    0x400
    
    O_RDONLY           equ    000000q        ; read only
    O_WRONLY           equ    000001q        ; write only
    O_RDWR             equ    000002q        ; read and write
    
    S_IRUSR            equ    00400q
    S_IWUSR            equ    00200q
    S_IXUSR            equ    00100q
    
    ; -----
    ;  Variables/constants for main.
    
    BUFF_SIZE          equ 255
    
    newLine            db    LF, NULL
    header             db    LF, "File Read Example."
                       db    LF, LF, NULL
    
    fileName           db    "url.txt", NULL
    fileDes c          dq    0
    
    errMsgOpen         db    "Error opening the file.", LF, NULL
    errMsgRead         db    "Error reading from the file.", LF, NULL
    
    ; -------------------------------------------------------
    
    section    .bss
    readBuffer resb     BUFF_SIZE
    
    ; -------------------------------------------------------
    
    section     .text 
    global _start 
    _start:
    
    ; -----
    ;  Display header line...
    
        mov    rdi, header
        call   printString
     
    ; -----
    ; Attempt to open file - Use system service for file open
    
    ;  System Service - Open
    ;      rax = SYS_open
    ;      rdi = address of file name string
    ;      rsi = attributes (i.e., read only, etc.) 
    ;  Returns:
    ;      if error -> eax < 0
    ;      if success -> eax = file descriptor number
    
    ;  The file descriptor points to the File Control
    ;  Block (FCB).  The FCB is maintained by the OS.
    ;  The file descriptor is used for all subsequent file
    ;  operations (read, write, close).
    
    openInputFile:
        mov     rax, SYS_open             ; file open
        mov     rdi, fileName             ; file name string
        mov     rsi, O_RDONLY             ; read only access
        syscall                           ; call the kernel
        
        cmp     rax, 0                    ; check for success
        jl      errorOnOpen
        
        mov    qword [fileDesc], rax      ; save descriptor
      
    ; -----
    ;  Read from file.
    ;  For this example, we know that the file has only 1 line.
    
    ;  System Service - Read
    ;      rax = SYS_read
    ;      rdi = file descriptor
    ;      rsi = address of where to place data
    ;      rdx = count of characters to read 
    ; Returns:
    ;      if error -> rax < 0
    ;      if success -> rax = count of characters actually read
    
        mov    rax, SYS_read
        mov    rdi, qword [fileDesc]
        mov    rsi, readBuffer
        mov    rdx, BUFF_SIZE
        syscall
        
        cmp    rax, 0
        jl     errorOnRead
    
    ; -----
    ;  Print the buffer.
    ;  add the NULL for the print string
    
        mov     rsi, readBuffer
        mov     byte [rsi+rax], NULL
        
        mov     rdi, readBuffer 
        call    printString
        
        printNewLine
    
    ; -----
    ;  Close the file.
    ;  System Service - close
    ;        rax = SYS_close
    ;        rdi = file descriptor
    
        mov     rax, SYS_close
        mov     rdi, qword [fileDesc]
        syscall
    
        jmp     exampleDone
    
    ; -----
    ; Error on open.
    ; note, eax contains an error code which is not used 
    ;     for this example.
    
    errorOnOpen:
        mov     rdi, errMsgOpen 
        call    printString
    
        jmp     exampleDone 
    
    ; -----
    ; Error on read.
    ; note, eax contains an error code which is not used ; for this example.
    
    errorOnRead:
        mov     rdi, errMsgRead 
        call    printString
        
        jmp     exampleDone
        
    ; -----
    ;  Example program done.
    
    exampleDone:
        mov     rax, SYS_exit 
        mov     rdi, EXIT_SUCCESS 
        syscall
    
    ; **********************************************************
    ;  Generic procedure to display a string to the screen.
    ;  String must be NULL terminated.
    ;  Algorithm:
    ;  Count characters in string (excluding NULL)
    ;  Use syscall to output characters
    
    ; Arguments:
    ;     1) address, string 
    ; Returns:
    ;     nothing
    
    global printString 
    printString:
        push    rbp
        mov     rbp, rsp 
        push    rbx
        
    ; -----
    ;  Count characters in string.
    
        mov     rbx, rdi
        mov     rdx, 0 
    strCountLoop:
        cmp     byte [rbx], NULL 
        je      strCountDone     
        inc     rdx
        inc     rbx
        jmp     strCountLoop
    strCountDone:
    
        cmp     rdx, 0 
        je      prtDone
        
    ; -----
    ;  Call OS to output string.
    
        mov     rax, SYS_write                 ; code for write ()
        mov     rsi, rdi                       ; addr of characters
        mov     rdi, STDOUT                    ; file descriptor
                                               ; count set above
        syscall                                ; system call
        
    ; -----
    ;  String printed, return to calling routine.
    
    prtDone:
        pop     rbx
        pop     rbp 
        ret     
    

    The printString() function is the exact same in both examples and is only repeated to allow each program to be assembled and executed independently.


    13.8: File Operations Examples is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?