Blogger news

Followers

Friday, February 21, 2014


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)

0 comments: