Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I’ve just started learning flat assembler for windows (well trying). Every time I try to assign a variable a value I get a memory access violation error c0000005. It's driving me crazy.
ASM
mov [var],4

The example programs will run fine but I can't set a variable without it crashing. Any ideas?

The code
ASM
; example of simplified Windows programming using complex macro features
include 'win32ax.inc' ; you can simply switch between win32ax, win32wx, win64ax and win64wx here

.code
var   Dd  3
  start:
mov eax,5

mov [var],4 ;<----- causes the crash (will work without)

invoke  MessageBox,HWND_DESKTOP,var,invoke GetCommandLine,MB_OK
invoke  ExitProcess,0
.end start
Posted
Updated 13-May-11 23:45pm
v4
Comments
Fabio V Silva 14-May-11 5:46am    
Edited formatting.
Sam Homer 14-May-11 17:12pm    
Thanks guys I tried putting it in to the .data section of the program and it all works fine.

An access violation means that you are trying to write to a memory address that does not belong to your program's address space. That's the what the protected mode of the CPU is supposed to protect: No program running amok can violate another program's memory space.

Memory addressing and paging on a PC is a matter you must understand once you really want to do some more complicated things. For now it will be sufficient to set up the 'variables' the right way.

In assembly you simply have to reserve the right amount of bytes for the value you want to store. The label (you would call it the variable name) represents the memory address of the first reserved byte. Since you get an access violation, this label represents an invalid address which does not belong to your program.

Obviously you did something wrong when you reserved the memory for your value. Please post that part of your code (including any assembly directives) and we should resolve this quickly.


Thanks for posting the code.

Sam Homer wrote:
var Dd 3


Try it like this:

Var Dd ?


Edit: I have gotten a short sample from the assembler's homepage. Look at the _hheap label and how it is declared and used.

; GetMainArgs v1.01
; Copyright © 2003 Theodor-Iulian Ciobanu

format PE GUI 4.0
entry start

include 'win32a.inc'
include 'cmd.inc'

  start:
        invoke  GetProcessHeap
        mov     [_hheap],eax
        invoke  HeapAlloc,[_hheap],HEAP_ZERO_MEMORY,1000h
        mov     [_strbuf],eax

        call    GetMainArgs
        mov     esi,[_argv]
        cinvoke wsprintf,[_strbuf],_fmt1,[_argc]
        mov     ebx,[_argc]
    @@:
        cinvoke wsprintf,[_strbuf],_fmt2,[_strbuf],[esi]
        add     esi,4
        dec     ebx
        cmp     ebx,0
        jnz     @b
        invoke  MessageBox,0,[_strbuf],_msgcap,MB_ICONINFORMATION+MB_OK

        invoke  HeapFree,[_hheap],0,[_argv]
        invoke  HeapFree,[_hheap],0,[_strbuf]
        invoke  ExitProcess,0

_strbuf  dd ?
_fmt1    db '%u',0
_fmt2    db '%s, "%s"',0
_msgcap  db 'Command line parameters',0
_hheap   dd ?

data import
 library kernel,'KERNEL32.DLL',\
         user,'USER32.DLL'

 import kernel,\
         GetCommandLine,'GetCommandLineA',\
         GetProcessHeap,'GetProcessHeap',\
         HeapAlloc,'HeapAlloc',\
         HeapFree,'HeapFree',\
         ExitProcess,'ExitProcess'

 import user,\
        MessageBox,'MessageBoxA',\
        wsprintf,'wsprintfA'
end data
 
Share this answer
 
v3
Comments
Richard MacCutchan 14-May-11 13:02pm    
Sorry about the confusion above; your answer makes a lot of sense. If I ever get the urge to try assembler again I know who to ask for help.
[no name] 14-May-11 13:19pm    
Your objection was valid. Data is supposed to go into the data segment. But even in the old days you could put everything into one segment if you wanted to. You just had to be careful about which segment register is used when addressing memory and somtimes you had to override the default.

Anyway, this Flat Assembler looks interesting. The name suggests that it tries to do away with the awkwardness we owe to the old real mode in favor of the protected mode's flat memory model. This seems to include more intuitive use of the segment registers. I think I will have a better look at it.

P.S: What do you expect from somebody who keeps ranting about his old computer with a hex keyboard and who just at this moment is also posting in a user group for such old boxes?
Richard MacCutchan 14-May-11 15:41pm    
I started my programming life keying in machine code at the front panel of the mainframe, again using Hex codes.
Are you sure your variable should be in the .code segment? It's a long time since I did assembler but I always kept my variables in the .data segment.
 
Share this answer
 
v2
Comments
[no name] 14-May-11 9:29am    
You are absolutely right, but I have taken a look at the handbook and some samples. Doing everything in the code segment used to be and also is in this case ok.

The handbook was not too specific on how to declare and use memory pointers. The only thing I could make out is, that he initialized it with the value 3 while the samples did not (the '?' operator). Therefore it's my theory that he accidentally initialized his pointer to the address (without the segment) 0003, which is not valid.
Richard MacCutchan 14-May-11 12:18pm    
How do you know that putting data in the .code segment is OK, since it crashes your program? What happens when you put it in the .data segment?

Also declaring a variable with '?' is, if I remember correctly, merely reserving space without initialising it, so it should be valid.
[no name] 14-May-11 12:31pm    
Sorry, but I did not post the question. I was looking for the answer as well.

Anyway, I took a look at the documentation, which was not too informative and also at the code samples. In some of the samples this was done exactly the same way: The data was in the code segment and not initialized. Not initializing them was the only difference I could make out, so I think it's worth a try.

Edit: Please take a look at the code sample I got from the assembler's homepage. I posted it my answer.

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