用汇编语言计算n!

[复制链接]
查看11 | 回复1 | 2009-7-10 01:18:53 | 显示全部楼层 |阅读模式
DATASEGMENT
;数据段定义
stringdb
"Please input a number(0-10000):" ,0dh,0ah wrongstringdb
" INPUT ERROR!INPUT ONCE MORE! ",0ah,0dh,'$' inputbufferdb
6,?,6 dup(?)
c10dw
10
;输入时进制转换的数
ndw
?
;要求阶乘的数
mdw
?
;步长
cdw
?
;进位
idw
?
outputbufferdw 30000 dup(?)
DATAENDS;--------------------------------------------------------
STACKSEGMENTPARA STACK 'STACK'
;堆栈段代码
DW
100 DUP(?)
STACKENDS;----------------------------------------------------------
CODESEGMENT
;码段定义
ASSUME
CS:CODE,DS:DATA,SS:STACK
START:
MOV
AX,DATA
MOV
DS,AX
;-------------------------------------------------------
mainproc
call
input
call
fractor
mov
cx,di
routput:
;循环输出
push
cx
mov
di,cx
call
output
pop
cx
dec
cx
cmp
cx,0
jge
routputdisplaylength:
mov
dl,0dh
;
mov
ah,2
;
int
21h
;
mov
dl,0ah
;
mov
ah,2
;
int
21h
;
jmp
Start
;
exit:
mov
ah,07h
int
21h
mov
ax,4c00h
;结束处理
INT
21H
;返回DOS
mainendp
;结束过程
;-------------------------------------------------------
;--------------------------------------------------------
inputproc
near
lea
bx,string
mov
cx, 33disstring:
mov
dl,[bx]
mov
ah,2
int
21h
inc
bx
loop
disstring
jmp
inputinit
wronginput:
;错误输入
lea
dx,wrongstring
mov
ah,9
int
21hinputinit:
;初始化
lea
dx,inputbuffer
;装载输入缓冲区首地址
mov
ah,0ah
;输入功能代码
int
21h
;从键盘输入一个数,以回车键结束
mov
ax,0
;累加器清0
mov
cl,inputbuffer+1
;循环次数
mov
ch,0
lea
bx,inputbuffer+2
;装载字符存放区首地址 inputone:
;
mul
c10
mov
dl,[bx]
cmp
dl,'0'
jb
wronginput
cmp
dl,'9'
ja
wronginput
and
dl,0fh
add
al,dl
adc
ah,0
inc
bx
loop
inputone
mov
n,ax
mov
dl,0dh
mov
ah,2
int
21h
mov
dl,0ah
mov
ah,2
int
21h
ret
inputendp
;-----------------------------------------------------
fractorproc
near
mov
cx,n
mov
i,1d
mov
m,0d
push
dx
mov
di,0d
mov
ax,di
mov
bx,2d
mul
bx
mov
si,ax
pop
dx
mov
outputbuffer[si],1d
ctrli:
mov
c,0
mov
di,0d
ctrldi:
cmp
di,m
jbe
done
jmp
cmpc
done:
push
dx
mov
ax,di
mov
bx,2d
mul
bx
mov
si,ax
pop
dx
mov
ax,outputbuffer[si]
mov
bx,i
mul
bx
add
ax,c
adc
dx,0
mov
bx,10000
div
bx
mov
c,ax
push
dx
mov
ax,di
mov
bx,2d
mul
bx
mov
si,ax
pop
dx
mov
outputbuffer[si],dx
inc
di
jmp
ctrldi
cmpc:
cmp
c,0
ja
three1
jmp
next
three1:
inc
m
mov
ax,c
mov
outputbuffer[si+2],ax
next:
inc
i
cmp
cx,0
jng
if0
loop
ctrli
if0:
mov
di,m
ret
;
;
;
fractorendp
;;-----------------------------------------------------
outputproc
near
;
C2:
push
dx
;
mov
ax,di
;
mov
bx,2d
;
mul
bx
;
mov
si,ax
pop
dx
mov
bx,outputbuffer[si]
;
bidproc
;
mov
cx,10000
;
mov
ax,bx
;
mov
dx,0
;
div
cx
;
mov
bx,dx
;
mov
cx,1000
;
call
ddiv
;
mov
cx,100
;
call
ddiv
;
mov
cx,10
;
call
ddiv
;
mov
cx,1
call
ddiv
;
ret
;
bidendp
;;---------------------------------
ddivproc
;
mov
ax,bx
;
mov
dx,0
;
div
cx
;
mov
bx,dx
;
mov
dl,al
;
add
dl,30h
;
mov
ah,02h
;
int
21h
;
ret
;
ddivendp
;
;
ret
;
outputendp
;;--------------------------------------------
CODEENDS
;结束段
END
START
;结束程序
回复

使用道具 举报

千问 | 2009-7-10 01:18:53 | 显示全部楼层
早说是单片机的,我就不答了,不会。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行