2016-12-10 8 views
-2

私のコードは、フィボナッチシリーズの要素をユーザーが指定した数値に基づいて生成するものです。何かを入力するたびに無限ループに入りますある種の。要素I入力...と、ここで私はフィボナッチ数列をプリントアウトするために使用する手順です:フィボナッチシリーズを生成中の無限ループasm8086

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

コードをステップ実行するためにエミュレータを使用していませんか? – Jester

+0

はい私は持っているが、私はエラーがまだ初心者で、それはほとんど私の最初のコードですどこに見つけることができません.. –

答えて

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

入力が2の場合はどうなりますか?無限ループ!!!


正しい方法で入力を処理しないため、プログラムが失敗します。

KEYINルーチンはAHレジスタを破壊し、まだあなたがNUM1変数にAXレジスタを移動します。明示的にAHレジスタをゼロにして修正してください。

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 

同じことがNUM2変数のために行きます。ポップ/プッシュに起こったものは何でも


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 

PUSH CX    ;saving the state of the counter as it will be modified in the displayNum 
call displayNum  ;display the n'th term (current term) 
POP CX    ;restoring state of the counter 
+0

あなたの詳細なソリューションのために多くのありがとう:) ..残念ながら私はまだ同じ問題が.. –

+0

ありがとうございます..それは今:) :))) –