【问题标题】:How to print fibonacci series in assembly?如何在汇编中打印斐波那契数列?
【发布时间】:2013-06-10 16:14:40
【问题描述】:

我试图让斐波那契数列达到给定的数字。但不会正确打印。 这是我的代码。 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

请有人帮助我。在此先感谢..!

【问题讨论】:

  • int 21h,这还能用吗?你用的是 1983 年的书还是什么?
  • 你期望什么输出,实际输出是什么?您是否尝试过在调试器中单步执行代码?
  • 当我输入 3 作为数字时,它给了我 001,这是斐波那契的前三个。但是当我输入 4 作为数字时,它给了我 01135,但答案应该是 0112。是的,我尝试过,但我不知道那里发生了什么。

标签: assembly x86 tasm


【解决方案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 jle。 由于您只打印一个字符,因此这将无法正常工作:0112358

【讨论】:

    【解决方案2】:

    在 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                            
    

    【讨论】:

    • 这不是真正的答案。
    【解决方案3】:
    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
    

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

    【讨论】:

      【解决方案4】:

      或者试试下面的代码,结果如下:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-27
        • 1970-01-01
        • 1970-01-01
        • 2014-04-13
        • 2015-01-04
        • 2016-06-05
        • 2013-04-11
        相关资源
        最近更新 更多