如何打印斐波那契数列中的前20个元素(汇编8086)



我正试图编写一段代码,运行一个循环,打印斐波那契数列中的前20个数字。但我不知道该怎么解决,我试了很多方法。

org 100h
mov ax, 0
mov bx, 1
mov cx, 20
start:        
call print_num ; this is function that print the value that inside ax register
PRINTN ; this is print new line
mov dx, ax
add ax, bx   
loop start

mov ah, 0
int 16h
ret 

include magshimim.inc ; this is a private library

有人知道我该怎么做吗?

取决于我们希望序列如何开始

甚至斐波那契自己也从1,2,3,5,8开始了他的序列
请参阅维基百科文章https://en.wikipedia.org/wiki/Fibonacci_number


应用@cem:建议的更正后的代码

org 100h
mov ax, 0
mov bx, 1
mov cx, 20
start:        
call print_num
PRINTN
mov dx, ax
add ax, bx   
MOV BX, DX
loop start
mov ah, 0
int 16h
ret 
include magshimim.inc

它将输出:

0,1,1,2,3,5,8,13,...

有时我们不想从零开始。下一个代码是这样工作的:

org 100h
mov  ax, 1
xor  bx, bx
mov  cx, 20
start:        
call print_num
PRINTN
xchg ax, bx
add  ax, bx   
loop start
mov  ah, 00h  ; BIOS.WaitKey
int  16h      ; -> AX
ret
include magshimim.inc

它将输出:

1,1,2,3,5,8,13,...

8086组合中的简单fibonacci打印机

最新更新