Reversing the bytes in a word: (i.e. changing endianness)

Various people came up with code to reverse the endianness of a word (i.e. to take &aabbccdd to &ddccbbaa. Each of their solutions has different things to recommend it, register usage, speed etc, so the best ones are included below.

John.P.Baker@bristol.ac.uk (John P. Baker)
 MOV R2,#&FF
 ORR R2,#&FF0000 ; R2=&00FF00FF
 ;Data In=R0, Out = R1, R2 = &ff00ff
 AND R1,R2,R0,ROR#24 ; R1=&00CC00AA
 AND R2,R2,R0 ; R2=&00BB00DD
 ORR R1,R1,R2,ROR#8 ; R1=&DDCCBBAA
If it is in a loop and you can destroy original R0
 MOV R2,#&FF
 ORR R2,#&FF0000 ; R2=&00FF00FF
 ;Data In=R0, Out = R1, R2 = &ff00ff, R0 corrupted.
 AND R1,R2,R0,ROR#24 ; R1=&00CC00AA
 AND R0,R2,R0 ; R0=&00BB00DD
 ORR R1,R1,R0,ROR#8 ; R1=&DDCCBBAA
tom@compton.demon.co.uk (Tom Hughes)
 ; Data In=R0, Out = R0, R1 Corrupted
 ; r0 is of form abcd
 EOR r1,r0,r0,ROR #16 ; (aEORc)(bEORd)(aEORc)(bEORd)
 BIC r1,r1,#&FF0000 ; (aEORc) 0 (aEORc)(bEORd)
 MOV r0,r0,ROR #8 ; d a b c
 EOR r0,r0,r1,LSR #8 ; d c b a
 ; r0 is now of form dcba
olly@mantis.co.uk (Olly Betts)
 ;outside loop, if we're doing this more than once
 MVN r2, #&FF00 ; r2 = &ff &ff 0 &ff
 ;Data In=R0, Out = R0, R1 Corrupted, R2 = Constant
 EOR r1, r0, r0,ROR #16 ; r1 = aXc bXd aXc bXd
 AND r1, r2, r1,LSR #8 ; r1 = 0 aXc 0 aXc
 EOR r0, r1, r0,ROR #8 ; r0 = d c b a
3n+1 cycles, wins for n>1 and is equal (but uses an extra register) for n=1.

Robin.Watts@prg.ox.ac.uk

AltStyle によって変換されたページ (->オリジナル) /