For a program I'm making the user needs to enter a string and then it will be checked to see if it is a palindrome. So far I've managed to reverse the ordering of the string entered so that I have the original string and the reverse version of it.

The problem is that I don't know how to compare these two strings.

Please if you can help me I would appreciate it.

CR equ 13
LF equ 10


prompt    db    CR,LF,'Please enter a word to be checked:  $'
outmsg    db    CR,LF,'The reverse word is:  $'
instring  db    12,?,'              $'
outstring db    '????????????$'
correct   db    CR,LF,'The word that you have entered is a palindrome$'
incorrect db    CR,LF,'The word that you have entered is not a palindrome$'
firstchar dw     ?


    LEA DX,prompt
    MOV AH,9
    INT 21h
    LEA DX,instring
    MOV AH,0ah
    INT 21h
    LEA SI,instring
    MOV BL,byte ptr[SI + 1]
    INC BL
    MOV BH,0
    LEA firstchar,instring+2
    LEA DI,outstring

    MOV BH,byte ptr[SI]
    MOV byte ptr[DI],BH
    CMP SI,firstchar
    DEC SI
    INC DI

    INC DI
    MOV byte ptr[DI],'$'
    LEA DX, outmsg
    MOV AH,9
    INT 21h
    LEA DX,outstring
    MOV AH,9
    INT 21h
    MOV AH,4ch
    INT 21h

The following will do it:

xor ch,ch                           ;clear ch
lea di, instring
mov cl, [ di + 1 ]                ;cx = length of string
mov di,ds                                
mov es,di                          ;es -> data segment
lea di, outstring                  ;es:di -> output string
mov si, offset instring + 2   ;ds:si -> input string
repe cmpsb                     ;compare strings (abort as soon as two
                              ;characters fail to match)
je                            ;jump to relevant code if strings are the same

For the CMPSB instruction, cx = number of bytes to compare, es:di -> one string, ds:si -> the other string, REPE means continue to compare bytes while they are equal; abort as soon as they are not. REPNE would mean continue to compare bytes while they are not equal, abort as soon as they are. There is a similar instruction CMPSW for comparing words, and also in similar vein are:

lodsb/lodsw ;always used to load single byte/word into al/ax

Be a part of the DaniWeb community

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