0

I am making a program in Assembly language x8086 environment. According to me and all possible logic my code is fine but i don't know why it keeps showing me that 'Not Palindrome'. I'll be thankful if anyone can guide me what's wrong in my code.

Code:

.MODEL SMALL
.STACK 64H
.DATA
STRING DB ?
MSG1 DB 'PALINDROME$'
MSG2 DB 'NOT PALINDROME$'
MSG3 DB 'ENTER STRING:  $'
NEW DB 0AH,0DH

.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX

MOV AH,9
LEA DX,MSG3
INT 21H

MOV BX,OFFSET STRING
INPUT:
    MOV AH,1
    INT 21H

    CMP AL,13
    JE FIND_PALINDROME

    MOV [BX],AL
    INC BX
    JMP INPUT

FIND_PALINDROME:
MOV DI,OFFSET STRING
MOV CX,4
CHECK:
    MOV DX,[BX]
    CMP [DI],DX
    JNE EXIT

    INC DI
    DEC BX
    LOOP CHECK

    MOV AH,9
    LEA DX,MSG1
    INT 21H
    JMP EXIT_ALL

EXIT:
    MOV AH,9
    LEA DX,MSG2
    INT 21H

    EXIT_ALL:
    MOV AH,4CH
    INT 21H
    MAIN ENDP
    END MAIN
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ahmad Habib
  • 33
  • 1
  • 9
  • you're doing 2-byte compares (DX is a `word` register), where it looks like you intended to compare 1 byte at a time. Use a debugger to single-step your code and examine register values. – Peter Cordes Aug 15 '18 at 11:08
  • In addition to what @PeterCordes said, BX points just after the string before the comparison starts. Decrement it once. – ninjalj Aug 15 '18 at 11:09
  • @ninjalj: or better: mov the `dec bx` to the top of the loop, before `mov al, [bx]`. Other optimizations: you should be able to use `loope` to count and check the condition at the same time, if you do the inc/dec first. (Then `jne exit` to figure out why you left the loop.) [Why are loops always compiled into "do...while" style (tail jump)?](https://stackoverflow.com/q/47783926). But if you care about modern x86 instead of 8086, you'd avoid the slow `loop` instruction entirely. – Peter Cordes Aug 15 '18 at 11:11
  • @PeterCordes Thanks it worked – Ahmad Habib Aug 15 '18 at 13:56

0 Answers0