.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位数字,则应输出商和余数。我的代码出了什么问题?
答案 0 :(得分:5)
使用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
答案 1 :(得分: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
答案 2 :(得分: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