我试图找到一个数字的因子,但意外的输出即将来临。为5它应该是120但00即将到来。请帮助以下代码有时会进入无限循环。
.model small
.stack 100h
.data
buffer db 10 dup('$')
n dw 5
.code
main proc
mov ax , @data
mov ds ,ax
mov ax , n
mov bx , offset buffer
mov cx , 1
l1 :
inc cx
mul cx
cmp cx , n
jne l1
l2 :
mov dx, 0
mov cx ,10
div cx
add dl,48
mov [bx], dl
inc bx
cmp ax, 0
jne l1
mov dx , offset buffer ; moving address to dx
mov ah,9 ; printing string
int 21h
mov ax, 4c00h
int 21h
main endp
end main
答案 0 :(得分:3)
l2
循环结束时的跳转不正确。当你应该跳到l1
时,你会跳到l2
。
此外,在您决定是否退出循环之前,在l1
循环中inc
和mul
。因此,在n
为5的情况下,您将获得5 * 2 * 3 * 4 * 5
(即600)。