Search code examples
assemblyx86x86-16tasm

8086 Assembly - DIV going ape, IP jumping to a weird location endlessly


i've been stuck with this problem for like a day, not finding any relevant information..

there's this one part in my code that takes a number (ex. 259) and separates it's digits into array slots.

SEPERATE_DIGITS:
    mov ax,RESULT               ; result is the number im working on. RESULT dw 259h
    mov si,0
    SEPERATE_DIGITS_LOOP:
        div TEN                 ; TEN dw 10h
        add dl,30h              ; fix-up to print later on
        mov SEPERATED[si],dl    ; store separated digit in my array.
        inc si
        cmp ax,0
    jne SEPERATE_DIGITS_LOOP

i have been debugging it on turbo debugger.. first division works fine. (25 goes to ax, 9 goes to dx).. next division, IP goes ape and just jumps to command "db FE" and again and again in an endless loop.

what the hell am i doing wrong? q:


Solution

  • Here DIV's dividend is a 32-bit values taken from DX (top 16 bits) and AX (low 16 bits).
    You need to zero out DX before every DIV to avoid reusing remainders as part of the dividend.

    Also, start using a debugger. It helps.