hi,
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.
example:
a = daniweb

is input = a
if yes then
.....
if no then
......
end

thanks..

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

.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

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                                    
 
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$'

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.