Normaly, you will create a data segment for your data, and a code segment for your code. This is however not the best way to go, if you are aiming for a
really small exe.
If you are going to include a lib made in asm into your c project, you will get two data "sections". This drasticly increases the size of the final exe.
Look at the following code:
.data
szMyString db "param1",0 ;7 bytes
.code
push offset szMyString ; 5 bytes
call myFunctionWithOneParameterPushed ; 5 bytes
(total of 7+5+5 = 17 bytes)
This is indeed a nice and structured way to program, but...
instead, look at this code:
.code
call _ff ; 5 bytes
szMyString db "param1",0 ;7 bytes
_ff:
call myFunctionWithOneParameterPushed ; 5 bytes
(also 17 bytes, but we got rid of the data section)
When we call the label _ff, the processor automaticly pushes the return value to the stack. And guess what, that return value is the address to our string!
So this has eliminated the use of a data segment... or has it?
There is still one more problem to solve: how do we write to the "read-and-execute-only" code segment?
We could use the win api to unprotect the code segment in runtime - but that would be useless because of the extra bytes needed to do so.
Instead, you can link the masm code with:
\masm32\bin\Link /SUBSYSTEM:WINDOWS "myProgram.obj" /section:.text,RWE /MERGE:.rdata=.data /MERGE:.data=.text
If you want to use code that write to the code segment in a c-program, you will have to link your c program so it makes the code segment (.text) writable - dont forget that.
If your compiler fails to do this, just use PEExplorer or something like that to make your exe's code segment writable.
I hope this help someone out there, struggeling with huge mysterial overheads between his c-code, and asm code.
Sorry if something is unclear, i had to much coffee.
