1

I have a little problem with a div, i need some help with this problem, I have an application in TASM, I must find the multiple of 3 in a series of numbers, the problem is when i do a div the program freezes and i have no idea why.

My code is:

.model small
.stack 1000h
.data

msj1 db 13,10,'Tirame un numero: ','$'
msj2 db 13,10,'ES MULTIPLO DE 3 : ','$'
msj3 db 13,10,'NO ES MULTIPLO DE 3 : ','$'
var1 db ?
cont db 0

.code
.startup

call limpia
mov ah,09h
lea dx, msj1 ;desplegar numero 1:
int 21h

call leer ;lee primer numero
sub al,30h ;restar 30h para obtener el numero
mov var1,al ;lo guardo en var1
mov ah,09h

mov cl,al
mov cont,1

 ciclo:  
            ;push cont
            mov al ,cont
            mov bl,3
            div bl ;this is where the prog freezes , if you comment this line the prog runs

            cmp ah,'0'          
            je multiplo
            jne nomult   

            multiplo :
                mov ah,09h
                lea dx, msj2 ;desplegar numero 2:
                int 21h

                mov dl,cont ;mover al a dl para imprimir
                add dl,30h ;sumar 30 para obtener caracter
                mov ah,02h ;imprimir caracter
                int 21h
                inc cont

            nomult:
                mov ah,09h
                lea dx, msj3 ;desplegar numero 2:
                int 21h     

                mov dl,cont ;mover al a dl para imprimir
                add dl,30h ;sumar 30 para obtener caracter
                mov ah,02h ;imprimir caracter
                int 21h     
                inc cont    

loop ciclo
.exit

limpia proc near
mov ah,00h
mov al,03h
int 10h
ret
limpia endp

leer proc near
mov ah,01h;leer caracter desde el teclado
int 21h;lee primer caracter
ret
leer endp
end
Michael Petch
  • 46,082
  • 8
  • 107
  • 198

2 Answers2

1

div bl is dividing ax but you only load al. Presumably ah has leftover value in it, so the div overflows. Try loading ax for example by using movzx ax, cont.

Also, learn to use a debugger in conjuction with the instruction set reference.

Jester
  • 56,577
  • 4
  • 81
  • 125
1

When the dividend is 8 bits, the quotient will be stored in AL. Therefore you need to make sure that the quotient will fit in 8 bits, or your program will crash.

You can achieve this by clearing the AH register before the division, e.g. using one of the following methods.

CBW   ; only if AL is unsigned (00h..7Fh)

; or..

MOV AH,0

; or..

XOR AH,AH
Michael
  • 57,169
  • 9
  • 80
  • 125