Chapter 6: Input and Output Operations in MASM
Introduction:
Importance of input and output operations in assembly language programming
Overview of input and output methods available in MASM
Console Input and Output:
Reading user input from the console using interrupt 21h
Displaying output to the console using interrupt 21h
Handling string input and output operations
File Input and Output:
Opening, reading, writing, and closing files in MASM
Using interrupts 21h and 3Dh for file input/output operations
Handling text files and binary files in MASM
Example:
assembly
Copy code
.model small
.stack 100h
.data
prompt db 'Enter your name: $'
name db 50 dup(0)
filename db 'data.txt', 0
buffer db 50 dup(0)
.code
main proc
mov ax, @data
mov ds, ax
; Console input
lea dx, prompt
mov ah, 9
int 21h
lea dx, name
mov ah, 0Ah
int 21h
; Console output
lea dx, name
mov ah, 9
int 21h
; File output
mov ah, 3Ch ; create or open file
lea dx, filename
mov cx, 0 ; file attributes (normal)
int 21h
mov bx, ax ; save file handle
lea dx, name
mov cx, 50 ; number of bytes to write
mov ah, 40h ; write to file
int 21h
; File input
mov ah, 3Dh ; open existing file
lea dx, filename
int 21h
mov bx, ax ; save file handle
lea dx, buffer
mov cx, 50 ; number of bytes to read
mov ah, 3Fh ; read from file
int 21h
; Display file content
lea dx, buffer
mov ah, 9
int 21h
; Close file
mov ah, 3Eh ; close file
mov bx, ax ; file handle
int 21h
mov ah, 4Ch
int 21h
main endp
end main
In this example, we demonstrate input and output operations in MASM. We have a prompt message, prompt, a name buffer, name, a filename, filename, and a general-purpose buffer, buffer.
For console input, we use the DOS interrupt 21h with function 0Ah. We display the prompt message using the ah register set to 9, and then read the user's input into the name buffer.
For console output, we display the contents of the name buffer using the DOS interrupt 21h with function 9.
For file output, we create or open a file named data.txt using the DOS interrupt 21h with function 3Ch. We write the contents of the name buffer to the file using the DOS interrupt 21h with function 40h.
For file input, we open an existing file using the DOS interrupt 21h with function 3Dh. We read the contents of the file into the buffer using the DOS interrupt 21h with function 3Fh.
Finally, we display the contents of the buffer using the DOS interrupt 21h with function 9.
By understanding input and output operations in MASM, readers can interact with users through console input and output, as well as read from and write to files. They learn how to use interrupt 21h to handle console input and output, as well as file input and output operations. This knowledge enables them to build interactive programs and work with external data sources in their assembly language programs.
0 comments:
Post a Comment