ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›File Operations Procedures
    Computer Organization and Assembly LanguageTopic 61 of 73

    File Operations Procedures

    7 minread
    1,172words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,172
      Code examples0
      DifficultyIntermediate