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)


ASSUME DS:DATA, CS:CODE
DATA SEGMENT                                       ; start of data segment
STR DB 25 DUP(' '),'$'                                 ; reserve 25 bytes blank memory location for STR array(to store
;user entered string)
RSTR DB 25 DUP(' '),'$'                              ; reserve 25 bytes blank memory location for RSTR array( to
;store the reversed string)
COUNT DB  ?                                      ; a variable is declared to store the total no. characters present in
;user entered string.
MSG1 DB 0DH, 0AH, 'ENTER A STRING :', '$'
MSG2 DB 0DH, 0AH, 'REVERSE OF THE STRING IS :', '$'                       
MSG3 DB 0DH, 0AH, 'ENTERED STRING IS PALINDROME', '$'
MSG4 DB 0DH, 0AH, 'ENTERED STRING IS NOT PALINDROME', '$'
DATA ENDS                                                 ; end of data segment
DISPLAY MACRO MSG                             ; DISPLAY macro definition starts here ( used to display the
;various messages on the screen)
MOV DX, OFFSET MSG                                      
MOV AH, 09H                                               ; use service no. 09H to display the message on the screen
INT 21H                                                         ; using DOS interrupt 21H
ENDM
CODE SEGMENT                                       ; end of code segment
START :
                         MOV AX, DATA
                         MOV DS, AX                       ; initializing data segment.
                         MOV ES, AX                        ; initializing extra segment.
                         XOR CX, CX                                    ; clear CX register content
                         MOV SI, OFFSET STR        ; STR array’s 1st memory location is pointed by SI (offset addr)
                         DISPLAY MSG1                   ; macro is invoked to display the 1st message MSG1
BACK: MOV AH, 01H                       ; using 01H service no with DOS interrupt 21H, read
                         INT 21H                                ; a character from keyboard.
                         CMP AL, 0DH                      ;compare the user entered character with the enter key (ascii)           
                         JE STOP                                  ; if it is equal to enter key, then go to STOP, & terminate the read process.
                         MOV [SI], AL                       ; otherwise store the character in STR array
                         INC SI                                   ; after copying a character to the STR array, memory pointer is
; incremented by 1, to point to the next memory location.
                         INC CL                                  ; after reading a character, counter value in CL is incremented by 1
 ; to indicate the length of the string.
                         JMP BACK                                       ; go to BACK and read the next character from keyboard.
STOP:   MOV COUNT, CL                  ; store the length of the input string in COUNT variable.  
                    DEC SI                                   ; SI points to the last character of the input string in STR array.
                         MOV DI, OFFSET RSTR     ; DI points to the 1st location of RSTR array (store the reversed string)
           AGAIN: MOV AL, [SI]                                   
                         MOV [DI], AL                      ; generates the reverse of STR array contents and stores in RSTR
                         INC DI                                  ; DI points the next position in RSTR array.
                         DEC SI                                  ; SI points to the previous character in STR array.
                         LOOP AGAIN                                 ; if count (length) in CL is not zero, then jump to AGAIN
                         DISPLAY MSG2                 ; macro is invoked to display the message MSG2
                         DISPLAY RSTR                  ; invoke the macro to print the reversed string, which is in RSTR
                         MOV CL, COUNT                  ; length is again moved to CL
                        MOV SI, OFFSET STR           ; SI points to the 1st character of the string in STR
                        MOV DI, OFFSET RSTR       ; DI points to the 1st character of the reversed string in RSTR
                        CLD                                        ;clear direction flag for auto increment mode
                        REPE CMPSB                      ; compare the string bytes pointed by SI and DI
                        JNE NEXT                                         ; if strings are not equal jump to label NEXT
                        DISPLAY MSG3                  ; otherwise invoke the macro to display the message MSG3
                        JMP EXIT                                          ; jump to label EXIT to terminate the program
NEXT: DISPLAY MSG4                     ; invoke the macro to display the message MSG4
EXIT:  MOV AH, 4CH                      ; terminate the program
                         INT 21H
       CODE ENDS                                          ; end of code segment.
                   END START                                            ; end of program




Output:
D:\MASM> Masm 5a.asm;
D:\MASM> Link 5a.obj;
D:\MASM> 5a.exe
D:\MASM> 5a.exe
OR
D:\MASM> TD 5a.exe
Then press F7 to trace the program (line by line execution).