用彙編求N!

2021-04-02 02:24:34 字數 4285 閱讀 3101

求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

有些繁索,本人對彙編不大敏感,於是求簡化**……  

常用彙編指令

1 加減指令 2 adr指令 adr是小範圍的位址讀取偽指令,ldr是大範圍的讀取位址偽指令。可實際上adr是將基於pc相對偏移的位址值或基於暫存器相對位址值讀取的為指令,而ldr用於載入32為立即數或乙個位址到指定的暫存器中。到這兒就會看到其中的區別了。如果在程式中想載入某個函式或者某個在聯接時候...

常用彙編指令

一 資料傳輸指令 它們在存貯器和 暫存器 寄存 器和輸入輸出埠之間傳送資料.1.通用資料傳送指令.mov 傳送字或位元組.movsx 先符號擴充套件,再傳送.movzx 先零擴充套件,再傳送.push 把字壓入堆疊.pop 把字彈出堆疊.pusha 把ax,cx,dx,bx,sp,bp,si,di依...

常用彙編指令

1 資料傳送指令 mov move r1,r2 r1 r2 move r1,4096 r1 4096 2 偽指令ldr 用於將乙個大於八位的資料裝入暫存器 ldr r1,0x123456789 r1 0x123456789 ldr r1,label 獲取絕對位址,即label的位址 label 3 ...