54 Program to compare two string with contain using String Instruction in 8086 Microprocessor

Code:

; Program to compare two string with contain
.MODEL SMALL
.STACK 100H
.DATA
   STR1 DB 'ATHARV$'
   STR2 DB 'ATHARV$'
   L1   DB  0
   L2   DB  0
   MSG1 DB 'Strings are Same $'
   MSG2 DB 'Strings are not Same $'
.CODE
   MOV AX , @DATA  ; Initializing Data Segment
   MOV DS , AX
   MOV ES , AX    ; Initializing Extra Segment
   ; Counting length of STR1
   MOV DL , 0
   MOV SI , OFFSET STR1    ; Storing base address of STR1
 UP1:
    MOV AL , [SI]
    CMP AL , '$'          ; Compare end of string
    JE DN1                ; Jump If Equal
    INC DL
    INC SI
    JMP UP1               ; Jump to UP1

DN1:
   MOV  L1 , DL           ; Storing Result


   ; Counting length of STR2
   MOV DL , 0
   MOV SI , OFFSET STR2    ; Storing base address of STR2
 UP2:
    MOV AL , [SI]
    CMP AL , '$'          ; Compare end of string
    JE DN2                ; Jump If Equal
    INC DL
    INC SI
    JMP UP2               ; Jump to UP2

DN2:
   MOV  L2 , DL           ; Storing Result
 
   
   ; Comparing the length of str1 and str2
   CMP L1 , DL           
   JE RE
   JMP EXIT2               ; If string not equal
 
RE:
   ; Comparing String Based on Contains
 
   CLD   ; Clear Direction Flag
   MOV CL , L1           ; Initializing Loop Counter
 
   MOV SI , OFFSET STR1
   MOV DI , OFFSET STR2
   
  UP:
  CMPSB     ; Compare character of source string with character
   ; of destination string

  JNZ EXIT2 ; Jump If Not Zero If character are not equal
LOOP UP

; String Same
EXIT1:
   LEA DX , MSG1
   MOV AH , 09H
   INT 21H
   JMP EXIT

; String Not Same
EXIT2:
   LEA DX , MSG2
   MOV AH , 09H
   INT 21H

EXIT:
   MOV AH , 4CH    ; Service routine for exit
   INT 21H
END






Ouput:

Program to compare two string with contain in 8086 Microprocessor


Previous
Next Post »