ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137File Operations Proced…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    7 min read
    Topic 61 of 73
    Topic Study Note Illustration
    61

    File Operations Procedures

    MASM provides several functions via DOS interrupts (primarily int 21h ) for performing file operations such as opening, reading, writing, and closing files. These operations are handled...

    File Operations in MASM (Microsoft Macro Assembler)

    MASM provides several functions via DOS interrupts (primarily int 21h) for performing file operations such as opening, reading, writing, and closing files. These operations are handled via system calls that allow you to interact with files on disk. Below are examples of file operations in MASM, including file opening, reading, writing, and closing using procedures.


    1. Opening a File

    To open a file in MASM, you use the DOS interrupt int 21h with function 3Dh, which opens a file and returns a file handle.

    MASM Code: Opening a File

    ; open_file.asm
    .model small
    .stack 100h
    
    .data
        filename db 'testfile.txt', 0  ; Filename to open
        handle dw 0                    ; File handle
        filemode db 'r', 0             ; File access mode ('r' for read, 'w' for write)
        errorMessage db 'Error opening file.$', 0
    
    .code
    main:
        mov ax, @data            ; Initialize data segment
        mov ds, ax
    
        ; Call OpenFile procedure
        call OpenFile
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to open a file
    OpenFile proc
        ; Open file using DOS function 3Dh (open)
        lea dx, filename         ; Load address of filename
        mov al, 0                ; '0' = read mode, 'w' = write mode, 'a' = append mode
        int 21h                  ; DOS interrupt 21h, function 3Dh (open)
        jc ErrorOpeningFile      ; Jump if there was an error (carry flag set)
        
        ; Store the returned file handle
        mov [handle], ax
    
        ; File opened successfully, return
        ret
    
    ErrorOpeningFile:
        ; Print error message if file could not be opened
        mov ah, 09h
        lea dx, errorMessage
        int 21h                  ; DOS interrupt 21h to print string
        ret
    
    OpenFile endp
    end main
    

    Explanation:

    • int 21h, function 3Dh: Used to open the file. The filename is passed in DX, and the mode is passed in AL.
    • The file handle returned by the int 21h call is stored in AX.
    • If an error occurs (file not found or invalid mode), the carry flag is set, and the program will jump to ErrorOpeningFile.

    2. Writing to a File

    Writing to a file in MASM involves using DOS interrupt int 21h with function 40h. This function requires the file handle, a pointer to the data, and the number of bytes to write.

    MASM Code: Writing to a File

    ; write_to_file.asm
    .model small
    .stack 100h
    
    .data
        filename db 'testfile.txt', 0  ; File name
        textToWrite db 'Hello, MASM! Writing to a file.$'
        handle dw 0                    ; File handle
        errorMessage db 'Error writing to file.$', 0
    
    .code
    main:
        mov ax, @data            ; Initialize data segment
        mov ds, ax
    
        ; Open the file for writing
        call OpenFile
    
        ; Write to the file
        call WriteToFile
    
        ; Close the file
        call CloseFile
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to open a file
    OpenFile proc
        lea dx, filename         ; Load address of filename
        mov al, 'w'              ; 'w' = write mode
        int 21h                  ; DOS interrupt 21h, function 3Dh (open)
        jc ErrorOpeningFile      ; Jump if error
    
        mov [handle], ax         ; Store the returned file handle
        ret
    
    ErrorOpeningFile:
        mov ah, 09h
        lea dx, errorMessage
        int 21h                  ; Print error message
        ret
    
    OpenFile endp
    
    ; Procedure to write to a file
    WriteToFile proc
        lea dx, textToWrite      ; Load address of text to write
        mov ah, 40h              ; DOS function 40h (write)
        mov bx, [handle]         ; Load the file handle into BX
        mov cx, 18               ; Number of bytes to write
        int 21h                  ; Call DOS interrupt
        jc ErrorWritingFile      ; Jump if there was an error
    
        ret
    
    ErrorWritingFile:
        mov ah, 09h
        lea dx, errorMessage
        int 21h                  ; Print error message
        ret
    
    WriteToFile endp
    
    ; Procedure to close the file
    CloseFile proc
        mov ah, 3Eh              ; DOS function 3Eh (close file)
        mov bx, [handle]         ; Load file handle into BX
        int 21h                  ; Call DOS interrupt
        ret
    
    CloseFile endp
    
    end main
    

    Explanation:

    • OpenFile: Opens a file for writing using the 3Dh function (open file) with 'w' mode.
    • WriteToFile: Writes data to the file using int 21h with function 40h. The file handle is passed through BX, the data to write is in DX, and the number of bytes to write is passed through CX.
    • CloseFile: Closes the file using int 21h with function 3Eh.

    3. Reading from a File

    To read from a file in MASM, you use DOS interrupt int 21h with function 3Fh. This function allows you to read data from a file into a buffer.

    MASM Code: Reading from a File

    ; read_from_file.asm
    .model small
    .stack 100h
    
    .data
        filename db 'testfile.txt', 0  ; File name to read from
        buffer db 100, 0               ; Buffer to store the data read
        handle dw 0                    ; File handle
        errorMessage db 'Error reading file.$', 0
        bytesRead dw 0                 ; Variable to store number of bytes read
    
    .code
    main:
        mov ax, @data            ; Initialize data segment
        mov ds, ax
    
        ; Open the file
        call OpenFile
    
        ; Read from the file
        call ReadFromFile
    
        ; Print the data read (optional)
        call PrintBuffer
    
        ; Close the file
        call CloseFile
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to open a file
    OpenFile proc
        lea dx, filename         ; Load address of filename
        mov al, 'r'              ; 'r' = read mode
        int 21h                  ; DOS interrupt 21h, function 3Dh (open)
        jc ErrorOpeningFile      ; Jump if error
    
        mov [handle], ax         ; Store the returned file handle
        ret
    
    ErrorOpeningFile:
        mov ah, 09h
        lea dx, errorMessage
        int 21h                  ; Print error message
        ret
    
    OpenFile endp
    
    ; Procedure to read from a file
    ReadFromFile proc
        lea dx, buffer           ; Load address of the buffer
        mov ah, 3Fh              ; DOS function 3Fh (read)
        mov bx, [handle]         ; Load the file handle into BX
        mov cx, 100              ; Number of bytes to read
        int 21h                  ; Call DOS interrupt
    
        mov [bytesRead], ax      ; Store the number of bytes read
        jc ErrorReadingFile      ; Jump if there was an error
    
        ret
    
    ErrorReadingFile:
        mov ah, 09h
        lea dx, errorMessage
        int 21h                  ; Print error message
        ret
    
    ReadFromFile endp
    
    ; Procedure to print the buffer (the data read)
    PrintBuffer proc
        mov ah, 09h
        lea dx, buffer
        int 21h                  ; Call DOS interrupt to print string
        ret
    
    PrintBuffer endp
    
    ; Procedure to close the file
    CloseFile proc
        mov ah, 3Eh              ; DOS function 3Eh (close file)
        mov bx, [handle]         ; Load file handle into BX
        int 21h                  ; Call DOS interrupt
        ret
    
    CloseFile endp
    
    end main
    

    Explanation:

    • OpenFile: Opens the file in read mode ('r').
    • ReadFromFile: Reads the contents of the file into the buffer using the int 21h function 3Fh. The number of bytes read is stored in the bytesRead variable.
    • PrintBuffer: Prints the data from the buffer using int 21h, function 09h.
    • CloseFile: Closes the file using int 21h, function 3Eh.

    4. File Operations Summary

    In MASM, file operations can be done using DOS interrupts. Here’s a summary of the key functions for file manipulation:

    • Opening a File: int 21h, function 3Dh
      • Mode (AL): `'
    ← Previous topic 60
    Procedures
    Next topic 62 →
    Labels in Procedures

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language