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

JamesNip
Tue Mar 19 2024, 02:57 pm
EdwardFew
Mon Mar 18 2024, 01:14 pm
EmeryPah
Mon Mar 18 2024, 11:51 am
RobertMax
Sun Mar 17 2024, 10:22 pm
DanielJar
Fri Mar 15 2024, 06:52 pm
Tuzaimecor
Fri Mar 15 2024, 02:32 am
PromotionFoode
Thu Mar 14 2024, 08:11 pm
EdwardGeawn
Sun Mar 10 2024, 12:24 pm