0

CX is equal to 14 AX is equal to 16

IDIV CX

But somewhy in AL there is 37. There is no mistakes or errors before that line. Where I've made a mistake? Thank you! p.s. writting on Emu8086

Anton Bondar
  • 319
  • 2
  • 4
  • 13
  • 1
    Try doing an `xor dx, dx` before the div. – 500 - Internal Server Error Oct 24 '13 at 23:21
  • You want `cwd` before 16-bit `idiv` (signed division). Zero-extension (of ax into dx:ax) would give wrong results for negative divisors or dividends. See also [IDIV operation in assembly (understanding)](https://stackoverflow.com/q/25489192) / [Why should EDX be 0 before using the DIV instruction?](https://stackoverflow.com/q/38416593) – Peter Cordes May 21 '23 at 12:47

1 Answers1

2

IDIV CX divides the 32-bit value DX:AX by CX, and stores the quotient in AX and the remainder in DX.

Therefore the value of DX prior to the IDIV instruction matters, and you should either sign-extend AX into DX using the CWD instruction (before IDIV), or clear DX using e.g. XOR DX,DX (before DIV).

Michael
  • 57,169
  • 9
  • 80
  • 125