1.11M Members

Assembly language comparing strings


im really new to this programming language and im having a hard time in strings. can anyone teach me how to compare strings the easiest way. what im doing is a login program and i have to compare the inputed username to a string.
a = daniweb

is input = a
if yes then
if no then



Hi, I hope this helps, 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...

       msgCorrect db "Credentials Correct","$"
       msgBad  db "Credentials BAD","$"
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
               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

assumming that the strings are null terminated, the way to do it is:

lea si, string1                           ;ds:si points to first string
lea di, string2                           ;ds:di points to second string
dec di                                    
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
lea dx, msg2                             ;not equal, so point ds:dx at appropriate message
mov ah, 9                                 ;print message
;rest of prog here
mov ax, 4c00h
int 21h
db  msg1  'Strings are equal$'
db  msg2  'Strings are not equal$'

In a real life program the above would be best in a subprocedure, rather than the main body of the code.

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article