;计算 Fibonacci 数
;
20 =
6765
;
21 =
10946
;
22 =
17711
;
23 =
28657
;
24 =
46368
;
25 =
75025
;
26 =
121393
;
27 =
196418
;
28 =
317811
;
29 =
514229
;
30 =
832040
.MODEL SMALL
.Data
mass1 DB
'input the number(20~30):',
'$'
mass2 DB
'The result is :',
'$'
mass3 DB
0dh,
0ah,
'press q/Q to exit',
0dh,
0ah,
'$'
mass4 DB
0dh,
0ah,
'thank you!',
0dh,
0ah,
'$'
mass5 DB
0dh,
0ah,
'input is error,input again',
0dh,
0ah,
'$'
num DW ?
result1H DW
0;高位
result1L DW
0;低位
result2H DW
0;高位
result2L DW
0;低位
ans db
6 dup(
' '),
0dh,
0ah,
'$';保存答案的十进制数
text DB
'text',
0dh,
0ah,
'$'
.code
MAIN PROC FAR
MOV AX ,@Data
MOV DS ,AX
;
again:
mov ax ,
0;初始化
mov bx ,
0
mov dx ,
0
mov result1L,
0;初始化
mov result1H,
0
mov result2L,
0
mov result2H,
0
mov dx ,
offset mass3;停止输入提示
mov ah ,
9
int
21h
mov dx ,
offset mass1;输入限制提示
mov ah ,
9
int
21h
;
mov ah ,
1
int
21h
cmp al ,
'q';输入停止
jz
return
cmp al ,
'Q';输入停止
jz
return
sub al ,
30h;减去
48
cbw
mov bx , ax
call input;调用输入的子程序
cmp bx ,
20;小于
20 , 大于
30 都是输入错误
jl error
cmp bx ,
30
ja error
mov
num , bx;将输入的值放进
num中
call fibP;调用求fibonacci数的子程序
mov dx ,
offset mass2;运算结果提示
mov ah ,
9
int
21h
mov ax , result2L;低位放进ax中
mov dx , result2H;高位放进dx中
call output;调用输出的子程序
jmp again
error:
lea dx , mass5;输入错误错误提示
mov ah ,
9
int
21h
jmp again
;
return:
mov dx ,
offset mass4;程序结束提示
mov ah ,
9
int
21h
mov ax,
4C00H
int
21h
Main endp
;**********************************************************
;输入子程序
;**********************************************************
input proc near
;键盘输入十进制数放在bx中
;mov bx ,
0
newchar:
mov ah,
1 ;键盘输入
int
21h
sub al,
30h
jl exit ; <
0退出
cmp al,
9
jg exit ; >
9退出
cbw
xchg ax, bx
mov cx,
10
mul cx
xchg ax, bx
add bx, ax
jmp newchar
exit:
ret
input endp
;**********************************************************
;
32位输出子程序
;**********************************************************
output proc near
lea di,ans
add di,
5
m2:
mov bx,
10000
div bx ; 这里是发生溢出之处
push ax
push dx
; ============
; 以下转换
10进制的低
4位
pop dx
mov cx,
4
mov ax,dx
m3: mov dx,
0
mov bx,
10
div bx
add dl,
30h
mov [di],dl;求的余数加
30h然后放进字符数组里面
dec di
loop m3
; ===========
; 以下转换
10进制的高
2位
pop ax
mov cx,
2
m4: mov dx,
0
mov bx,
10
div bx
add dl,
30h
mov [di],dl
dec di
loop m4
; ============
; 输出
10进制串
lea dx,ans
mov ah,
9
int
21h
ret
output endp
;**********************************************************
;调试子程序
;**********************************************************
TextP proc near
mov dx ,
offset text
mov ah ,
9
int
21h
ret
TextP endp
;**********************************************************
;fibonacci数的子程序
;**********************************************************
fibP proc near
cmp
num ,
1;等于
1 的情况
jz fibL1
cmp
num ,
2;等于
2 的情况
jz fibL2
dec
num
call fibP
mov ax , result2L ; 将当前f(
num-
1) 的高位和地位放进dx,ax
mov dx , result2H
mov cx , result1L;然后加上f(
num-
2),即放在result1L,result1H 的值,计算得到f(
num-
1)+f(
num-
2)放进result2中
add result2L , cx
mov cx , result1H
adc result2H , cx
mov result1L , ax ;最后将保存在dx,ax,中的值放进result1中
mov result1H , dx
jmp exitFib
fibL1: ;
num等于
1 的时候
mov result1L ,
1
mov result2L ,
1
jmp exitFib
fibL2: ;
num等于
2 的时候
mov result2L ,
1
dec
num
call fibP
exitFib:
ret
fibP endp
;******************************************************************
end