Прикреплённый файл «Fibonacci.asm»

Загрузка

   1 # Compute several Fibonacci numbers and put in array, then print
   2 .data
   3 fibs:.word   0 : 19         # "array" of words to contain fib values
   4 size: .word  19             # size of "array" (agrees with array declaration)
   5 prompt: .asciiz "How many Fibonacci numbers to generate? (2 <= x <= 19)"
   6 .text
   7       la   $s0, fibs        # load address of array
   8       la   $s5, size        # load address of size variable
   9       lw   $s5, 0($s5)      # load array size
  10 
  11 # Optional: user inputs the number of Fibonacci numbers to generate
  12 #pr:   la   $a0, prompt      # load address of prompt for syscall
  13 #      li   $v0, 4           # specify Print String service
  14 #      syscall               # print the prompt string
  15 #      li   $v0, ?????Replace_this_dummy_with_the_correct_numeric_value???????           # specify Read Integer service
  16 #      syscall               # Read the number. After this instruction, the number read is in $v0.
  17 #      bgt  $v0, $s5, pr     # Check boundary on user input -- if invalid, restart
  18 #      blt  $v0, $zero, pr   # Check boundary on user input -- if invalid, restart
  19 #      add  $s5, $v0, $zero  # transfer the number to the desired register
  20       
  21       li   $s2, 1           # 1 is the known value of first and second Fib. number
  22       sw   $s2, 0($s0)      # F[0] = 1
  23       sw   $s2, 4($s0)      # F[1] = F[0] = 1
  24       addi $s1, $s5, -2     # Counter for loop, will execute (size-2) times
  25       
  26       # Loop to compute each Fibonacci number using the previous two Fib. numbers.
  27 loop: lw   $s3, 0($s0)      # Get value from array F[n-2]
  28       lw   $s4, 4($s0)      # Get value from array F[n-1]
  29       add  $s2, $s3, $s4    # F[n] = F[n-1] + F[n-2]
  30       sw   $s2, 8($s0)      # Store newly computed F[n] in array
  31       addi $s0, $s0, 4      # increment address to now-known Fib. number storage
  32       addi $s1, $s1, -1     # decrement loop counter
  33       bgtz $s1, loop        # repeat while not finished
  34       
  35       # Fibonacci numbers are computed and stored in array. Print them.
  36       la   $a0, fibs        # first argument for print (array)
  37       add  $a1, $zero, $s5  # second argument for print (size)
  38       jal  print            # call print routine. 
  39 
  40       # The program is finished. Exit.
  41       li   $v0, 10          # system call for exit
  42       syscall               # Exit!
  43 		
  44 ###############################################################
  45 # Subroutine to print the numbers on one line.
  46       .data
  47 space:.asciiz  " "          # space to insert between numbers
  48 head: .asciiz  "The Fibonacci numbers are:\n"
  49       .text
  50 print:add  $t0, $zero, $a0  # starting address of array of data to be printed
  51       add  $t1, $zero, $a1  # initialize loop counter to array size
  52       la   $a0, head        # load address of the print heading string
  53       li   $v0, 4           # specify Print String service
  54       syscall               # print the heading string
  55       
  56 out:  lw   $a0, 0($t0)      # load the integer to be printed (the current Fib. number)
  57       li   $v0, 1           # specify Print Integer service
  58       syscall               # print fibonacci number
  59       
  60       la   $a0, space       # load address of spacer for syscall
  61       li   $v0, 4           # specify Print String service
  62       syscall               # print the spacer string
  63       
  64       addi $t0, $t0, 4      # increment address of data to be printed
  65       addi $t1, $t1, -1     # decrement loop counter
  66       bgtz $t1, out         # repeat while not finished
  67       
  68       jr   $ra              # return from subroutine
  69 # End of subroutine to print the numbers on one line
  70 ###############################################################

Прикреплённые файлы

Для ссылки на прикреплённый файл в тексте страницы напишите attachment:имяфайла, как показано ниже в списке файлов. Не используйте URL из ссылки «[получить]», так как он чисто внутренний и может измениться.

Вам нельзя прикреплять файлы к этой странице.