Lounge - Compare two string in 8086 assembly language

Asked By Phil Tajonera on 12-Oct-11 02:55 AM
Hi Guys,

Can you help me on this assembly language code. The user will input words in a databank then the user can search a word. If search word is found, it will promt "word is found" and if not "word not found. Im having problem in the compare line.


e200 "Write word in the Databank: $"
e220 "Search a word: $"
e270 "successfully save! $"
e235 "word found! $"
e250 "word not found! $"
a100
mov ah,09      
mov dx,200
int 21
mov bx, 0300    ;allocate memory for 1st input in the databank
mov ah, 01
int 21
mov [bx], ax   ;loop start
inc bx         ;increment by 1
cmp al, 0d     ;if enter was hit
jne 10a
mov ch, 24     
mov [bx], ch 
mov ah, 02
mov dl, 0A
int 21
mov dl, 0d
int 21        
mov ah, 09     ;succecfully save in the bank!
mov dx, 270    
int 21
mov ah, 09     ;display the 1st word at memory 300
mov dx, 0300   
int 21         
mov ah, 02     ;enter next line
mov dl, 0A     ;\
int 21         ;\
mov dl, 0d     ;\
int 21         ;\
mov ah, 09     ;display enter a word ulet
mov dx, 200    
int 21         
mov bx, 0320   ;allocate memory for 2nd input in the databank
mov ah, 01      
int 21
mov [bx], ax   ;loop start
inc bx         ;increment by 1
cmp al, 0d     ;if enter was hit
jne 145
mov ch, 24     
mov [bx], ch   ;ch for 2nd word 
mov ah, 02
mov dl, 0A
int 21
mov dl, 0d
int 21
mov ah, 09     ;succecfully save in the bank!
mov dx, 270    
int 21  
mov ah, 09     ;display the 2nd word at memory 320
mov dx, 0320   
int 21
mov ah, 02     ;enter next line
mov dl, 0A     ;\
int 21         ;\
mov dl, 0d     ;\
int 21         ;\ 
mov ah, 09     ;search word
mov dx, 220
int 21     
mov bx, 400    ;------------allocate for memory for search
mov ah, 01
int 21
mov [bx], ax
inc bx
cmp al, 0d     ;if enter was hit
jne 180
mov al, 24
mov [bx], al
mov ah, 02     ;next line
mov dl, 0A     ;\
int 21         ;\
mov dl, 0d     ;\
int 21         ;
cmp ch, al
cmp cl, al
jne 01c4
mov ah, 09     ;compare if not word found
mov dx, 250
int 21
int 20         
mov ah, 09     ;compare if word  found
mov dx, 235
int 21         
int 20
Jitendra Faye replied to Phil Tajonera on 12-Oct-11 02:58 AM
Here;'s a simple procedure to compare two string in 8086 assembly language. s

strcmp proc near            ;requires the offset of first and second
    push bx cx dx si di
    loop_strcmp_loop1:        ;string in si and di
      mov al,byte ptr cs:[si]   ;if s1s2 then al=2
      inc si
      cmp al,bl
      jg loop_strcmp_great
      cmp al,bl
      jl loop_strcmp_less
      cmp al,'$'
      je loop_strcmp_quit
      cmp bl,'$'
      je loop_strcmp_quit
      jmp loop_strcmp_loop1
      loop_strcmp_quit:
      mov al,1
      pop di si dx cx bx
      ret
    loop_strcmp_great:
      mov al,2
      pop di si dx cx bx
      ret
    loop_strcmp_less:
      mov al,0
      pop di si dx cx bx
      ret
endp
Reena Jain replied to Phil Tajonera on 12-Oct-11 03:10 AM
Hi,

try this code and let me know

lea si, string1 ;ds:si points to first string
lea di, string2 ;ds:di points to second string
dec di
  
lab1:
inc di ;ds:di -> next character in string2
lodsb ;load al with next char from string 1
;note: lodsb increments si automatically
cmp [di], al ;compare characters
jne NotEqual ;jump out of loop if they are not the same
  
cmp al, 0 ;they are the same, but end of string?
jne lab1 ;no - so go round loop again
  
;
;end of string, and the "jne NotEqual" instruction hasn't been executed so they're equal
;
  
lea dx, msg1 ;point ds:dx at message to say so
mov ah, 9 ;print message
int 21h
jmp lab2 ;continue with rest of program
  
NotEqual:
lea dx, msg2 ;not equal, so point ds:dx at appropriate message
mov ah, 9 ;print message
int21h
lab2:
;rest of prog here
  
mov ax, 4c00h
int 21h
  
db msg1 'Strings are equal$'
db msg2 'Strings are not equal$'
Anoop S replied to Phil Tajonera on 12-Oct-11 03:18 AM
basically BL contains "d" and BH contains "e", when compared they are false and the code jumps to the BadCredentials label, like wise if you change BL to contain "e" it will continue and display the msgCorrect string. Im a learner ASM person aswell, so there might be better ways to acheive the below, if there is then apologies to the ASM gurus and please elaborate...



    .data
    msgCorrect db "Credentials Correct","$"
    msgBad db "Credentials BAD","$"
    .code
    main proc
     
    MOV BL,"d" ;Put string in BL to Compare
    MOV BH,"e" ;Put string in BH to compare
    CMP BH,BL ;Compare BH and BL
    JNE BadCredentials ;Jump if not equal
     
    MOV AX, SEG msgCorrect ;Move Segment Number into AX
    MOV DS, AX ;Move Segment number into DS
    MOV AH, 09h ;Function 09 of INT21,Prnt String
    LEA DX, msgCorrect ;Load Effective Address of string
    INT 21h ;Execute Print String
     
    MOV AH, 4Ch ;Function 4C of INT21, exit dos
    INT 21h ;Execute Exit Dos
     
    BadCredentials:
    ;DO SOMETHING LIKE DISPLAY A BAD LOGIN MESSAGE
    MOV AX, SEG msgBad
    MOV DS, AX
    MOV AH, 09h
    LEA DX, msgBad
    INT 21h
     
    MOV AH, 4Ch ;Function 4C of INT21, exit dos
    INT 21h ;Execute Exit Dos
     
     
    main endp
    end main