Jump to content
  • Advertisement
Sign in to follow this  

implementing SHR in ASM

This topic is 4992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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)
push ebp
mov ebp, esp

mov ebx, 2
mov ecx, [ebp + 12]
mov eax, [ebp + 8]

xor edx, edx
div ebx
loop .divide

mov esp, ebp
pop ebp
ret 8


This must be slow as hell [smile]


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!