News:

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

dtata in .code segment

Started by zigzag, February 21, 2012, 09:24:01 AM

Previous topic - Next topic

zigzag

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?

donkey

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
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

zigzag

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!

vanjast

There's a text macro in Masm32 that does just what you've described.. you can use that..

dedndave

 :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