0

I'm working on a program which gets a number (for example, 42 ) then prints it to the screen using interrupt 21 with ah 2. My programs manages to split the number, but when I get a number like 60 my programs calls interrupt 0h because I'm dividing by 0

How can I overcome this?

This is the code:

PROC printNumber 
    push bp 
    push dx
    push bx
    push ax
    mov bp, sp 

    mov ax, [bp + number]
    mov dx, 0 
    mov bx, 10
    splitNumber:
        cmp ax, 0
        jz exit 
        div bx  
        add dx, '0'
        mov ah, 2
        int 21h 
        jmp splitNumber


    exit:
    pop ax
    pop bx
    pop dx
    pop bp
    retn 2
ENDP printNumber

Thanks! :D

Kidsm
  • 308
  • 2
  • 14
  • Looks like you forgot to zero DX *inside* the loop, before the 2nd `div`. Are you sure this works for `42`? Wouldn't the 2nd division run with dx:ax='2':4=0x32:04, leading to divide overflow? Or no, `int 21h/ah=2` returns the character printed in AL. http://spike.scu.edu.au/~barry/interrupts.html#ah02 So that's broken as well. – Peter Cordes Dec 08 '19 at 14:43
  • Single-step your code in a debugger to see register-values change – Peter Cordes Dec 08 '19 at 14:51
  • @PeterCordes I changed ax and dl to al and dl, but when I'm doing xor dl, dl at the first line of the loop the program prints the first number forever :( – Kidsm Dec 08 '19 at 14:55
  • 1
    `div bl` (8-bit operand size) writes AH and AL instead of DX and AX. Anyway, it prints the same number forever because (like I commented already), `int 21h` with AL=2 copies DL to AL, overwriting the rest of the number. **Use the debugger built-in to emu8086** – Peter Cordes Dec 08 '19 at 15:07

0 Answers0