求n! 当n>0时, n!=n*(n-1)!
当n=0时, n!=1
我想实现的是: 输入一个数(先考虑小于10的整数),把n存入al中,n!存入bx中, 能在屏幕上以十进制的形式显示出来。下面是我的实现代码:
data segment
msg db 'input a number: n = '
db '$'
msg1 db ' The n is: al = '
db '$'
msg2 db ' The factor of n is: bx = '
db '$'
n db ?
n1 db '0'
n2 db '0'
n3 db '0'
n11 dw '0'
n22 dw '0'
n33 dw '0'
n44 dw '0'
n55 dw '0'
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov dx,offset msg
mov ah,09h
int 21h
mov ah,01h
int 21h
mov ah,0
sub al,30h
mov n,al
cmp al,1
jbe bottom
dec al
mov cl,al
mov al,n
mov ah,0
mov ch,0
again:
mul cx
loop again
jmp finish
bottom:
xor ah,ah
mov al,n
mov dx,offset msg1
mov ah,09h
int 21h
mov al,30h
mov dl,al
mov ah,02h
int 21h
mov dx,offset msg2
mov ah,09h
int 21h
mov dl,31h
mov ah,02h
int 21h
mov al,0
mov bx,01h
jmp over
finish:
push ax
mov dx,offset msg1
mov ah,09h
int 21h
mov dl,n
add dl,30h
mov ah,02h
int 21h
mov dx,offset msg2
mov ah,09h
int 21h
pop ax
push ax
mov cx,0ah
cmp ax,cx
ja find2
jmp disp
find2:
mov bx,78h
cmp ax,bx
jae find3
div cl
mov n1,ah
mov ah,0
mov n2,al
add n2,30h
mov dl,n2
mov ah,02h
int 21h
add n1,30h
mov dl,n1
mov ah,02h
int 21h
jmp over
find3:
mov bx,078h
cmp ax,bx
ja find4
div cl
mov n1,ah
mov ah,0
div cl
mov n2,ah
mov n3,al
add n3,30h
mov dl,n3
mov ah,02h
int 21h
add n2,30h
mov dl,n2
mov ah,02h
int 21h
add n1,30h
mov dl,n1
mov ah,02h
int 21h
jmp over
find4:
mov bx,13b0h
cmp ax,bx
jae find5
mov dx,0
mov cx,0ah
div cx
mov n11,dx
mov dx,0
div cx
mov n22,dx
mov dx,ax
mov n33,dx
add n33,30h
mov dl,byte ptr n33
mov ah,02h
int 21h
add n22,30h
mov dl,byte ptr n22
mov ah,02h
int 21h
add n11,30h
mov dl,byte ptr n11
mov ah,02h
int 21h
jmp over
find5:
mov bx,9d80h
cmp ax,bx
jae find6
mov dx,0
mov cx,0ah
div cx
mov n11,dx
mov dx,0
div cx
mov n22,dx
mov dx,0
div cx
mov n33,dx
mov n44,ax
add n44,30h
mov dl,byte ptr n44
mov ah,02h
int 21h
add n33,30h
mov dl,byte ptr n33
mov ah,02h
int 21h
add n22,30h
mov dl,byte ptr n22
mov ah,02h
int 21h
add n11,30h
mov dl,byte ptr n11
mov ah,02h
int 21h
jmp over
find6:
mov dx,0
mov cx,0ah
div cx
mov n11,dx
mov dx,0
div cx
mov n22,dx
mov dx,0
div cx
mov n33,dx
mov dx,0
div cx
mov n44,dx
mov n55,ax
add n55,30h
mov dl,byte ptr n55
mov ah,02h
int 21h
add n44,30h
mov dl,byte ptr n44
mov ah,02h
int 21h
add n33,30h
mov dl,byte ptr n33
mov ah,02h
int 21h
add n22,30h
mov dl,byte ptr n22
mov ah,02h
int 21h
add n11,30h
mov dl,byte ptr n11
mov ah,02h
int 21h
jmp over
disp:
mov dl,al
add dl,30h
mov ah,02h
int 21h
jmp over
pop ax
mov bx,ax
mov al,n
over:
pop ax
mov bx,ax
mov al,n
mov ah,4ch
int 21h
code ends
end start
有些繁索,本人对汇编不大敏感,于是求简化代码……