;一个汇编语言编写的斐波那契数列
;输入:n = 7
;输出:1 1 2 3 5 8 13
;题目链接:http://zhidao.baidu.com/question/582889882284037845.html
;做而论道编写的程序如下:
;斐波那契数列
;---------------------------
ASSUME CS:CC, DS:QQ
;---------------------------
QQ SEGMENT
M1 DB 13, 10, 'Please input n = $'
M2 DB 13, 10, 'Fibonacci Sequence is: $'
N DW ?
F1 DW 0
F2 DW 1
QQ ENDS
;---------------------------
CC SEGMENT
START:
MOV AX, QQ
MOV DS, AX
;
LEA DX, M1
MOV AH, 9
INT 21H
CALL INPUT
CMP CX, 1
JB EXIT
MOV N, CX
LEA DX, M2
MOV AH, 9
INT 21H
MOV DL, '1'
MOV AH, 2
INT 21H
MOV DL, ' '
INT 21H
DEC N
JZ EXIT
LP:
MOV AX, F1
ADD AX, F2
JC EXIT
MOV BX, F2
MOV F1, BX
MOV F2, AX
CALL OUTPUT
MOV DL, ' '
MOV AH, 2
INT 21H
DEC N
JNZ LP
EXIT:
MOV AH, 4CH
INT 21H ;返回DOS
;---------------------------
INPUT:
MOV BL, 10
MOV CX, 0
IN_X:
MOV AH, 7
INT 21H
CMP AL, 13
JE IN_END
CMP AL, '0'
JB IN_X
CMP AL, '9'
JA IN_X
MOV DL, AL
MOV AH, 2
INT 21H
MOV AL, DL
SUB AL, 30H
MOV AH, 0
XCHG AX, CX
MUL BL
ADD CX, AX
CMP CH, 0
JNZ IN_END
JMP IN_X
IN_END:
RET
;---------------------------
OUTPUT:
MOV BX, 10
MOV CX, 0
OL1:MOV DX, 0
DIV BX
ADD DL, '0'
PUSH DX
INC CX
CMP AX, 0
JNZ OL1
MOV AH, 2
OL2:POP DX
INT 21H
LOOP OL2
RET
;---------------------------
CC ENDS
END START
;---------------------------
运行时,显示如下:
C:\MASM>WW
Please input n = 7
Fibonacci Sequence is: 1 1 2 3 5 8 13