The MASM Forum

General => The Campus => Topic started by: Hyzau on November 26, 2012, 05:56:55 AM

Title: Initialize Data inside .code section
Post by: Hyzau on November 26, 2012, 05:56:55 AM
Hi,

Is it possible to have a section (.code) where you can have initialized data in MASM?
For example, can I have :


Title db "Some title or other things",0

;--- and then access the data like that :

mov eax, [Title] ;



The "why" of this question is long to explain sorry (and also hard to explain  in english) ^^
Thanks in advance for your help

Best regards,
Title: Re: Initialize Data inside .code section
Post by: qWord on November 26, 2012, 06:00:43 AM
you can place the data between your procedures/functions or:
jmp @F
myData db "123",0
@@:

Title: Re: Initialize Data inside .code section
Post by: dedndave on November 26, 2012, 06:03:55 AM
you can do that...
mov eax, dword ptr [Title]

you see, EAX is a dword sized register and Title is defined as bytes
so, you must use the size-override operator

now - as to code sections
by default, the code section attributes are "read-execute"
meaning, you can read data or execute code from that section
if you try to write to it, an exception will be generated (crash)

you can alter the attribute for the section, or for part of it
however, if you want to write to the data, it is best to put it in .DATA
Title: Re: Initialize Data inside .code section
Post by: fearless on November 26, 2012, 06:04:37 AM
If you look at the macros used in masm32, you can get an idea of how to use both .data and .code, so for example i think you could set up your example code like so:

.data
Title   db      "Some title or other things",0
.code
mov eax, [Title] ; or lea eax, Title
Title: Re: Initialize Data inside .code section
Post by: Hyzau on November 26, 2012, 06:12:29 AM
Quote from: qWord on November 26, 2012, 06:00:43 AM
you can place the data between your procedures/functions or:
jmp @F
myData db "123",0
@@:



Okay, just by the way what means @F and @@.
I've already seen that someone called "jmp @B" and it jumped to the "@@" procedure and I don't know how and why ?

To came back to the subject, when I'm using this syntax, I have an error

Access violation when writing to [004012F7] ; it's the "adress" of my variable Title


Is it possible to modify some of the initialized data (which is inside the .code section)
Also, I'm calling my title right after my start procedure but it is initialized just before my last procedure.
Don't know if it can create a problem

Okay it's because of that :

Quote from: dedndave on November 26, 2012, 06:03:55 AM
now - as to code sections
by default, the code section attributes are "read-execute"
meaning, you can read data or execute code from that section
if you try to write to it, an exception will be generated (crash)

you can alter the attribute for the section, or for part of it
however, if you want to write to the data, it is best to put it in .DATA
Quote from: dedndave on November 26, 2012, 06:03:55 AM
you can alter the attribute for the section, or for part of it
How can I do it inside my own code ? :D
Title: Re: Initialize Data inside .code section
Post by: Hyzau on November 26, 2012, 06:49:06 AM
Okay, once again thanks guys, you're the best  :t
I find another way to make everything work (I don't write anymore on my title, nothing magic ^^)
Title: Re: Initialize Data inside .code section
Post by: jj2007 on November 26, 2012, 08:46:25 AM
Quote from: Hyzau on November 26, 2012, 06:12:29 AM
Okay, just by the way what means @F and @@.

They are jumps to anonymous labels, meaning Forward and Backwards
@@:
  inc eax
  cmp eax, 123
  je @F
  jne @B
@@:

Saves you some typing and the difficult task to invent a thousand new, innovative and meaningful label names :bgrin: