1
.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

我是汇编语言的新手,我遇到了 DIV 操作的问题。

如果 1 位数除以 1 位数,这应该输出商和余数。我的代码有什么问题?

4

3 回答 3

6

DIV使用指令的8 位除法需要AX被除数和除数的操作数。

我已经更正了代码的除法部分和堆栈大小。堆栈大小至少应为 1000,否则您的程序可能会由于堆栈存储空间不足而崩溃。下面是代码。

.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
于 2012-07-18T16:47:37.790 回答
1
.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
于 2013-04-22T16:15:16.133 回答
1

你在你的程序中所做的一切都是正确的,除了一个。除法运算用完 AX 寄存器。如果您只使用 AL 进行除法,则必须首先清除 AH 寄存器。如果 AH 中存储了一个预值,则 AX 寄存器将包含该值,并且 AX 的最终值将与您期望的不同:)

此外,如果您不使用 Jumps、Call、Push 或 Pop,则无需使用堆栈段,因此您可以将其删除 :)

资料来源:经验

这是在注释中进行更改的代码:

.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
于 2013-09-12T18:07:44.530 回答