Dividing in Assembler x86

前端 未结 4 1761
余生分开走
余生分开走 2020-12-22 10:10

My college gave me a exercise:

1. Create a new document in Jasmin

2. Use the AL-Register to to add 9 to 8.

3. Subtract 2.

4条回答
  •  长情又很酷
    2020-12-22 10:40

    The div and idiv instructions don't have forms that take an immediate. They only take one explicit operand (register or memory), with the dividend being implicit in AX, or DX:AX, EDX:EAX, or RDX:RAX. See this answer for how to use them.

    But x86 in 16 and 32-bit mode does have an immediate-division instruction, and it's actually slightly faster than div r/m8 on Intel CPUs before Skylake: aam 7 will divide AL by an immediate 7, placing the quotient in AH, remainder in AL. (https://agner.org/optimize/ says that on Haswell it has 1 cycle lower latency, and 1 cycle better throughput than div r8. And it's 8 uops instead of 9.)

    Note that it's different from mov cl, 7 / div cl, which takes the whole of AX as the dividend, and places quotient in AL, remainder in AH.

    AAM is not available in 64-bit long mode, removed along with the other less-useful legacy BCD instructions. But if it saves uops overall (including mov an immediate to a register), it could be useful. On Skylake, it costs 11 uops vs. 10 for div r8, and has 1c worse throughput, and same latency.

提交回复
热议问题