user6287161
user6287161

Reputation: 5

Is there a way to use only shifts and rotates to reverse an array in assembly language?

I am trying to figure out if there is a way to reverse the elements of an array in assembly language, only using shifts and rotates.

Lets say, for example, I a have an array of bytes that has 5 elements:

01, 02, 03, 04, 05

I want it to be transformed to:

05, 04, 03, 02, 01

I know several other ways I could go about this, but I'm trying to do it with shifts and rotates only.

Upvotes: 0

Views: 256

Answers (1)

zx485
zx485

Reputation: 29022

You can use ROL or ROR over WORDs with a (kind of static) BubbleSort algorithm swapping adjacent elements, for example.

EAX points to the first element of the BYTE array

01 02 03 04 05   ; ROL WORD PTR [EAX]  , 8   ==>
02 01 03 04 05   ; ROL WORD PTR [EAX+1], 8   ==>
02 03 01 04 05   ; ROL WORD PTR [EAX+2], 8   ==>
02 03 04 01 05   ; ROL WORD PTR [EAX+3], 8   ==>
02 03 04 05 01   ; ROL WORD PTR [EAX]  , 8   ==>
03 02 04 05 01   ; ROL WORD PTR [EAX+1], 8   ==>
03 04 02 05 01   ; ROL WORD PTR [EAX+2], 8   ==>
03 04 05 02 01   ; ROL WORD PTR [EAX]  , 8   ==>
04 03 05 02 01   ; ROL WORD PTR [EAX+1], 8   ==>
04 05 03 02 01   ; ROL WORD PTR [EAX]  , 8   ==>
05 04 03 02 01   ; ==> DONE!

I did not create a full implementation of this algorithm, but you should get the idea.

Upvotes: 2

Related Questions