【问题标题】:modulo in Assembly x86汇编 x86 中的模数
【发布时间】:2022-12-11 02:13:50
【问题描述】:

嗨,所以我试着做一个做模数的程序。 我不能这样做。 我尝试使用 Idiv 或 div,但我想不出这样做的方法。如果您能告诉我如何执行取模指令,我将非常感激。 谢谢

【问题讨论】:

    标签: assembly x86


    【解决方案1】:

    模运算符是%或者在大多数汇编程序中。 模运算代表分红被整数除以除数.

    例如当被除数为12,除数为5时,则取模结果为12 % 5 = 2。指令DIV 在 x86 中提供了无符号除法,它取决于 CPU 模式:

    ; 8bit, works with dividend 0..64 KiB
    MOV AX, 12  ; dividend
    MOV CL,  5  ; divisor
    DIV CL      ; Remainder 5 is now in AH, quotient 2 is in AL.
    
    ; 16bit, works with dividend 0..4 GiB
    MOV AX, 12  ; lower 16 bits of dividend
    MOV DX,  0  ; higher 16 bits of dividend
    MOV CX,  5  ; divisor
    DIV CX      ; Remainder 5 is now in DX, quotient 2 is in AX.
    
    ; 32bit, works with dividend 0..4 GiB
    MOV EAX, 12  ; lower 32 bits of dividend
    MOV EDX,  0  ; higher 32 bits of dividend
    MOV ECX,  5  ; divisor
    DIV ECX      ; Remainder 5 is now in EDX, quotient 2 is in EAX.
    
    ; 64bit, works with dividend 0..16 EiB
    MOV RAX, 12  ; lower 64 bits of dividend
    MOV RDX,  0  ; higher 64 bits of dividend
    MOV RCX,  5  ; divisor
    DIV RCX      ; Remainder 5 is now in RDX, quotient 2 is in RAX.
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多