Monday, May 15, 2023

Chapter 10: Input and Output Operations in MASM



Introduction:


Importance of input and output operations in assembly language programming

Overview of different input and output methods available in MASM

Console Input:


Reading character input from the console

Handling input errors and checking for special keys

Reading numeric input from the console

Console Output:


Displaying characters and strings to the console

Formatting output using escape sequences

Displaying numeric values to the console

File Input and Output:


Reading from and writing to files in MASM

Opening and closing files

Performing sequential and random access file operations

Example:


assembly

Copy code

.model small

.stack 100h


.data

    filename db 'sample.txt', 0

    buffer db 100 dup(0)


.code

main proc

    mov ax, @data

    mov ds, ax


    ; Console input: Read a character from the user

    call readCharacter


    ; Console output: Display the character

    call displayCharacter


    ; File output: Write the character to a file

    call writeToFile


    ; File input: Read the contents of the file

    call readFromFile


    ; Console output: Display the file contents

    call displayString


    mov ah, 4Ch

    int 21h

main endp


; Procedure to read a character from the console

readCharacter proc

    mov ah, 01h

    int 21h ; Read character from console

    mov buffer, al ; Store the character in buffer

    ret

readCharacter endp


; Procedure to display a character to the console

displayCharacter proc

    mov dl, buffer ; Move the character to dl

    mov ah, 02h

    int 21h ; Display character to console

    ret

displayCharacter endp


; Procedure to write a character to a file

writeToFile proc

    mov ah, 3Ch ; Create or open a file

    mov cx, 0 ; Mode: Write

    lea dx, filename ; File name

    int 21h ; Create or open the file


    mov bx, ax ; File handle

    mov ah, 40h ; Write to file

    mov cx, 1 ; Number of bytes to write

    lea dx, buffer ; Data to write

    int 21h ; Write the character to the file


    mov ah, 3Eh ; Close the file

    int 21h ; Close the file

    ret

writeToFile endp


; Procedure to read the contents of a file

readFromFile proc

    mov ah, 3Dh ; Open the file

    mov al, 0 ; Mode: Read

    lea dx, filename ; File name

    int 21h ; Open the file


    mov bx, ax ; File handle

    mov ah, 3Fh ; Read from file

    mov cx, 100 ; Number of bytes to read

    lea dx, buffer ; Buffer to store data

    int 21h ; Read the contents of the file


    mov ah, 3Eh ; Close the file

    int 21h ; Close the file

    ret

readFromFile endp


; Procedure to display a string to the console

displayString proc

    lea dx, buffer ; Address of the string

    mov ah, 09h ; Display string

    int 21h ; Display the string

    ret

displayString endp


end main

In this example, we demonstrate input and output operations in MASM. We have a buffer variable to store input/output data and a filename variable that represents the name of the file.


First, we read a character from the console using the readCharacter procedure. It uses the int 21h interrupt with function 01h to read a character from the console and stores it in the buffer.


Next, we display the character to the console using the displayCharacter procedure. It moves the character from buffer to the dl register and uses the int 21h interrupt with function 02h to display the character.


Then, we write the character to a file using the writeToFile procedure. It creates or opens the file specified in filename, writes the character from buffer to the file, and then closes the file.


After that, we read the contents of the file using the readFromFile procedure. It opens the file specified in filename, reads the contents into the buffer, and then closes the file.


Finally, we display the contents of the file to the console using the displayString procedure. It uses the int 21h interrupt with function 09h to display the string stored in the buffer.


By understanding input and output operations in MASM, readers gain the ability to interact with users, read and write data to files, and display information on the console. This knowledge allows them to build assembly language programs that can handle user input, process data from files, and present results to the user effectively.


No comments:

Post a Comment