implementing SHR in ASM
how can implement SHR in assembly?( without the SHR instructor, of course :-D)
I thought I seperate each bit with AND and then get a new number, but this is way too long...
;stdcall dword shiftRight (dword number, dword shiftCount)shiftRight: push ebp mov ebp, esp mov ebx, 2 mov ecx, [ebp + 12] mov eax, [ebp + 8].divide: xor edx, edx div ebx loop .divide mov esp, ebp pop ebp ret 8
[smile]
This must be slow as hell [smile]
Oxyd
Can’t think why anyone would want to implement a SHR in assembler without actually using the SHR instruction! That’s why it’s there isn’t it?
However, the SHR shifts all the bits in a source operand by the number in the second operand, zeroising the top bits.
Repeated DIV by 2 of the source operand will do it as has been shown already.
Another option, if the number of shifts > 1, is to do a single DIV by 2 and then a SAR by 1 less than the number of desired shifts.
The SAR shifts to the right, but retains the existing value of the top bit. If you know your source operand has a zero top bit, a SAR has the same effect as a SHR.
However, the SHR shifts all the bits in a source operand by the number in the second operand, zeroising the top bits.
Repeated DIV by 2 of the source operand will do it as has been shown already.
Another option, if the number of shifts > 1, is to do a single DIV by 2 and then a SAR by 1 less than the number of desired shifts.
The SAR shifts to the right, but retains the existing value of the top bit. If you know your source operand has a zero top bit, a SAR has the same effect as a SHR.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement