masm call procedure access violation

So I am working on an assignment in assembly to generate a fibonacci sequence. I've written the code successfully in the main procedure but when I try to wrap it in it's own procedure and call that procedure I run into an access violation error. Here's my code:

INCLUDE Irvine32.inc

.data
array DWORD 47 DUP(?)

.code
main proc
mov esi, OFFSET array

call generate_fibonacci

invoke ExitProcess,0
main endp

generate_fibonacci PROC

mov DWORD PTR [esi], 1h
add esi, 4
mov DWORD PTR [esi], 1h
push [esi]
push [esi - 4]
add esi, 4
mov ecx, 45
L1: 
pop eax
pop ebx
add eax, ebx
mov DWORD PTR [esi], eax
add esi, 4
push [esi - 4]
push [esi - 8]
loop L1
ret

generate_fibonacci ENDP

end main

The error looks like this: "Exception thrown at some memory location in Project...: Access violation executing location same memory location.

I noticed that the memory location listed in the error message was being loaded onto the EIP register when the call generate_fibonacci instruction is executed. I'm not sure how to fix this.

Answers


The pushes and pops in your PROC are not balanced.

Before loop L1: you make 2 pushes. Within the loop L1: you make 2 pops and 2 pushes. When loop L1: ends, that leaves 2 items still on the stack when ret attempts to pull off the return address. So the code tries to resume execution somewhere that causes an access violation.

Please add two lines of code before the ret instruction to clean up the stack

pop eax
pop eax
ret

If the same code worked when it was in main, it worked because main does not end with ret.

EDIT. You could simplify it considerably by keeping the recent terms in registers. The last three terms will be in eax, ebx, edx.

generate_fibonacci PROC

    mov     eax, 1                  ;init first two terms
    mov     DWORD PTR [esi], eax    ;store first two terms
    add     esi, 4
    mov     DWORD PTR [esi], eax
    add     esi, 4

    mov     ebx, eax
    mov     ecx, 45                 ;init loop count
L1: 
    mov     edx, ebx                ;move terms along
    mov     ebx, eax
    add     eax, edx                ;sum last two terms
    mov     DWORD PTR [esi], eax
    add     esi, 4
    loop    L1
    ret

generate_fibonacci ENDP

Need Your Help

Binding iron-ajax data to a template

ajax data-binding polymer

I've got a Polymer (1.0) app that loads a list of books from AJAX: