【问题标题】:Assembly print to file from array汇编打印到数组中的文件
【发布时间】:2014-04-11 17:50:52
【问题描述】:

我需要这个程序来写出一个包含斐波那契数列前 47 个值的文件。我的程序使用提供的库中包含的程序正确显示了 47 个条目,但它不会将它们打印到文件中。

我很确定它将数组存储在 esi 中,但我的 fib.bin 文件只有一个条目,而不是数组的开头。我相当确定我需要做的就是使用 esi,但我无法弄清楚,提前谢谢。

TITLE Fibonacci Numbers                     (FibNums.asm)

INCLUDE Irvine32.inc

.data
fileHandle DWORD ?
filename BYTE "fib.bin",0

FIB_COUNT = 47
array DWORD FIB_COUNT DUP(?)

.code
main PROC

    ;Creates the file
    mov  edx,OFFSET filename
    call CreateOutputFile
    mov  fileHandle,eax

    ;Generates the array of values
    mov esi,OFFSET array
    mov ecx,FIB_COUNT
    call generate_fibonacci

    ;Write out to file
    mov eax,fileHandle
    mov edx,OFFSET array
    mov ecx,SIZEOF array
    call WriteToFile

    ;Close the file
    mov eax,fileHandle
    call CloseFile


    exit
main ENDP

;--------------------------------------------------
generate_fibonacci PROC USES eax ebx ecx
;
;Generates fibonacci values and stores in an array
;Receives: ESI points to the array, ECX = count
;Returns: Nothing
;---------------------------------------------------

    mov eax,1
    mov ebx,0

L1: add eax,ebx
    call    WriteDec
    call Crlf

    mov [esi],eax
    xchg    eax,ebx
    loop L1
    ret
generate_fibonacci ENDP

END main

【问题讨论】:

  • “我的 fib.bin 文件只有一个条目,它不是数组的开头” 只是为了确保:您正在十六进制编辑器中查看文件,对吗?

标签: assembly masm fibonacci irvine32


【解决方案1】:

你必须增加 esi:

...
L1: add eax,ebx
    call WriteDec
    call Crlf

    mov [esi], eax
    xchg eax, ebx
    add esi, 4          ; move forward 4 Bytes (4*8 bits) = 1 dword (1*32 bits)
    loop L1
...

【讨论】:

  • 哎呀,我在查看我以前的一个数组程序后想通了,不过还是谢谢你...忘记这一点感觉有点傻。
【解决方案2】:
L1: add eax,ebx
    call    WriteDec
    call Crlf

    mov [esi],eax
    xchg    eax,ebx
    ***add  esi,TYPE array***
    loop L1

有趣的解决方案,只需将 TYPE 数组添加到 esi 中,以移动它在寄存器中输出答案的位置。现在工作 100%。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多