News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

No assemble, but runtime error (mov)

Started by n00b!, June 13, 2008, 12:11:41 PM

Previous topic - Next topic

n00b!

I thought this would work....    :'(

xor ecx, ecx
xor eax, eax

mov [ecx], eax         ;Runtime-Error
mov [ecx], ax          ;Runtime-Error
mov eax, [ecx]         ;Runtime-Error
mov ax, [ecx]          ;Runtime-Error

PauloH

Hello,

Yes, this is right. Think about this:

xor ecx,ecx ; ecx = 0
xor eax,eax ; eax = 0
mov [ecx],eax ; puts eax 'into' address pointed by ecx!
mov [eax],ecx ; puts ecx 'into' address pointed by ecx!


The address pointed by 0 could be in a protected area of memory or even not exist at execution time of your program. This is the problem. If you program in C, the following situation is similar:


int *p;  /* p is a pointer to int, but what is the address of p? To where it points ? */
p = 3;   /* The answer: indetermined!!!! */


Code like these can cause runtime errors frequently, if you are a lucky guy. If you are unlucky your program can run, but with unpredictable results and hard to track bugs!

Bye, (forgive my english).

Paulo

n00b!

Oh, but if the address is valid it should work, thanks :-)

hutch--

noob,

Paulo is correct here, absolute address 0 is not accessible by an application program so anything that tries to read or write to it will generate an unhandled exception. Its pretty easy to get around with a simple test piece. You can either allocate a variable on the stack or a variable in the .data or .data? sections and access it this way if you need to do so.


.data?
  gVar dd ?
.code
  mov eax, OFFSET gVar  ; get its address
.....

or in a procedure
LOCAL lVar :DWORD
  lea eax, lVar


You uswe techniques like this if you need to pass the adress of a variable to another procedure that changes the variable.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php