📄 fibonaci.asm.bak
字号:
data segment
n_v dw 5
result dw ?
messin db 'Please input an integer n(0<=n<=100):',20h,20h,'$'
messout db 'The result of F(n):',20h,20h,'$'
data ends
stack segment
dw 128 dup(?)
tos label word
stack ends
code segment
main proc far
assume cs:code,ds:data,ss:stack
start:
mov ax,stack
mov ss,ax
mov sp,offset tos
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
call input
push bx ;输入n的值
call fib ;计算结果
pop result
call output
ret
main endp
;---------------------------
output proc near
lea dx,messout
mov ah,09h
int 21h
mov bx,result
call binidec
ret
output endp
;---------------------------
input proc near
again:
call crlf
lea dx,messin
mov ah,09h
int 21h
call decibin
cmp bx,100
ja again
cmp bx,0
jb again
call crlf
ret
input endp
;---------------------------
binidec proc near ;用bx传递参数
push bx
push cx
push si
push di
mov cx,10000
call dec_div
mov cx,1000
call dec_div
mov cx,100
call dec_div
mov cx,10
call dec_div
mov cx,1
call dec_div
pop di
pop si
pop cx
pop bx
ret
binidec endp
;---------------------------
dec_div proc near
mov ax,bx
mov dx,0
div cx
mov bx,dx
mov dl,al
add dl,30h
mov ah,02h
int 21h
ret
dec_div endp
;---------------------------
decibin proc near
mov bx,0
newchar:
mov ah,1
int 21h
mov dl,al
sub al,30h
jl exit1
cmp al,9
jg exit1
cbw
xchg ax,bx
mov cx,10
mul cx
xchg ax,bx
add bx,ax
jmp newchar
exit1:
ret
decibin endp
;---------------------------
crlf proc near
mov dl,0ah
mov ah,02h
int 21h
mov dl,0dh
mov ah,02h
int 21h
ret
crlf endp
;---------------------------
fib proc near
push bp ;ax存储要计算F(n)中的n值
mov bp,sp
mov ax,[bp+4]
cmp ax,2
je l1
cmp ax,1
je l1
dec ax ;ax存放要计算F(n)的n值
push ax
call fib
pop ax
sub ax,ax
add ax,[bp-2] ;将F(n-1)和F(n-2)相加
add ax,[bp-8]
jmp exit
l1: mov ax,1
mov word ptr [bp-2],1
exit:
mov [bp+4],ax
pop bp
ret
fib endp
;---------------------------
code ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -