2015-05-02 38 views
0

我正在試圖找到一個數字的因子,但意外的輸出即將到來。對於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  

回答

3

在您的l2循環末尾跳轉不正確。當你跳到l2時,你跳到l1

此外,在您決定是否退出循環之前,在您的l1循環中您可以使用incmul。所以在n是5的情況下,你會得到5 * 2 * 3 * 4 * 5(這是600)。

+1

除了邁克爾說的話,輸出將會相反! – Fifoernik

+0

謝謝@michael –