所以我试着做一个做模的程序。我不能这样做。
我尝试使用idiv
或div
,但我只是想不出一种方法来做到这一点。如果你能告诉我怎么做模运算,我将非常感激。
模算子%或在大多数汇编程序中。模运算表示余数在股息被除数整除.
如被除数为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文章。