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

前两行应该去。如果您的意图是循环 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 回答
0

或者尝试下面的代码,它会产生如下所示的结果:

my $N = 11;                                 # The number of Fibonacci numbers to generate
  Mov r13, 0;                                 # First  Fibonacci number
  Mov r14, 1;                                 # Second Fibonacci
  PrintOutStringNL " i   Fibonacci";          # The title of the piece

  V(N => $N)->for(sub                         # Generate each fionacci numbr by adding tehtwo previous ones together
   {my ($index, $start, $next, $end) = @_;
    $index->outRightInDec(2);                 # Index
    Mov rax, r13; PrintOutRaxRightInDecNL 12; # Fibonacci number at this index

    Mov r15, r14;                             # Next number is the sum of the two previous ones
    Add r15, r13;

    Mov r13, r14;                             # Move up
    Mov r14, r15;
   });

  ok Assemble eq => <<END;                    # Assemble and show expected output
 i   Fibonacci
 0           0
 1           1
 2           1
 3           2
 4           3
 5           5
 6           8
 7          13
 8          21
 9          34
10          55
END

可在:https ://github.com/philiprbrenan/NasmX86#print-some-fibonacci-numbers-from-assembly-code-using-nasm---the-netwide-assember-and-perl

于 2021-12-04T19:30:49.697 回答