如何在装配中打印斐波纳契系列?

时间:2013-06-10 16:14:40

标签: assembly x86 tasm

我试图让斐波纳契系列达到给定数量。但是打印不正确。 这是我的代码。 number是给定的数字。

proc getFibo
        mov al,num

        mov cl,0
        mov bl,1

        mov dl,cl
        add dl,48
        mov ah,02h
        int 21h


        getNext:
            mov dl,bl
            add dl,48
            mov ah,02h
            int 21h

            add cl,bl

            mov dl,cl
            add dl,48
            mov ah,02h
            int 21h

            add bl,cl

            mov cl,bl
            add bl,1
            cmp bl,num
            jl getNext


        ret
    endp

有人请帮帮我。谢谢你提前......!

3 个答案:

答案 0 :(得分:1)

最后的循环条件不正确:

mov cl,bl   # this is skipping a value in the F-series. F(i-2) == F(i-1)
add bl,1    # this is just wrong for the F-series. F(i) = F(i-1) + 1 + F(i-2)

cmp bl,num  # ok - `bl` is the next value printed if < num.
jl getNext

前两行应该去。如果您的意图是在&lt; = num时循环,请使用:jle。 由于您只打印一个字符,因此在0112358

之后无法正常工作

答案 1 :(得分:0)

16位系统上的寄存器空间不会花费很长时间......

我已经更新了我的答案,但它仍然不完美

 mov bp,sp                                 
 mov ax,1                        
 mov bx,2                        

 .again     ;fibonacci bit                      
 add ax,bx                       
 push ax                         
 add bx,ax                       
 jc putnumsonstack  ;finishes when the fibonacci number bigger than the register
 push bx                         
 jmp again                       

 .putnumsonstack  ;Turns hex into decimal                
 mov bx,A         ;and puts individual decimal numbers onto stack               
 .again2                          
 cmp ax,0         ;tells you the division by A is finished               
 jz print         ;and the decimal number is ready to print 
 div bx           ;divide a hex num by hex-A and the dx carry gets the decimal 
 add dl,30        ;The dx carry is what we print out, so we add hex-30              
 push dx          ;and put it on the stack for printing              
 mov dx,0         ;then clear dx for the next DIV bx               
 jmp again2                      


 .print           ;prints out the decimal numbers               
 pop ax           ;strips the stack back towards the next hex number
 cmp ah,0                 ;tells you the decimals are all printed       
 jnz putnumsonstack       ;jumps to the next decimal numbers stackload       
 ;PRINT OUT AL HERE              
 cmp sp,bp                                  
 jae finished                             
 jmp print                       

 .finished                            

答案 2 :(得分:0)

proc getFibo
        mov al,f1
        mov bl,f2

    ;mov cl,count
    ;cmp cl,num
    ;je exitFibo

    mov dl,al
    add dl,48
    mov ah,02h
    int 21h

    mov cl,count
    add cl,1
    mov count,cl

    mov dl,bl
    add dl,48
    mov ah,02h
    int 21h

    mov cl,count
    add cl,1
    mov count,cl

    calcFibo:
        mov al,f1
        add al,f2
        mov f1,al

        mov dl,f1
        add dl,48
        mov ah,02h
        int 21h

        mov cl,count
        add cl,1
        mov count,cl

        mov cl,count
        cmp cl,num
        je exitFibo

        mov bl,f2
        add bl,f1
        mov f2,bl

        mov dl,f2
        add dl,48
        mov ah,02h
        int 21h

        mov cl,count
        add cl,1
        mov count,cl

        mov cl,count
        cmp cl,num
        je exitFibo

        jmp calcFibo

    exitFibo:
    ret
endp

我找到了答案。谢谢大家。