Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

byte reversal code - need help understanding!

Status
Not open for further replies.

facemanfacey

New Member
hi all, i acquired a piece of code from a website which reverses a byte of code and i am having a little trouble understanding it. could anyone explain to me why he is swapf x,w in the first line, the xorwf x,w in the second line? in my (very shallow) understanding, wouldn't what just return the code back to its original form?

thank you

(i tried to format the code as good as possible)

thomas

; input X = abcdefgh , Output X = hgfedcba
; Written by Dmitry A. Kiryashov 2000
; 12 clocks/words

reverse8bit:

swapf X,W ;efghabcd
xorwf X,W ;efghabcd
;abcdefgh

andlw 0x66 ;.fg..bc.
;.bc..fg.

xorwf X,F ;afgdebch
;
rrf X,W
rrf X,F ;hafgdebc
;
andlw 0x55 ;.a.g.e.c
addwf X,F ;h.f.d.b.
;a.g.e.c.
rrf X,F ;.h.f.d.b
;.a.g.e.c

addwf X,F ;ahgfedcb
;
rlf X,W
rlf X,F ;hgfedcba
;it can be replaced
;with rlf X,W
;if necessary...

Total Program instructions: 7 + 19 Total instructions executed: 13
 
pfft, sorry, i tried to format the code but it just reverted back to its original form... i could also be doing with some help with that aswell!

sorry and thanks

thomas
 
hi,
What you need to do is, post the code, select it, then click the '#' sign in the menu bar... keeps the formatting.:)

Code:
; input X = abcdefgh , Output X = hgfedcba  
; Written by Dmitry A. Kiryashov 2000  
; 12 clocks/words  

reverse8bit: 

	swapf	X,W		;efghabcd
	xorwf	X,W		;efghabcd
;abcdefgh 

	andlw	0x66		;.fg..bc.
;.bc..fg. 

	xorwf	X,F		;afgdebch
;
	rrf	X,W
	rrf	X,F		;hafgdebc
;
	andlw	0x55		;.a.g.e.c
	addwf	X,F		;h.f.d.b.
;a.g.e.c. 
	rrf	X,F		;.h.f.d.b
;.a.g.e.c 

	addwf	X,F		;ahgfedcb
;
	rlf	X,W
	rlf	X,F		;hgfedcba
;it can be replaced  
;with rlf X,W 
;if necessary...
 
I don't know the assembly instructions that you are talking about, but I can show you how I do it in C.

Code:
char Reverse8(char x)
{
    x = ((x >> 1) & 0x55) | ((x << 1) & 0xAA);
    x = ((x >> 2) & 0x33) | ((x << 2) & 0xCC);
    x = ((x >> 4) & 0x0F) | ((x << 4) & 0xF0);
    return x;
}


Code:
int Reverse16(int x)
{
    x = ((x >> 1) & 0x5555) | ((x << 1) & 0xAAAA);
    x = ((x >> 2) & 0x3333) | ((x << 2) & 0xCCCC);
    x = ((x >> 4) & 0x0F0F) | ((x << 4) & 0xF0F0);
    x = ((x >> 8) & 0x00FF) | ((x << 4) & 0xFF00);
    return x;
}

I'll give you an example of what the 8 bit function does:

Original: 01101011

Step 1)
x = ((x >> 1) & 0x55) | ((x << 1) & 0xAA);

(x >> 1) = 00110101
AND with 01010101 (0x55)
result: 00010101

(x << 1) = 11010110
AND with 10101010 (0xAA)
result: 10000010

OR the results: 10010111


Step 2)
(x >> 2) = 00100101
AND with 00110011 (0x33)
result: 00100001

(x << 2) = 01011100
AND with 11001100 (0xCC)
result: 01001100

OR the results: 01101101


Step 3)
(x >> 4) = 00000110
AND with 00001111 (0x0F)
result: 00000110

(x << 4) = 11010000
AND with 11110000 (0xF0)
result: 11010000

OR the results: 11010110

Compare that to the original and you will see the bits have been reversed. The same logic applies to the 16 bit swap. The assembly code you've listed is doing the same thing.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top