2

Write an assembly language program to add all the elements of a table which are between 50 and 100 only. Display the result as the decimal value.

My soln:

.model small 
.stack 64
.data
    table db 0, 25, 50, 75, 100, 125, 150, 175, 200 
    ten db 10
.code
    main proc
        mov dx, @data
        mov ds, ax
        lea si, table
        mov dx, 0
        mov bx, 0
        mov cx, 9
    l2: mov ax, [si]   
        cmp ax,   50
        jb l1
        cmp ax, 150
        ja l1
        add ax, dx
        mov dx, ax
    l1: inc si
        loop l2
    l3: mov dx, 0
        div ten
        add dx, 30H
        push dx
        inc bx
        cmp ax, 0
        jne l3
        mov ah, 02H
    l4: pop dx
        int 21h
        dec bx
        jnz l4 
        mov ax, 4c00H
        int 21H
    main endp 
end main

It's showing an error -->> divide error - overflow.to manually process this error,change address of INT 0 in interrupt vector table.

What's the solution for this. Thanks!

Sachin Bhusal
  • 63
  • 2
  • 10

1 Answers1

4

divide error - overflow.to manually process this error,change address of INT 0 in interrupt vector table.

Very remarkable that emu8086 would make such a suggestion right away!
The solution is to avoid division error (an error that you get because the quotient of the division couldn't fit in the destined register AL)!
The byte sized division div ten that you use, operates on AX and leaves the quotient in AL and the remainder in AH.

Some errors (might) contribute to the problem:

  • You're loading words from a table filled with bytes (mov ax, [si]).
  • You're allowing for a bigger dividend than what the task stated (cmp ax, 150).

This will work:

    xor  dx, dx
    mov  cx, 9
l2: mov  al, [si]
    mov  ah, 0
    cmp  al, 50
    jb   l1
    cmp  al, 100
    ja   l1
    add  dx, ax
l1: inc  si
    loop l2

; At the end of the previous loop, `CX` is zero.
; We can use that for a digit counter instead of `BX`.
; Next division scheme will work fine for dividends up to 2550.

    mov  ax, dx     ; -> AX = 50 + 75 + 100 = 225
l3: div  ten        ; AX / 10  -> Quotient is in AL
    add  ah, 30h    ; Remainder is in AH
    push ax
    inc  cx
    mov  ah, 0
    cmp  al, 0
    jne  l3

l4: pop  dx
    mov  dl, dh
    mov  ah, 02h    ; DOS.PrintChar
    int  21h
    dec  cx
    jnz  l4 
Sep Roland
  • 33,889
  • 7
  • 43
  • 76