← Back to Home

Practical 9

To perform string operations using DOS interrupts.

Theory

  1. What are DOS interrupts and how are they used for string operations in assembly language?
  2. How does INT 21h function 09h work for displaying strings?
  3. How can you read a string from the user using DOS interrupts?
  4. What is the purpose of the buffer in DOS string input operations?
  5. Explain the steps to display a user-entered string back to the screen using assembly language.

Code

Create a file named exp9.asm in Notepad and save it in D:\TASM:

.model small 
.stack 100h 
.data 
msg db "Enter a string: $" 
newline db 0Dh, 0Ah, "$"
output_msg db "You entered: $"
buffer db 20, 0, 21 dup('$') ; max length, actual length, string buffer
.code 
main proc 
  mov ax, @data 
  mov ds, ax 
 
  ; Prompt 
  lea dx, msg 
  mov ah, 09h 
  int 21h 
 
  ; Read string 
  lea dx, buffer 
  mov ah, 0Ah 
  int 21h 
 
  ; Print newline
  lea dx, newline
  mov ah, 09h
  int 21h
  
  ; Print output message
  lea dx, output_msg
  mov ah, 09h
  int 21h
 
  ; Null-terminate the input string
  mov bl, buffer+1    ; get actual length
  mov bh, 0           ; clear high byte
  mov buffer+2[bx], '$' ; add string terminator
 
  ; Display string 
  lea dx, buffer+2    ; offset to actual text 
  mov ah, 09h 
  int 21h 
 
  ; Print newline
  lea dx, newline
  mov ah, 09h
  int 21h
 
  mov ah, 4ch 
  int 21h 
 main endp 
 end main 

    

Compilation and Execution using TASM

Inside DOSBox, compile and run:

    tasm filename.asm
    tlink filename.obj
    

Using Debug Tool

Run Debug:

td filename.exe