Discussion in "8051 Discussion Forum" started by    Rizz    Jan 26, 2019.
Sat Jan 26 2019, 12:01 am
I have written a code to check if a number is a palindrome or not.
But i'm not sure if its valid in all the cases
Can someone help me with possible test cases where this code would fail.

```org 0000h
sjmp start
org 0030h
start:
mov a,#0a5h  //a5h in bin 10100101,its a palindrome,same when read backwards and forward
mov b,a
swap a         //changing no. to 5ah
mov 40h,a            //moving the result to some random address
swap a           //swapping the nibbles
cjne a,40h,exit     //now if value at 40h is equal to the value after swapping its nibble,then its a palindrome.basically proving that addition product's upper nibble == lower nibbles.
//not sure if the logic is exactly correct.
setb 30h    //sets bit high if the no. is a palindrome
exit:nop
end                    //this code works on other no.'s like 81h etc also.
```

Thank you

[ Edited Sat Jan 26 2019, 12:12 pm ]
Sat Jan 26 2019, 08:00 am
What is your definition of a Palindrome ?
The ones I have seen are multi bytes long.
Can you comment your code to explain what every line does.
Sat Jan 26 2019, 12:13 pm
@ExperimenterUK I have updated the post with more comments . Hope its clearer now.
Sun Jan 27 2019, 12:49 am
Yes that helps a lot.
At first sight I can't see that addition is a good test.
I would go for a loop that tests each bit like this.

Make a copy of the test byte in A and B

Is top bit of A same as bottom bit of B ?
if no, then not a palindrome, exit loop
Rotate A left, rotate B right.
repeat 4 times.
If you reach the end, it is a palindrome.

[ Edited Sun Jan 27 2019, 12:09 pm ]

### Recent

xzvCar
Thu Sep 12 2024, 08:00 pm
Sazrpho
Thu Sep 12 2024, 01:27 pm
Malvebria
Thu Sep 12 2024, 01:24 pm
Sazrpig
Thu Sep 12 2024, 01:23 pm
ScottPease
Thu Sep 12 2024, 10:22 am
FeDerHaine
Thu Sep 12 2024, 10:08 am
Mulacrodesia
Thu Sep 12 2024, 12:42 am
Ubrat_qwmr
Wed Sep 11 2024, 04:50 am