用汇编求N!

这篇博客展示了如何使用汇编语言实现输入一个小于10的整数n,然后计算并显示n的阶乘。代码中包含数据段定义、用户输入处理、阶乘计算过程以及结果的屏幕显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

求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

           
          有些繁索,本人对汇编不大敏感,于是求简化代码……  
            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值