开发者

Why is this array declaration invading the next?

开发者 https://www.devze.com 2022-12-26 13:40 出处:网络
I am learning Assembly for IA-32 with MASM, using Microsoft Visual C++ Express Edition, and this difficulty came up. When I do this:

I am learning Assembly for IA-32 with MASM, using Microsoft Visual C++ Express Edition, and this difficulty came up. When I do this:

INCLUDE Irvine32.inc

QUANT = 47

.data

    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileName BYTE "vetor.txt", 0
    fileHandler DWORD 0

.code
main PROC

    mov esi, 0
    mov ecx, QUANT

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax
    inc esi
    loop L1

    mov edx, OFFSET fileName
    c开发者_StackOverflow社区all CreateOutputFile
    mov fileHandler, eax
    mov edx, OFFSET fibonacciVetor
    mov ecx, QUANT * TYPE fibonacciVetor
    call WriteToFile
    mov eax, fileHandler
    call CloseFile

    exit

main ENDP
END main

This program does not run correctly, because the fileName string is erased in the middle of the process. The Irvine32.inc library can be found in Kip Irvine's website. I'm using it because the textbook my professor is using is "Assembly Language for Intel-based Computers", 5th Edition by Kip Irvine. When I change the variables declaration for this:

    fileName BYTE "vetor.txt", 0
    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileHandler DWORD 0

The program runs correctly.

Why is it that simply changing the order of the declaration has influence in how the program runs or does not, since the fileName variable should be allocated right after the end of the fibonacciVetor and should not be affected when I write to the array?

Thank you very much.


...since the fileName variable should be allocated right after the end of the fibonacciVetor and should not be affected when I write to the array

Well, "should not" != "is". Step through it with a debugger to see where your range error is.


I would suspect this section here:

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax

I think that last line might be what clobbers your fileName.

Follow Per Larsen's advice and either step through it with a debugger or add some print statements. Hopefully this gives you an idea of where to start looking.


Just a guess, but I would say that the (Quant - 2) term in the definition is the problem. I could see saying (Quant - 1) if you are going to use a zero relative array, but with -2, you are losing the space to put your last element.

Just get rid of the -2. Memory is cheap and plentiful. You aren't programming a KIM-1, after all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号