在asm8086中生成Fibonacci系列时的无限循环

时间:2016-12-10 13:45:14

标签: assembly fibonacci emu8086

我的代码是根据用户给出的数字生成Fibonacci系列的元素..事情就是每当我输入任何数字时,它会进入一个无限循环而不是输出某个no。我输入的元素..这里是我用来打印斐波纳契数列的程序:

displayFib proc
MOV DX, 30h         ; move value 30 hexadecimal to DX, which represents 0
call display
MOV AX, input   
CMP AX, 0        ;if the input is 0 in hexadecimal ASCII value then jump to finish
JE finish_it

mov   ah,9              ; formating - coma
mov   dx,offset msg3
int   21h       

;display the 1st term
MOV DX, 31h         ; move value 31 hexadecimal to DX, which represents 1
call display
CMP input, 1        ;if the input is 1 in hexadecimal ASCII value then jump to finish
JE finish_it

MOV CX, input       ;intializing counter, knowing that first 2 terms were displayed already
SUB CX, 2

repeat:
    mov   ah,9              ; formating - coma
    mov   dx,offset msg3
    int   21h       

    MOV AX, fibn_2        ; calculating the n'th term of a sequence    n = (n-1) + (n-2) 
    ADD AX, fibn_1
    MOV fib, AX
    MOV DX, fib
    MOV saveCount, CX       ;saving the state of the counter as it will be modified in the displayNum
    call displayNum
    ;display the n'th term (current term)
    MOV CX, saveCount       ;restoring state of the counter
    MOV AX, fibn_1        ; n-1 in the next round of a loop will be n-2
    MOV fibn_2, AX
    MOV AX, fib         ;n'th term in the next round will be n-1
    MOV fibn_1, AX
    DEC  CX             ;decrementing counter
    JNZ repeat          ; loop until counter = 0

finish_it:

ret
displayFib endp
  • 我正在使用emu8086
  • here是我完整编写的代码(如果需要)

谢谢,

1 个答案:

答案 0 :(得分:1)

<activity
            android:name=".ui.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="fullSensor"
            android:theme="@style/AppTheme.NoActionBar" />

输入为2时会发生什么?无限循环!!!

您的程序失败是因为您没有以正确的方式处理输入!

keyin 例程会破坏MOV CX, input ;intializing counter, knowing that first 2 terms were displayed already SUB CX, 2 寄存器,但您将AH寄存器移动到 num1 变量。通过明确归零AX寄存器来纠正。

AH

num2 变量也是如此。

call keyin     ;gets user input
SUB AL, 48     ;changes ASCII value into numeric value for further processing

mov ah, 0      <<<<<<< ADD THIS

mov num1 , AX  ;saves user input to variable num1

无论是推/弹都发生了什么?

MOV saveCount, CX       ;saving the state of the counter as it will be modified in the displayNum
call displayNum         ;display the n'th term (current term)
MOV CX, saveCount       ;restoring state of the counter