我试图将前47个斐波纳契数写入磁盘文件。我该怎么办?

时间:2015-11-06 03:04:09

标签: assembly fibonacci irvine32

; This program will store the first 47 Fibonacci numbers in an array of doublewords and write the doubleword array to a disk file

include Irvine32.inc

FibCount = 45

.data

    fileName BYTE "Fib47", 0    ; Creates file name
    FibArray DWORD ?            ; Initializes DWORD array to store fibonacci numbers

.code

main PROC

    mov ecx, FibCount
    mov esi, OFFSET FibArray
    call CreateOutputFile
    call computeFibonacciNumbers
    call WriteToFile

    exit
main ENDP

;-----------------------------------------------
; Computes Fibonacci numbers (type DWORD) and stores them in an array
; Receives: ECX = count of Fibonacci numbers
;           ESI = offset of array of Fibonacci numbers
; Returns: nothing
;----------------------------------------------

computeFibonacciNumbers PROC

    mov eax, 1
    mov ebx, 1

L1:
    cmp ecx, 0
    jbe L2
    add eax, ebx
    mov edx, eax
    mov FibArray, edx
    mov ebx, eax
    mov edx, ebx
    loop L1

L2:
    ret

computeFibonacciNumbers ENDP

END main

1 个答案:

答案 0 :(得分:1)

mov ecx, FibCount
mov esi, OFFSET FibArray
call CreateOutputFile
call computeFibonacciNumbers

为什么在 computeFibonacciNumbers 的参数设置和 computeFibonacciNumbers 的实际调用之间调用 CreateOutputFile ?非常不合逻辑且容易出错。

FibArray DWORD ?            ; Initializes DWORD array to store fibonacci numbers

FibArray 的设置只为1 dword准备空间。如果你想要47个元素的数组空间,那么写:

FibArray DWORD 47 dup(?)
  • computeFibonacciNumbers 过程的参数为ESI,但您不在任何地方使用它。

  • 在此过程中,您将所有值移到彼此之上。最好写mov [esi],edx add esi,4

  • 您可以在循环前保存cmp ecx,0 jbe L2。你无需重新测试这种情况。

  • 你循环根本不需要使用EDX。