x86: Access writing violation while using the OFFSET operator to address of array

1

I am getting the Exception thrown at 0x0044369D in Project2.exe: 0xC0000005: Access violation writing location 0x00000099. From my research so far, I am under the impression this has to do with a null pointer or out of range memory being accessed from the line mov [eax], ecx

I used the offset operator in mov eax, OFFSET arrayfib and I thought that should remedy this. I can't seem to figure out what is causing the issue here.

.model flat, stdcall 
.stack 4096
INCLUDE Irvine32.inc
ExitProcess PROTO, dwExitCode: DWORD

.data
arrayfib DWORD 35 DUP (99h)


.code 
main PROC   
    mov eax, OFFSET arrayfib    ;address of fibonacci number array 
    mov bx, 30                  ;number of Fibonacci numbers to generate
    call fibSequence            ;call to Fibonacci sequence procedure 
    mov edx, OFFSET arrayfib    ;passes information to call DumpMem
    mov ecx, LENGTHOF arrayfib
    mov ebx, TYPE arrayfib 
    call DumpMem

    INVOKE ExitProcess, 0 
main ENDP
;----------------------------------------------------------------------------------
;fibSequence
;Calculates the fibonacci numbers to the n'th fibonacci number 
;Receives: eax = address of the Fibonacci number array
;bx = number of Fibonacci numbers to generate
;ecx = used for Fibonacci calculation (i-2)
;edx = used for Fibonacci calculation (i-1)
;returns: [eax+4i] = each value of fibonacci sequence in array, scaled for doubleword
;---------------------------------------------------------------------------------

fibSequence PROC                    
    mov ecx, 0                  ;initialize the registers with Fib(0) and Fib(1)
    mov edx, 1      
    mov [eax], edx              ;store first Fibonacci number in the array 
                                ;since a Fibonacci number has been generated, decrement the counter
                                ;test for completion, proceed
    mov eax, [eax+4]

fibLoop: 
    sub bx, 1
    jz quit                     ;if bx = 0, jump to exit
    add ecx, edx
    push ecx                    ;save fib# for next iteration before it is destroyed
    mov [eax], ecx              ;stores new fibonacci number in the next address of array 
    push edx                    ;save other fib# before it is destroyed
    mov ecx, edx
    mov edx, [eax]
    mov eax, [eax+4]            ;increments accounting for doubleword 
    pop edx
    pop ecx 
quit: 
    exit
fibSequence ENDP
END main

Also if there are any other suggestions I would be happy to hear them. I am new to all this and looking to learn as much as possible.

exception
assembly
x86
asked on Stack Overflow Nov 2, 2019 by Madeline Do • edited Jul 6, 2020 by Martijn Pieters

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0