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?

.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

Edited 4 Years Ago by jrmeasures: Missing line of code

With an 8-bit division, the dividend is held in AX, with AH being the high bit and AL being the low bit (naturally enough). This means that you need to have a suitable value in both AH and AL before dividing.

In this case, since you are only dividing one digit numbers, the best solution is to simply clear AH:

    MOV CL, AL
    MOV AL, BL
    MOV AH, 0      ; clear AH

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

There may be other issues with the code as well, but that is the one which stands out to me.

This article has been dead for over six months. Start a new discussion instead.