Palindrome Check[updated with comments]
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.
Thank you
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 add a,b //adding 5ah and a5h 5ah+a5h=bin(1111 1111) 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.
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.
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 ]
Powered by e107 Forum System