The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: zigzag on February 21, 2012, 09:24:01 AM

Title: dtata in .code segment
Post by: zigzag on February 21, 2012, 09:24:01 AM
hi.. here is a small program

.Code

start:

        DA DB "ABC", 0

Invoke MessageBox, NULL, Offset DA, NULL, MB_OK

Invoke ExitProcess, 0

End start


It does assembly, and it used to work but then it decided not to. But when it did work, the variable DA could only contain 3 bytes of data, any more than that and it would crash. is there a way around this ONLY using the .code segment?
Title: Re: dtata in .code segment
Post by: donkey on February 21, 2012, 10:02:11 AM
The way you have it set up the bytes "ABC" will be executed. In this case it works out to:

DB 41h ;("A")
DB 42h ;("B")
DB 43h ;("C")

which are the instructions:

inc ecx
inc edx
inc ebx

You have to remember that every instruction in the computer is simply a number or series of numbers, the same applies to characters or other data. If the instruction pointer is at a specific address it has no idea that you meant it to be data (since it isn't in the data section) and will attempt to execute it as instructions (likely crashing your app). In order to avoid this you either place the data in an area that will never be executed (such as before the entry point) or jump over it. Also note that barring changing the page protection using VirtualProtect, the data in the code section is read only.

Edgar
Title: Re: dtata in .code segment
Post by: zigzag on February 21, 2012, 10:19:52 AM
jmp skip
DA DB "whatever"
skip:
invoke MessageBox, null, offset DA, null, MB_OK

well that works perfectly fine.. buffers seem to be no problem because i can create one on-the-fly in the stack.

thanks, such a simple solution! :D yay!
Title: Re: dtata in .code segment
Post by: vanjast on February 21, 2012, 12:00:48 PM
There's a text macro in Masm32 that does just what you've described.. you can use that..
Title: Re: dtata in .code segment
Post by: dedndave on February 21, 2012, 12:12:05 PM
 :P

.Code

LongerNameToo DB "Longer String", 0

start:

Invoke MessageBox, NULL, Offset LongerNameToo, NULL, MB_OK

Invoke ExitProcess, 0

End start


the code section has the default attribute of PAGE_EXECUTE_READ, meaning that code
may be executed and data may be read however, data may not be written
this can be altered by changing a bit in the PE EXE header or by using the VirtualProtect function

another way to go is to use seperate sections, then merge them at link-time with one of the following link switches
/MERGE:.data=.text
/MERGE:.text=.data

much cleaner than the other methods, i think
but - you should make a note of it at the beginning of the source file