We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,470 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to use DIV operation in assembly

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
3
Contributors
2
Replies
3 Hours
Discussion Span
11 Months Ago
Last Updated
3
Views
jrmeasures
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

What did you enter and what were the results?

ShiftLeft
Light Poster
33 posts since Jun 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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.

Schol-R-LEA
Veteran Poster
1,089 posts since Oct 2010
Reputation Points: 495
Solved Threads: 173
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.2932 seconds using 2.67MB