【问题标题】:I'm trying to write the first 47 Fibonacci numbers to a disk file. What should I fix?我正在尝试将前 47 个斐波那契数写入磁盘文件。我应该修复什么?
【发布时间】:2015-11-06 03:04:08
【问题描述】:
; 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

【问题讨论】:

  • 对于初学者,您应该在问题中尽可能详细地解释什么是不工作的。

标签: assembly fibonacci irvine32


【解决方案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],edxadd esi,4

  • 您可以将cmp ecx,0 jbe L2 移动到循环之前。您无需重新测试此条件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2016-04-21
    • 2012-12-29
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多