0

So I wrote the following assembly code to calculate sum of digits of a number (TASM).the problem is it keeps showing me divide by zero.What does this mean and where is the error?I'm a newbie to assembly so any help would be great.Here is the code:

dosseg 
.model small 
.stack 
.data  
nr DB 10,10 dup(?) 
rezultat DB 10,10 dup(?) 
mesaj  db 13,10,'Introduceti numarul:$' 
mesaj_suma db 13,10,'Suma cifrelor numarului este: $' 
.code 
pstart: 
        mov ax,@data 
        mov ds,ax 

        mov ah,09  ; aici se afiseaza mesajul initial de introducere 
        mov dx,offset mesaj ; a numarului 
        int 21h  
    mov ah,0ah  ; functia 10(0ah) citeste un sir de caractere de la       
; tastatura intr-o variabila de memorie 
 mov dx,offset nr 
 int 21h 

 mov si,1 
 mov cl,nr[si]  ; incarc in CL numarul de cifre al numarului introdus 
 and cx,00FFh 
 inc cx  ; CX stocheaza acum ultima pozitie din sirul de cifre 
 xor ax,ax ; stocam rezultatul in AX, pe care il initializam cu zero 

urmatorul_caracter:  
 inc si  ; SI creste de la inceputul sirului spre sfarsit 
 add al,nr[si] 

 sub al,30h ; scadem codul ASCII al lui zero 
 cmp si,cx ; in sir se va merge pana la pozitia cl+1 
 jne urmatorul_caracter 
 xor si,si 
cifra: 

 mov bx,0ah 
 div bx 
 add dl,30h 
 mov rezultat[si],dl 
 inc si 
 xor dx,dx 
 cmp ax,0 
 jne cifra 

 mov ah,9 
 mov dx,offset mesaj_suma 
 int 21h 
caracter: 
 dec si 
 mov ah,02  
 mov dl,rezultat[si]  
 int 21h 
 cmp si,0 
 jne caracter 

 mov ah,4ch 
 int 21h   ; terminarea programului 
END pstart
Lola
  • 218
  • 1
  • 11
  • 1
    Since you only have a single division, you should have looked at that. Then, you should have taken out the instruction set reference to see how `div bx` works. It would have told you that the same error is triggered for overflow and `div bx` uses a 32 bit dividend, with the top 16 bits in `dx`. TL;DR: you should zero `dx`. – Jester May 27 '18 at 10:59
  • okay I'll give it a try now but besides this would you say the code is correct? – Lola May 27 '18 at 11:06
  • 1
    IDK, the comments aren't in English and it's pretty long. Fix your `div` bug then debug it yourself using a debugger so you can single-step. Make sure it gives you the right answer for the right reason. Once you've tested it and think it works, you can post it on https://codereview.stackexchange.com/ if you want more feedback. – Peter Cordes May 27 '18 at 11:12
  • Still not working getting the same divide by zero problem? Could you tell me specifically what instruction I should use? – Lola May 27 '18 at 11:22
  • `xor dx,dx`, right before `div`. – Peter Cordes May 27 '18 at 11:51

0 Answers0