0

Another problem :) when I do the division with 16-bit operand, then:

DX:AX = AX : r/m16 <-- 16bit(word) operand

the program crashes. This is the program:

stack SEGMENT PARA STACk

    db      ?

stack ENDS

data SEGMENT PARA PUBLIC

    db      ?

data ENDS

code SEGMENT PARA PUBLIC

    ASSUME ss: stack, ds: data, cs: code

_start:
    mov     ax, data
    mov     ds, ax

    mov     ax, 10
    mov     dx, 2
    div     dx

    ;output number 10/2 = 5 + 48 = 53(Ascii code '5')
    add     ax, 48
    mov     dx, ax
    mov     ah, 02h
    int     21h

    mov     ah, 04ch
    mov     al, 1
    int     21h

code ENDS
       END _start

From what I understand it happens due to an overflow, but where is the overflow here? also the result of the division is saved in DX and AX? What causes an overflow?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mattstack
  • 103
  • 6
  • Look at [this description](https://ulukai.org/ecm/doc/insref.htm#insDIV) of `div`: "For DIV `r/m16`, `DX:AX` is divided by the given operand; the quotient is stored in `AX` and the remainder in `DX`." If the divisor is zero or the quotient doesn't fit into the quotient target register then a divide error exception is thrown. If you divide by `dx` either `dx` is zero or the result would be `>= 1_0000h` which doesn't fit into `ax`. Your formula `DX:AX = AX : r/m16` is incorrect. It is correct for `mul`. `div` is `ax = dx:ax / r/m16` and `dx = dx:ax % r/m16` – ecm Apr 04 '21 at 13:40
  • thanks the site I was referring to said this: dx: ax = ax: r / m16, from what I see however it is not correct, I had to understand it when it defined a word as 32bit – mattstack Apr 04 '21 at 13:46
  • What site were you referring to? – ecm Apr 04 '21 at 13:56
  • In x86 terminology, a word is always 16-bit. Consult Intel's manuals for correct pseudo-code: https://www.felixcloutier.com/x86/div – Peter Cordes Apr 04 '21 at 19:16

0 Answers0