Displaying a String 

String Output:

A string is a list of characters treated as a unit. In programming languages we denote a string constant by using quotation marks, e.g. "Enter first number". In 8086 assembly language, single or double quotes may be used.

In order to display a string we must know where the string begins and ends. The beginning of string is given by its address, obtained using the offset operator. The end of a string may be found by either knowing the length of the string or by storing a special character at the end of the string which acts as a sentinel.

Function 09:

MS-DOS provides subprogram number 9h to display strings which are terminated by the ‘$’ character. In order to use it we must:

  • Ensure the string is terminated with the ‘$’ character.
  • Specify the string to be displayed by storing its address in the dx register.
  • Specify the string output subprogram by storing 9h in ah.
  • Use int 21h to call MS-DOS to execute subprogram 9h.
  • The following code illustrates how the string ‘Hello world’, followed by the Return and Line-feed characters, can be displayed.

    Displaying a string using function 09H
    Title "Display string using function 09H"
    .model small
    .stack 100h
    .data
    message db ‘Hello World‘, 13, 10, ‘$‘
    .code
    .startup 
    
    	; copy address of message to dx
    	mov dx, offset message 
    	mov ah, 9h 	; string output
    	int 21h 	; display string
    	mov ah, 4ch	; exit to DOS
    	int 21h
    end
    

    In this example, we use the .data directive. This directive is required when memory variables are used in a program.

    The offset operator allows us to access the address of a variable. In this case, we use it to access the address of message and we store this address in the dx register.

    Subprogram 9h can access the string message (or any string), once it has been passed the starting address of the string.

    Example:

    Write a program that prompts the user to enter a small letter through the keyboard. The program should read the letter without echo. The program displays then a message with the capital equivalent of the letter the user entered through the keyboard.

    I/O instructions
    Title 'I/O instructions'
    .model small
    .data
    	mes1 db 'Enter a small case character: ','$'
    	mes2 db 'The character converted to capital case letter is: ','$'
    .code
    	MOV AX, @DATA		; Assume Data segment
    	MOV DS, AX
    
    	MOV DX, OFFSET mes1	; Display message mes1
    	MOV AH, 09H
    	INT 21H
    
    	MOV AH, 08H		; Read character without echo
    	INT 21H
    				; AL = Input Charatcer 
    	SUB AL, 20H		; convert character to capital 
    	MOV BL, AL		; Save character 	
    
    	
    	MOV DX, OFFSET mes2	; Display message mes2
    	MOV AH, 09H
    	INT 21H
    
    	MOV DL, BL	
    	MOV AH, 02H		; Display character
    	INT 21H
    	
    	MOV AH, 4CH		; Terminate program and 
    	INT 21H			; Exit to DOS
    			
    end