tags:

views:

31

answers:

3

Hello, people.

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
    call 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.

A: 

...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.

500 - Internal Server Error
A: 

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.

Tim
A: 

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.

Ron
I'm not asking because I want the program to run, simply. I'm asking for I'm looking to learn something. Of course, making an array with 1000 positions and using 30 makes it, but teaches me nothing about why the problem is happening in the first place.
jpmelos
Which is why I answered you. You're putting 47 pounds in a 45 pound bag. No need to get snippy and downvote me.
Ron