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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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.