assembly 组件中的模数x86

ecfdbz9o  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(180)

所以我试着做一个程序来求模,但我做不到。
我试过使用idivdiv,但我就是想不出一种方法来这样做。如果你能告诉我如何执行模运算,我将非常感激。

bvpmtnay

bvpmtnay1#

在大多数汇编器中,模运算符是**%**。模运算表示被除数除以除数后的余数
例如,当被除数为13,除数为5时,则取模结果为13 % 5 = 3。x86中的无符号除法由指令DIV提供,它取决于CPU模式:

; 8bit, works with dividend 0 .. (2^16 - 1)      64 KiB
MOV AX, 13  ; dividend
MOV CL,  5  ; divisor
DIV CL      ; Remainder 3 is now in AH, quotient 2 is in AL.

; 16bit, works with dividend 0 .. (2^32 - 1)      4 GiB
MOV AX, 13  ; lower 16 bits of dividend
MOV DX,  0  ; higher 16 bits of dividend
MOV CX,  5  ; divisor
DIV CX      ; Remainder 3 is now in DX, quotient 2 is in AX.

; 32bit, works with dividend 0 .. (2^64 - 1)     16 EiB
MOV EAX, 13  ; lower 32 bits of dividend
MOV EDX,  0  ; higher 32 bits of dividend
MOV ECX,  5  ; divisor
DIV ECX      ; Remainder 3 is now in EDX, quotient 2 is in EAX.

; 64bit, works with dividend 0 .. (2^128 - 1)   256 ???
MOV RAX, 13  ; lower 64 bits of dividend
MOV RDX,  0  ; higher 64 bits of dividend
MOV RCX,  5  ; divisor
DIV RCX      ; Remainder 3 is now in RDX, quotient 2 is in RAX.

对于使用带符号除法IDIV的带符号数,它要复杂得多,请参阅文章Modulo on Wikipedia

相关问题