问题
I have a binary chunk of data 512 bytes long, I was wondering what the most efficient way would be if I wanted to shift it once to the right.
My best guess right now (very new to assembly) would be that I would have to first check a chunk (probably int) to see what it would shift out, shift, then carry in whatever the previous int would have shifted out and proceed carrying this shift down the data. Is there an easier way? If I have to use this carry technique, what's the largest chunk I can shift? DWord? QWord?
回答1:
If you just want to shift once, use the rotate-through-carry instructions.
First, make sure the carry flag is zero. Then:
- Pull 4 bytes into a register
- RCR
- Write back out
- Repeat with the next 4 bytes
回答2:
Shifting under x86 is very simple even for large memory structutes.
1)Set or clear carry flag depend of what do you wont as first bit (LSB) of result.
2)There is no need to pull data in registers you can direct shift 32bits at once in memory like:
rcr dword ptr[edx], 1
or even better
rcr dword ptr[edx + ecx *4], 1
where ecx is a loop counter and edx a memory pointer.
2)Store last shifted carry bit
EDIT: In memory you can shift at once 32bits and don't forget on memory aligment, shift 32bit aligend dwords to increase execution speed.
回答3:
The x86 processors have a Carry flag that works wonderfully for this purpose. There are instructions to shift with the Carry flag, scr
and scl
. http://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate#Shift_With_Carry_Instructions
来源:https://stackoverflow.com/questions/3209851/large-binary-shifts-in-8086-assembly