Assembly Language - How to do Modulo?

前端 未结 4 515
北荒
北荒 2020-11-28 05:56

Is there something like a modulo operator or instruction in x86 assembly?

4条回答
  •  心在旅途
    2020-11-28 06:34

    If you compute modulo a power of two, using bitwise AND is simpler and generally faster than performing division. If b is a power of two, a % b == a & (b - 1).

    For example, let's take a value in register EAX, modulo 64.
    The simplest way would be AND EAX, 63, because 63 is 111111 in binary.

    The masked, higher digits are not of interest to us. Try it out!

    Analogically, instead of using MUL or DIV with powers of two, bit-shifting is the way to go. Beware signed integers, though!

提交回复
热议问题