Reputation: 5
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
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