Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, I am working on an ASM project that involves converting a C++ code into an Assembly Language. I'm using the 32-bit project from Irvine. When I pasted my code into the project and built it, it was building successfully. However, when I tried to debug it, it gave me an error of the source not being available or that I was violating access. This is the message it gave me: Unhandled exception at 0x00401D2C in Project.exe: 0xC0000005: Access violation reading location 0x00000004.

What I have tried:

My code:

ASM
INCLUDE Irvine32.inc

.386
.model flat, stdcall
.stack 4096
Exitprocess proto, dwExitCode: dword

.data
	array dword 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4
	lower dword 4
	upper dword 8
	ArraySize = ($ - Array) / TYPE lower
	index dword 0
	sum dword 0
	disIndex BYTE "Qualified members: ",0
	disSum BYTE "Sum = ",0

.code
main proc
	mov eax, 0
	mov edx, lower
	mov esi, 0

L1:
	cmp ecx, edx
	jl L4
	cmp array[esi * 4], edx
	jg L3

L2:
	cmp edx, upper
	jg L4
	cmp esi, ArraySize
	jge L4
	add eax, array[esi * 4]
	inc esi
	jmp L2

L3:
	cmp esi, ArraySize
	jl L2

L4:
	mov sum, eax
	call WriteString
	mov edx, DWORD PTR disSum
	call WriteString

	invoke exitprocess, 0

main ENDP
END main
Posted
Updated 5-May-24 5:38am
v2

An access violation means a bad address - you are trying to read data that doesn't "belong" to you - it's outside your process address space.

Where in your code that happens, we can't tell - so it's going to be up to you: use the debugger to step through your code and find out where the violation occurs, then look at what value is in the register(s) involved.

When you know those, you can start backtracking and work out why they contain the wrong address.
Sorry, but we can't do that for you - debugging assembler is the same process as debugging C, or C++, or C# just it takes quite a bit longer, is all!
 
Share this answer
 
ASM
array dword 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4
lower dword 4
upper dword 8
ArraySize = ($ - Array) / TYPE lower

Are you sure you want to include both lower and upper in the calulation of ArraySize?
 
Share this answer
 
There are several problems in your code.

ASM
.data
	array dword 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4
	lower dword 4
	upper dword 8
	ArraySize = ($ - Array) / TYPE lower

Since you are using '$' - Array / 4 (dword size) this would give the wrong result because Lower and Upper variables (memory space) have been added to the calculations. The correct way would be like
.data
	lower dword 4
	upper dword 8
	array dword 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4
	ArraySize = ($ - Array) / TYPE lower

Another problem is you are calling WriteString without pointing String Array to EDX. This is causing an Access Violation Exception because EDX is pointing to some garbage memory location. Secondly, DWORD PTR is wrong because the actual datatype is BYTE.
ASM
L4:
	mov sum, eax
	call WriteString
	mov edx, DWORD PTR disSum
	call WriteString

To fix, you need to do this
ASM
mov sum, eax
mov edx, OFFSET disSum
call WriteString

Finally, you have not written code to display the result.
ASM
mov eax, sum
call WriteInt

To summarize, there are much easier ways to calculate the sum of elements present in the array. For example

ASM
.code
main proc

	mov  edi,OFFSET Array		    ; 1: EDI = address of array
	mov  ecx,LENGTHOF Array		    ; 2: initialize loop counter
	mov  eax,0						; 3: sum = 0
L1:									; 4: mark beginning of loop
	add  eax,[edi]					; 5: add an integer
	add  edi,TYPE array   		    ; 6: point to next element
	loop L1							; 7: repeat until ECX = 0
 
Share this answer
 
Since "ecx" is not defined in the code and "esi" is already used to iterate through the array, it would make sense to assume that "ecx" should have a different meaning or that the comparison with "ecx" is possibly an error.
 
Share this answer
 
Comments
Richard MacCutchan 6-May-24 4:24am    
Oops, my mistake. I think you mean that it is never initialised.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900