a86 Palindrome

awuja 0 Tallied Votes 731 Views Share

Hey guys.. need some help with this; I'm pulling my hair out.. i've trawled these boards but can't find anything that answers my question.

I, like many others before me (so it seems!) am trying to create a program to check for a palindrome.. this is what I have so far.

In the compare part.. it's just not working. I know I'm doing something wrong with SI and DI.. I just don't know what.

Any explanations would be greatly appreciate! :)

Thanks,

awuja

JMP START

CR equ 13 ; RETURN is 13D ASCII
LF equ 10 ; LINE FEED is 10D ASCII

letterUnitCount DB 0
letterTensCount DB 0
vowelCount DB 0

getWord: DB "Please enter a word",CR, LF,"$"
letterCountMessage: DB CR, LF, "Number of Characters: ", "$"

msg1: DB CR, LF, "  this is palindrome!", "$"
msg2: DB CR, LF, "  this is not a palindrome!", "$"

inWord: DB 128 DUP (0)

START: 
	LEA DX,getWord ;Output string asking user for word
	MOV AH,09h ;DOS call to output string
	INT 21H ;Execute interrupt 21H

KEYBUF: 
	LEA DI,inWord ;Load inWord start address into DI

NEXTCHAR: 
	MOV AH,01h
	INT 21H ;Read key pressed and echo to screen
	
	CMP letterUnitCount, 10
	JE ROLLOVER
		
UNIT:	
	INC letterUnitCount
	
		
	;CMP AL, 65
	;JE INCRVOWEL
	;CMP AL, 97
	;JE INCRVOWEL
	
STORE:	
	MOV [DI],AL ;store in inWord at position [DI]
	INC DI ;Make DI point to next byte in inWord
	CMP AL,0Dh ;Was it the carriage return key?
	JNZ NEXTCHAR ;NO, get some more characters
	MOV [byte ptr DI],"$" ;YES, add end of string marker
	
	MOV SI, DI
	
COMPARE:

	ADD SI, DI
	DEC SI
	
	MOV AL, [DI]
	
	MOV DL, AL
	MOV AH, 2
	INT 21h
	
	MOV BL, [SI]
	
	MOV DL, BL
	MOV AH, 2
	INT 21h
	
	CMP AL,BL
	JNE NOTPAL
	
	INC DI
	DEC SI
LOOP COMPARE

ISPAL:	
	MOV DX, msg1
	MOV AH, 09h
	INT 21h
	
NOTPAL:
	MOV DX, msg2
	MOV AH, 09h
	INT 21h

Dani AI

Generated

’s input code is fine as a start, but the compare stage has three concrete problems that break the palindrome check: the carriage return (CR) is being stored in the buffer instead of stopping input; the end-pointer math is wrong (the ADD SI, DI in the original makes SI meaningless); and LOOP COMPARE is used with no CX initialized. Those lead to comparing garbage addresses and an unpredictable loop count.

Fix strategy (short): stop storing the CR, compute a proper start and end pointer, and compare while SI < DI (or use CX = length/2). The simplest, safest flow is:

  • Read characters and increment a byte counter (do NOT store the CR).
  • After input, set SI to the buffer start and DI to start + (length - 1).
  • Loop: compare [SI] and [DI]; if equal INC SI / DEC DI; stop when SI >= DI.

Example compare/input sketch (MASM/TASM-style) — this replaces the buggy pointer math and the uninitialized LOOP:

; read input (do not store CR) -> letterUnitCount (CL)
LEA DI, inWord
XOR CX, CX
read_char:
  MOV AH,01h
  INT 21h
  CMP AL,13
  JE finish_input
  MOV [DI],AL
  INC DI
  INC CL
  JMP read_char
finish_input:
  MOV [DI],'$'
  MOV [letterUnitCount],CL

; prepare pointers and compare
LEA SI, inWord
MOV CX,0
MOV CL,[letterUnitCount]
CMP CX,0
JE is_palindrome
DEC CX
LEA DI, inWord
ADD DI, CX       ; DI = start + (length-1)

cmp_loop:
  CMP SI,DI
  JGE is_palindrome
  MOV AL,[SI]
  MOV BL,[DI]
  CMP AL,BL
  JNE not_palindrome
  INC SI
  DEC DI
  JMP cmp_loop

Notes: initialize CX before using LOOP or avoid LOOP entirely; check buffer bounds to prevent overflow; optionally normalize case (convert to upper/lower) if comparisons should be case-insensitive. ’s example (posted here earlier) follows the same corrected-pointer idea and is useful as a full implementation.

Member Avatar for Member #809393
Member #809393

in case anyone has the need for such an example, I found one [here]

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.