2A. Reading and Displaying a String on the screen - [VTU 4th sem Mp lab program]

vtu mp 2a


Q) Write two ALP modules stored in two different files. one module is to read a character from the keyboard and the other one is to display a character. Use the above two modules to read a string of characters from the keyboard terminated by the carriage return and print the string on the display in the next line.

This is the 2A question in the MP lab syllabus of 4th semester VTU CS/IS Students.

This program has to read a string from the keyboard (character by character until the terminating character, that is ‘Carriage return’ character has been entered). And display them back to the screen. Both tasks, reading and displaying character should be done using macros.

Algorithm:
Step 1: Create a macro which reads a character using ‘01H’ DOS service in a file and name it as ‘read.mac’.
Step 2: Create a macro which reads a character using ‘02H’ DOS service in a file and name it as ‘write.mac’.
Step 3:
i) In the 3rd file (named as any .asm file say, ‘filename.asm’ and include the 2 above macro files created in the data segment.
ii) Call the read macro and store the input character until ‘Enter key’ is pressed   iii) Call write macro to print newline character .
iv) Call write macro to print read string.
Step 4: Stop.


Explanation:

First create two macros for reading and displaying character separately.
What is a Macro?
It’s a piece of code which will perform a simple and small task and whenever you call the macro, the call is replaced with the macro definition. Similar to the language Macros.

  • READ_MACRO
This macro is used to read a character from the keyboard. You will be calling this macro in the code if you want to read a character.
Creating Read macro :
1.In your prompt, type ‘'edit read.mac'’ and hit Enter.
Enter the following code in the file.

In Brief:   
- READ MACRO is the definition of the macro.
‘- '01H'’ is the DOS service/function to read a single character from the keyboard. In order to read a character, first we should move ‘01H’ to AH register. And then call the interrupt ‘INT 21H’. The same is done in the definition.
"“The input will be stored in AL register.”"
- ENDM is the assembler directive to declare the end of the macro.

  • WRITE_MACRO 
This macro is used to display a character to the screen. You will be calling this macro in the code if you want to display a character.
Creating write macro \
In your prompt, type ‘edit write.mac’ and hit enter.
Enter the following code in the file.


- WRITE MACRO line declares the beginning of the macro definition.
‘- '02H'’ is the DOS service/function to display a single character on the screen. In order to display a character, first we should move ‘02H’ to AH register. And then call the interrupt ‘INT 21H’. The same is done in the definition.

"“Data present in the DL register is printed"

- ENDM is the end of the macro.

Now the macros are ready. Lets move on to .asm code.
Enter this below code in your program.asm file,

PROGRAM:


  • .MODEL SMALL
Assembler directive stating that the code contains one data segment and one code segment respectively. 

  • .DATA
  • STR DB 100 DUP(?)
  • MSG1 DB 10,13,"ENTER STRING:$" 
  • MSG2 DB 10,13,"STRING IS:$"
.DATA is the beginning of the data segment.
STR is the location to hold a string of 100 characters, each of byte length(1 byte). And all the 100 bytes are assigned to nothing/NULL using ‘DUP (?)’ .
MSG1 and MSG2 are the memory locations holding two strings for the User interface. 10,13 in each of them says that a carriage return (newline) will be printed before printing the message. And ‘$’ is the terminating character of the string.
  • .CODE
  • INCLUDE READ.MAC
  • INCLUDE WRITE.MAC 
  • MOV AX,@DATA 
  • MOV DS,AX
 .CODE is the beginning of code segment.
Two macros ‘READ.MAC’ and ‘WRITE.MAC’ which were defined earlier, are included here using ‘INCLUDE <macro file name> instruction.
Data segment is initialized in the next lines (compulsory for the codes having/using data segment) (@DATA can not be directly moved to DS because both the parameters for MOV can not be memory locations. Intermediate register AX is therefore used)
  • LEA SI,STR
  • MOV DX,OFFSET MSG1 
  • MOV AH,09H 
  • INT 21H
LEA is used to load the offset address(base address) of the Messages to any register.
 Alternative for ‘'LEA <register> <memory location>' is ‘'MOV <register>,OFFSET <memory location> '

Here offset of STR is loaded in Stack index register in the first line. (initially pointing to the first location of STR and then incrementing SI by 1 as we read a character every time)

Now we need to display the string “Enter string” to the screen so that user can know that he has to enter one. For displaying messages you can use DOS service ‘09H’. ‘09H’ when moved to AH and on interrupt, will display the message to the screen whose offset is found in DX register. 
So move offset of MSG1 to DX. And store ‘09H’ in AH. Call the interrupt. Message will be displayed.
  • IREAD: 
  • READ     
  • MOV [SI],AL  
  • INC SI 
  • CMP AL,13      
  • JNE IREAD
IREAD is the label. READ is the call to the READ.MAC macro. It will input a character from the keyboard and store it in AL register. So copy it from AL to the location pointed by the SI register, which can be accessed as [SI]. Then increment SI using ‘INC SI’,so that you can store next character in the next byte of the array.
Then compare the contents of AL with ASCII value of ‘ENTER’ key (that is 13). Because we need to stop taking input at that case. Comparison is done using CMP AL,13 and if not equal, control is taken to IREAD label to take the next input. Otherwise continued below.
  • LEA SI,STR
  • MOV DL,0AH
  • MOV DX,OFFSET MSG2 
  • MOV AH,09H 
  • INT 21H
Now that input is taken till the ENTER key was pressed, we need to display “"THE STRING IS”:" message with same procedure as before, Load the offset of STR in SI for displaying the characters one after other. ASCII value of ‘ENTER’ is loaded to DL for displaying next contents in the new line. Now display MSG2 with the same old displaying procedure using ‘09H’ on INT 21H.

Now comes the displaying part.

  • IWRITE:  
  • MOV DL,[SI] 
  • INC SI 
  • WRITE 
  • CMP DL,13 
  • JNE IWRITE
IWRITE is a label. 
Character pointed to by SI register is moved into DL register for displaying. SI is incremented after Moving its content into DL, for displaying next character.
WRITE.MAC is called by ‘WRITE’ instruction.
DL content is compared to ASCII value of ‘ENTER’ which is stored in DL.and if not equal, jump back to  IWRITE label to display the next character. Otherwise continue.

  • MOV AH,4CH 
  • INT 21H 
  • END   
After displaying all the characters, terminate program moving 4CH to AH and calling the interrupt 21H. END tells the end of the code.
Output:
ENTER STRING: Codeshuffle    ;type ‘Codeshuffle’ and hit enter
STRING IS: Codeshuffle            ;’Codeshuffle’ is displayed.


Hope this helped. Queries? then drop the comments. Share it so that others come to know about this program in detail. 
Continue next to '3A. Bubble sort program'

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!