0

A piece of my code in assembly x8086 is consistently entering an infinite loop inside the div call:

1. xor bx, bx
2. mov bx, 0002h
3. div bx

the infinite loop occurs in line 3 it just jumps into this code location:

it jumps into cs:1061 then to 1065 then back to div bx and so on.

it jumps into cs:1061 then to 1065 then back to div bx and so on.

how can I fix this? Thanks

Michael
  • 61
  • 1
  • 7
  • 3
    Please make a [mcve]. There are many reasons why this could be the case, the most likely of which is your division overflowing (did you remember to zero out `dx` before dividing?). – fuz Jan 25 '21 at 12:08
  • @fuz yep that was it! i forgot to zero out dx. thanks for the help :D – Michael Jan 25 '21 at 12:13
  • 4
    If you divide by zero or the division result is larger than 0FFFFh, you'll get a division trap. This means that the CPU pushes 3 values on the stack and (in the case of real mode) jumps to the address stored at address 0:0. If the value stored at address 0:0 is 1060h, the CPU jumps to that address... – Martin Rosenau Jan 25 '21 at 13:38
  • `div` is not a "call", its an instruction. – Erik Eidt Jan 25 '21 at 16:18
  • Did you mean to `xor dx,dx`? Looks like a typo where you meant to do that before `mov bx, 2`. Also, just use `shr ax,1` like a normal person. – Peter Cordes Jan 25 '21 at 16:55
  • 1
    @PeterCordes don't be rude please. You are right though I changed that to shr ax, 1. my initial thought was that I need to check the remainder in dx (to see if it was even or odd) but I can just check the carry flag. – Michael Jan 25 '21 at 17:40
  • 1
    Yeah, it's not a drop-in replacement; if you did need the remainder in a register other than FLAGS, you might want `mov dx, ax` / `and dx,1`. Or `setc dl`. Or add it somewhere with `adc`. But never use `div` with a known power-of-2, it's very much slower than a shift. I wasn't intending to be rude, just kind of jokingly point out that div wasn't a good way to do it. But I messed up, and the way I phrased it did look kind of rude, sorry. – Peter Cordes Jan 25 '21 at 17:48
  • @PeterCordes no problem man :) thanks for all the help! – Michael Jan 25 '21 at 18:32

0 Answers0