装配中的一个数字的因子

时间:2022-04-11 16:51:01

I am trying to find Factorial of a number but a unexpected output is coming. for 5 it should be 120 but 00 is coming. please Help in the following code Sometimes it goes into infinite loop.

我试图找到一个数字的因子,但意外的输出即将到来。为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    

1 个解决方案

#1


The jump at the end of your l2 loop is incorrect. You're jumping to l1 when you should be jumping to l2.

l2循环结束时的跳转不正确。当你应该跳到l2时,你会跳到l1。

Also, in your l1 loop you inc and mul before you decide whether to exit the loop. So in the case where n is 5 you'll get 5 * 2 * 3 * 4 * 5 (which is 600).

此外,在你决定是否退出循环之前,你的l1循环你是inc和mul。因此,在n为5的情况下,您将获得5 * 2 * 3 * 4 * 5(即600)。

#1


The jump at the end of your l2 loop is incorrect. You're jumping to l1 when you should be jumping to l2.

l2循环结束时的跳转不正确。当你应该跳到l2时,你会跳到l1。

Also, in your l1 loop you inc and mul before you decide whether to exit the loop. So in the case where n is 5 you'll get 5 * 2 * 3 * 4 * 5 (which is 600).

此外,在你决定是否退出循环之前,你的l1循环你是inc和mul。因此,在n为5的情况下,您将获得5 * 2 * 3 * 4 * 5(即600)。