Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Violation 1

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
US
Hi group,
Why do access violation errors occur? I am getting the error: Unhandled exception 0xC0000005 : Access Violation.
This error is occuring for this piece of code:
;;;;;;;;Begin ASM
Code:
INCLUDE Irvine32.inc

FillArray proto,
aSize:dword, pArry:ptr dword

.data
ArraySize = 10
Array dword ArraySize dup(?)

.code
main proc
Call Randomize
Invoke FillArray, ArraySize, addr Array
main endp

FillArray proc,
aSize:dword, pArry:ptr dword

push ebp
mov ebp, esp
pushad
mov esi, [ebp+12]
mov ecx, [ebp+8]
call dumpregs
cmp ecx, 0
jle Finish

Fill:
mov eax, 10000
call RandomRange
mov [esi], eax ; <- This is the line the debugger stops at.
add esi, type dword
Loop Fill

Finish:
popad
ret
FillArray endp
end main
;;;;;;;;;End ASM
Another thing I noted was the msdev debugger shows the value of the ECX register to be 0040102D after the instruction 'mov ecx, dword ptr [aSize]' has executed. Shouldnt the value be 0000000A? And the value in ESI is 0000000A, but shouldnt the value be the offset of array?
In disassembly the instruction shows as: 'mov esi, dword ptr [pArry]' in replacement for 'mov esi, [ebp+12]' which is what I expected, but i'm confused to why the value of ESI is not the offset of array! I'm sure this has something to do with why im getting that access violation error.
Can anyone please help?
K.
 
This is what I suspect.

FillArray proc,
aSize:dword, pArry:ptr dword

push ebp
mov ebp, esp


Remove that lines. When the compiler find a PROC, it will takes care for EBP & ESP


-- AirCon --
 
Thanks AirCon u were absolutely right! Just a quick question for you.
In which case would I need to use the 'push ebp' and 'mov ebp,esp' instructions?
See I was told, if im going to save and restore registers, I should push ebp onto the parameter stack just before using a 'pushad' instruction. Also why do some procedures use the ENTER and LEAVE instructions when 'push'ing ebp is not required?
Thanks
Kunal.
 
Well,

I'm not exactly sure how to explain about this. Someone will correct me if I'm wrong :)
I guess it's dealing with the version of the compiler (MASM compiler)

Older compiler didn't recognize procedure. So when we want to create a procedure we must take care the stack frame (EBP & ESP) manually.
A later processor add a new instruction ENTER & LEAVE. This is actually the same with:
push bp ; (push ebp for 32bit)
mov bp, sp ; (mov ebp, esp)

and LEAVE is to restore the stack frame back to original value:
mov sp, bp
pop bp

Then the later compiler gives a capability to create procedure using PROC & ENDP, also to make asm program more organize. When it compile, it also produce push ebp follow by mov ebp,esp. While ENDP will produce a LEAVE instruction.

So it's all the same actually. And this is the reason when we create procedure (PROC) we don't need to deal with stack frame anymore, cos the compiler already takes care of it.

There are some condition that we want to deal with stack frame manually

Hope it helps a bit

-- AirCon --
 
&quot;enter&quot; and &quot;leave&quot; may do the same job (exactly) but unfortunately on some processors they are actually slower than the mov equivalents. Therefore I prefer not to use them (because if I'm not doing something time critical, I'm not using assembly..)

For the same reason, I actually feel that assembler syntax that is designed to optimise and mimic high level languages is misguided. If you want a high level language style handling of parameter passing, then use a high level language! If you want complete control over what code is produced, and full transparency, then use assembler - but if assembler starts doing non-transparent things, that defeats the object.

But that's just my personal view.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top