2017-03-24 133 views
0

Nを入力してNフィボナッチ数を返す単純なアセンブリコードを作成しようとしています(たとえば、2を入力すると1が出力され、3を入力すると、出力2)。私のコードではエラーは発生しませんが、数字を入力すると何か変なものが返されます。単純なMIPSアセンブリ - フィボナッチ数を返す

1を入力すると、2685009921が返されます。 2を入力すると、0.01が返されます。 3を入力すると、0.02が返されます。 4を入力すると、最初に正の整数を要求するテキストが出力され、3(正解)と入力します。 5を入力すると何も出力せず、再度enterを押すと実行時例外(無効な整数入力システム5)が発生します。 5を超えるものは奇妙なエラーを返します。

これは、入力番号がコードとしてシステムコールを実行しているかのように、最初の4つの数値が何を出力するのか(最初の4つのシステムコール出力データ)を説明します。

あなたはどう思いますか?ここでは、コードです:

.data 
    introText: .asciiz "Type a positive integer, please! \n" 
    input: .word 123 


.text 
    # ask user for input 
    li $v0, 4 
    la $a0, introText 
    syscall 

    # read input int 
    li $v0, 5 
    syscall 

    # store input 
    addi $s1, $v0, 0 
    syscall 

    # main loop 
    li $s2, 0 # s2 starts at 0 and will increase until it's equal to $s1, the player input 
    li $s3, 0 # this will hold the most recent fib number 
    li $s4, 1 # this will hold the second most recent fib number 
    loop: 
    addi $s2, $s2, 1 # increment s2 for loop 
    add $s5, $s3, $s4 # make the current result the sum of the last two fib numbers 
    addi, $s4, $s3, 0 # make the second most recent fib number equal to the most recent fib number 
    addi, $s3, $s5, 0 # make the most recent fib number equal to the current fib number 
    bne $s2, $s1, loop 

    # return the answer 
    li $v0, 1 
    addi $a0, $s5, 0 
    syscall 

    # end program 
    li $v0, 10 
    syscall 

答えて

0

あなたはaddi $s1, $v0, 0syscallを配置したいくつかの理由。その指示はそこにあるべきではありません。

+0

ありがとうございました! – BowmanBeric