Blogger news

Followers

Showing posts with label read. Show all posts
Showing posts with label read. Show all posts
Monday, July 21, 2014
//refer libraries
#include<stdio.h>
#include<conio.h>

 void main()   //main function definition
{
 char c[10];// string variable declaration
clrscr();//to clear the screen it explained in conio.h
                printf(“enter your string \n”);
                scanf(“%s”,c);
                printf(“you are entered string is :%s”,c);
                getch();//this function makes to wait a character from standard input or keyboard this function described in conio.h
}
               

Friday, February 21, 2014


ASSUME CS: CODE, DS: DATA

DATA SEGMENT                                       ; start of data segment
MSG1 DB 0DH, 0AH,'ENTER YOUR NAME : ', '$'
MSG2 DB 'WHAT IS YOUR NAME? ', '$'
STR  DB 25 DUP('  '), '$'                               ; 25 bytes of STR array (blank) is declared to store the user entered name.  
X DB 04H                                                      ; X coordinate value
Y DB 12H                                                      ; Y coordinate value
DATA ENDS

DISPLAY MACRO MSG                                       ; DISPLAY macro definition starts
MOV DX, OFFSET MSG
MOV AH, 09H                                               ; display the message whose offset is in DX using 09H
INT 21H                                                         ; with DOS 21H interrupt.
ENDM                                                            ; end of macro definition

CODE SEGMENT
START:
                      MOV AX, DATA                    ; data segment initialization
                      MOV DS, AX
      
                      DISPLAY MSG1                     ; invoke the DISPLAY macro to display MSG1
                      MOV SI, OFFSET STR               ; SI point to the 1st memory location of STR array
         BACK:  MOV AH, 01H                         ; read a character from keyboard using 01H service
                      INT 21H                                   ; with 21h interrupt, then compare with enter key
                      CMP AL, 0DH                         ; if yes terminate, otherwise store in STR array pointed by SI                   JE STOP                                 ; if yes terminate,
                      MOV [SI], AL                          ; otherwise store in STR array pointed by SI
                      INC SI                                      ; increment SI, to point to the next location of STR array
                      JMP BACK                                 ; jump to BACK to read the next character
          STOP:    MOV AH, 07H             ; service number to clear the screen
          MOV AL, 00H                         ; Number of lines to scroll; 00 blank screen.
                      MOV BH, 0FH                         ; attribute of character; 0F white and 00 black
          MOV CX, 00H                         ; CH=00 Y coordinate of upper left corner and
; CL=00 X coordinate of upper right corner.
                      MOV DH, 31H                         ; Number of rows;
                      MOV DL, 79H                         ; Number of columns;
                      INT 10H                                   ; BIOS interrupt to clear the screen is invoked
                      MOV AH, 02H                        ; service number to set the cursor
                      MOV BH, 00H                         ; set video Page number to 0
                     MOV DH, Y                       ; store row number in DH (Y coordinate)
                     MOV DL, X                        ; store the column number (X)  in DL register
                     INT 10H                                    ; BIOS interrupt to set the cursor is invoked.    
                     DISPLAY MSG2                                    ; invoke the DISPLAY macro to display MSG2.
                     DISPLAY STR                                ; invoke the DISPLAY macro to display STR array contents
                     MOV AH, 4CH                         ; terminate the program
                     INT 21H
      CODE ENDS                                           ; end of code segment
      END START                                            ; end of program

Output:

D:\MASM> Masm 7a.asm;
D:\MASM> Link 7a.obj;

D:\MASM> TD 7a.exe
Press F7 to trace the program (line by line execution).


ASSUME DS: DATA, CS: CODE
DATA SEGMENT                                       ; start of data segment
MSG1 DB 0AH, 0DH, "ENTER THE FIRST STRING : ","$"
MSG2 DB 0AH, 0DH, "ENTER THE SECOND STRING : ","$"
MSG3 DB 0AH, 0DH, "STRINGS ARE SAME","$"
MSG4 DB 0AH, 0DH, "STRINGS ARE NOT SAME","$"
MSG5 DB 0AH, 0DH, "LENGTH OF FIRST STRING IS: ","$"
MSG6 DB 0AH, 0DH, "LENGTH OF SECOND STRING IS: ","$"
STR1 DB 100 DUP('  '),"$"                            ;reserve 100 bytes of blank memory location for STR1 array
STR2 DB 100 DUP('  '),"$"                            ;reserve 100 bytes of blank memory location for STR2 array
LEN1 DB ?                                          ; variable to store the length of 1st string.
LEN2 DB ?                                          ; variable to store the length of 2nd  string.
DATA ENDS
                                  
DISPLAY MACRO MSG                             ;DISPLAY macro definition starts here, this macro is used to display  
MOV DX, OFFSET MSG                              ; message, whose offset address is stored in DX using 09h  
MOV AH, 09H                                               ; with 21H DOS interrupt.
INT 21H
ENDM                                                            ; end of macro definition
           
DISP MACRO NUM                                      ;DISP macro definition starts here, this macro is used to display the length
MOV DL, NUM                                               ; of the string ( character display) using 02H. with DOS 21H interrupt.
MOV AH, 02H
INT 21H
ENDM                                                            ; end of macro definition.

CODE SEGMENT                                       ;   code segment starts                     
START: MOV AX, DATA
MOV DS, AX                                     ; initializing data segment
MOV ES, AX                                     ; initializing extra segment
DISPLAY MSG1                               ; invoke DISPLAY macro to display MSG1
MOV SI, OFFSET STR1                           ; SI points to the 1st memory location of STR1 array
CALL   READ                                   ; call READ procedure to read the 1st  string from keyboard
MOV LEN1, BL                                  ; store the length of the 1st string in LEN1 variable
DISPLAY MSG2                                        ; invoke DISPLAY macro to display MSG2
MOV SI, OFFSET STR2                   ; SI points to the 1st memory location of STR2 array
CALL READ                                                 ; call procedure READ to read the1st string from keyboard
MOV LEN2, BL                                  ; store the length of the 2nd string in LEN2 variable.      
MOV AL, LEN1         
CMP AL, LEN2                                               ; compare the length of these two strings.
JNE NOTEQUAL                                             ; if not equal go to label NOTEQUAL
          
MOV SI, OFFSET STR1                          ; SI points to 1st character of STR1 array (source string)
MOV DI, OFFSET STR2                         ; DI points to 1st character of STR2 array (destination string).
MOV CL, LEN1                                ;  store the length of the string in CL ( either string1 or string2)
CLD                                                    ; clear the direction flag for auto increment mode
REPE CMPSB                                 ; compare the string bytes pointed by SI and DI
JNE NOTEQUAL                                  ; if strings are not equal jump to label NOTEQUAL
DISPLAY MSG3                              ; otherwise invoke display macro to display MSG3 (strings are same)
JMP NEXT                                           ; jump to label NEXT, to display the length.

NOTEQUAL: DISPLAY MSG4                   ; invoke display macro to display MSG4 (strings are not same)
NEXT : DISPLAY MSG5                           ;  invoke display macro to display MSG5.
MOV AL, LEN1                                ; store the length of 1st string in AL
CALL   DISP_LEN                            ; Call DISP_LEN procedure to display the length of 1st string.
DISPLAY MSG6                                         ; invoke display macro to display MSG6.
MOV AL, LEN2                                ; store the length of 2nd string in AL.
CALL   DISP_LEN                            ; Call DISP_LEN procedure to display the length of 2nd  string.
MOV AH,4CH                                   ; terminate the program.
INT 21H

READ  PROC NEAR                                  ; start of READ procedure (procedure defined in the same segment)
MOV BL, 00                                                  ; initially BL = 00,  used as counter to store the number of characters .
BACK:  MOV AH, 01H
INT 21H                                                         ; read a character from keyboard, 01h with 21H dos interrupt
CMP AL, 0DH                                               ; compare with enter key.
JE STOP                                                         ; if equal terminate the read process
INC BL                                                           ; after reading a character from keyboard, increment BL by 1
MOV [SI], AL                                                            ; store the character in array pointed by SI
INC SI                                                                        ; increment SI by 1 to point to the next location of array.
JMP BACK                                                    ; jump to BACK to read the next character
STOP:  RET                                                    ; return to main
READ ENDP                                                            ; end of READ procedure
                       
DISP_LEN PROC NEAR                           ; start of DISP_LEN procedure
AAM                                                               ;convert the 2 digit binary (hex) length in AL to unpacked BCD
ADD AX, 3030H                                           ;add AX with 3030H to get the ASCII value of that 2 digit no.
PUSH AX                                                       ; store the AX content temporarily in Stack memory.
DISP AH                                                        ; invoke DISP macro to display the upper digit of the length.
POP AX                                                          ; retrieve AX content from stack
DISP AL                                                         ; invoke DISP macro to display the lower digit of the length
RET                                                                 ; return to main.
DISP_LEN ENDP                                        ; end of DISP_LEN procedure

CODE ENDS                                                 ; end of code segment
END START                                      ; end of program.


Output:
D:\MASM> Masm 6a.asm;
D:\MASM> Link 6a.obj;
D:\MASM> 6a.exe
D:\MASM> 6a.exe

Or
D:\MASM> TD 6a.exe
Then press F7 to trace the program (line by line execution)