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
MOV NUM2, AH

; 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
``````

## All 2 Replies

What did you enter and what were the results?

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