The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on June 13, 2008, 12:11:41 PM

Title: No assemble, but runtime error (mov)
Post by: n00b! on June 13, 2008, 12:11:41 PM
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
Title: Re: No assemble, but runtime error (mov)
Post by: PauloH on June 13, 2008, 12:40:49 PM
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
Title: Re: No assemble, but runtime error (mov)
Post by: n00b! on June 13, 2008, 01:02:21 PM
Oh, but if the address is valid it should work, thanks :-)
Title: Re: No assemble, but runtime error (mov)
Post by: hutch-- on June 13, 2008, 05:30:58 PM
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.