Blogger news

Followers

Friday, February 21, 2014


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). 

0 comments: