Discussion in "8051 Discussion Forum" started by    Rizz    Jan 26, 2019.
Sat Jan 26 2019, 12:01 am
#1
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
		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
#2
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
#3
@ExperimenterUK I have updated the post with more comments . Hope its clearer now.
Sun Jan 27 2019, 12:49 am
#4
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 ]

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Zijaina
Tue Nov 29 2022, 05:46 am
WilliamTup
Mon Nov 28 2022, 02:22 pm
StanleyAvake
Mon Nov 28 2022, 12:41 am
bvnCar
Sun Nov 27 2022, 10:52 am
Andreasaei
Sun Nov 27 2022, 06:50 am
FrancisCog
Sat Nov 26 2022, 09:48 pm
Irincwb
Tue Nov 22 2022, 03:15 pm
Footballhog
Mon Nov 21 2022, 09:15 am