Discussion in "8051 Discussion Forum" started by    Rizz    Jan 25, 2019.
Fri Jan 25 2019, 06:31 PM
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
		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
		end                    //this code works on other no.'s like 81h etc also.

Thank you

[ Edited Sat Jan 26 2019, 06:42 AM ]
Sat Jan 26 2019, 02:30 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, 06:43 AM
@ExperimenterUK I have updated the post with more comments . Hope its clearer now.
Sat Jan 26 2019, 07:19 PM
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, 06:39 AM ]

Get Social


Powered by e107 Forum System


Sat Mar 06 2021, 05:44 AM
Hello, just wanted to tell you, I liked this blog post. It was practical. Keep on posting! https://kratomwwwtea.com/where-to-buy-kratom.html kratom portland kratom portland



Thu Jun 25 2020, 03:46 PM
Mon Jun 22 2020, 03:03 PM
Tue Jun 16 2020, 05:10 PM
Sat Jun 13 2020, 09:43 PM
Thu May 21 2020, 12:44 PM
Thu May 21 2020, 01:28 AM
Wed May 20 2020, 07:05 PM
Wed May 20 2020, 11:52 AM


Guests: 79, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123