six.strings.and.a.bit
six.strings.and.a.bit

Reputation: 527

How to use DIV operation in assembly

.MODEL SMALL
.STACK 64
.DATA

MSGA DB 13,10,"Input first number: ","$"
MSGB DB 13,10,"Input second number:","$"
MSGC DB 13,10,"The quotient is: ","$"
MSGD DB 13,10,"The modulo is: ","$"

NUM1 db ?
NUM2 db ?

.CODE

MAIN PROC NEAR

MOV AX, @DATA
MOV DS, AX

; get first number
LEA DX, MSGA
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV BL, AL

; get second number
LEA DX, MSGB
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV CL, AL
MOV AL, BL

; divide
DIV CL
MOV NUM1, AL
ADD NUM1, '0'
MOV NUM2, AH
ADD NUM2, '0'

; output quotient
LEA DX, MSGC
MOV AH, 09h
INT 21h

MOV DL, NUM1
MOV AH, 02H
INT 21h

; output remainder/modulo
LEA DX, MSGD
MOV AH, 09h
INT 21h

MOV DL, NUM2
MOV AH, 02H
INT 21h

MOV AH, 4Ch
INT 21h

MAIN ENDP
END MAIN

I am new to Assembly language and I'm getting a problem with the DIV operation.

This should output the quotient and remainder if a 1-digit number is divided to a 1-digit number. What is wrong with my code?

Upvotes: 1

Views: 39330

Answers (3)

worriednacho
worriednacho

Reputation: 77

Everything you did in your program is right except one. Division operation uses up the AX register. If you are dividing using AL only, It is an utmost necessity that you must first clear the AH register. If AH has a pre value stored in it, AX register will include that value and the final value of AX would be different than what you expect it to be :)

Also if you aren't using Jumps, Call, Push or Pop, It is unnecessary to use the stack segment so you may delete it :)

Source: Experience

Here is the code with changes made in comments:

.MODEL SMALL
.DATA

MSGA DB 13,10,"Input first number: ","$"
MSGB DB 13,10,"Input second number:","$"
MSGC DB 13,10,"The quotient is: ","$"
MSGD DB 13,10,"The modulo is: ","$"

NUM1 db ?
NUM2 db ?

.CODE

MAIN PROC NEAR

MOV AX, @DATA
MOV DS, AX

; get first number
LEA DX, MSGA
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV BL, AL

; get second number
LEA DX, MSGB
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

;//CHANGES MADE HERE
MOV AH, 00h    ;Div operation requires AX register, if you are using only al, ah must be clear
;//END CHANGES

MOV CL, AL
MOV AL, BL

; divide
DIV CL
MOV NUM1, AL
ADD NUM1, '0'
MOV NUM2, AH
ADD NUM2, '0'

; output quotient
LEA DX, MSGC
MOV AH, 09h
INT 21h

MOV DL, NUM1
MOV AH, 02H
INT 21h

; output remainder/modulo
LEA DX, MSGD
MOV AH, 09h
INT 21h

MOV DL, NUM2
MOV AH, 02H
INT 21h

MOV AH, 4Ch
INT 21h

MAIN ENDP
END MAIN

Upvotes: 1

WAQAR HUSSAIN
WAQAR HUSSAIN

Reputation: 11

.MODEL SMALL
.STACK 100H
.DATA                   
    CF EQU 0DH
    LF EQU 0AH

        ; Data Definition Starts Here

            msg11 db            '                    Enter Divisor  (0 - 9)  : $'
            msg12 db  cf,lf,    '                    Enter Dividend (0 - 9)  : $'
            msg13 db cf,lf,     '                       The Quotient is   : $' 
            msg14 db cf,lf,     '                     ! Division is Impossible !   $' 
            e db ?
            f db ? 

        ; Data Definition Ends Here

            msgch1 DB CF,LF,CF,LF,CF,LF,            '                ******************************************* :  $ '
            msgch2 DB CF,LF,            '                *  Press [ 1 | 0 ] To [ Continue | Exit ]   *  $ '
            msgch3 DB CF,LF,            '                ******************************************* :  $ '
.CODE

    MAIN PROC       
        MOV AX,@DATA                
        MOV DS,AX                   

    divp:
        mov  ah,0   
            mov  al,3
            int  10h

        MOV AX,0600h          
        MOV BH,71H            
        MOV CX,0000H          
        MOV DX,184FH
        INT 10H

        ; Program Starts Here


            MOV AH,9                    
            LEA DX,MSG12                
            INT 21H                 

            MOV AH,1
            INT 21H     
            MOV f,AL        
            INT 21H

            MOV AH,9                    
            LEA DX,MSG11                
            INT 21H                 

            MOV AH,1
            INT 21H     
            MOV e,AL        
            INT 21H

            mov dl,e        
            mov bl,f

            mov al,31h      

            cmp dl,30h
            jle div2

            cmp bl,30h
            jle div4

            cmp dl,bl
            jnle div2

        div1:
            sub bl,e
            add bl,30h

            cmp bl,30h
            jle div3

            add al,31h
            add al,-30h

            jmp div1

        div2:
            mov ah,9
            lea dx,msg14
            int 21h

            jmp divf        

        div3:

            mov ah,9
            lea dx,msg13
            int 21h

            mov ah,2
            mov dl,al
            int 21h

            jmp divf

        div4:

            mov ah,9
            lea dx,msg13
            int 21h

            mov ah,2
            mov dl,30h
            int 21h

            jmp divf


        divf:


        ; Program Ends Here


        ; Repitition Loop Starts Here

            MOV AH,2                
            mov dl,0ah              
            INt 21h

            MOV AH,9                
            LEA DX,msgch1               
            INT 21H

            MOV AH,9                
            LEA DX,msgch2               
            INT 21H

            MOV AH,9                
            LEA DX,msgch3               
            INT 21H

            MOV AH,1
            INT 21H
            MOV BL,AL
            INT 21H

            CMP BL,31H

            jl divlp
            jg divlp

            jmp divp        

        divlp:

        ; Repitition Loop Ends Here

        mov  ah,0   
            mov  al,3
            int  10h    

        MOV AH,4CH
        INT 21H         
        MAIN ENDP       
END MAIN

Upvotes: 1

Jay
Jay

Reputation: 4686

8-bit division using DIV instruction requires AX for dividend and an operand for the divisor.

I've corrected the division part of the code and the stack size. Stack size should be at least 1000, or your program might crash due to insufficient stack storage. Below is the code.

.MODEL SMALL
.STACK 2000
.DATA

MSGA DB 13,10,"Input first number: ","$"
MSGB DB 13,10,"Input second number: ","$"
MSGC DB 13,10,"The quotient is: ","$"
MSGD DB 13,10,"The modulo is: ","$"

NUM1 db ?
NUM2 db ?

.CODE

MAIN PROC NEAR

MOV AX, @DATA
MOV DS, AX

; get first number
LEA DX, MSGA
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV BL, AL

; get second number
LEA DX, MSGB
MOV AH, 09h
INT 21h

MOV AH, 01
INT 21H
SUB AL, '0'

MOV CL, AL

; divide
MOV AH, 0 ; prepare dividend
MOV AL, BL
DIV CL
MOV NUM1, AL
ADD NUM1, '0'
MOV NUM2, AH
ADD NUM2, '0'

; output quotient
LEA DX, MSGC
MOV AH, 09h
INT 21h

MOV DL, NUM1
MOV AH, 02H
INT 21h

; output remainder/modulo
LEA DX, MSGD
MOV AH, 09h
INT 21h

MOV DL, NUM2
MOV AH, 02H
INT 21h

MOV AH, 4Ch
INT 21h

MAIN ENDP
END MAIN

Upvotes: 7

Related Questions