0

フィボナッチ数列を特定の数まで取得しようとしました。しかし、正しく印刷されません。これが私のコードです。num は与えられた数です。

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

誰か助けてください。よろしくお願いします..!

4

4 に答える 4

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

これらの最初の 2 行は移動する必要があります。if <= num をループする意図がある場合は、次を使用しますjle。文字を印刷しているだけなので、これは次の後に正しく機能しません。0112358

于 2013-06-10T17:33:44.450 に答える
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

私は答えを見つけました。ありがとうございます。

于 2013-06-11T01:18:17.663 に答える
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                            
于 2013-06-10T17:27:17.487 に答える