Sign in to follow this  
AssDruid

implementing SHR in ASM

Recommended Posts

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...

Share this post


Link to post
Share on other sites


;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

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this