how to optimize the code, so that the exe file becomes the smallest minimum in size? is it possible?



.data
MSGINHALT db "Hallo ollaH",0
MSGTITEL db "hi :)",0

.code
start:
invoke MessageBox, 0, ADDR MSGINHALT, ADDR MSGTITEL, MB_YESNO or MB_ICONQUESTION
cmp eax,IDYES
je start
invoke ExitProcess, 0
end start


don't tell me to remove the strings or make them shorter :grin:
Posted on 2002-06-17 13:00:57 by NOP-erator
Only use one section in stead of two. Put your data in your code section, instead of where they are now.

Fake
Posted on 2002-06-17 13:11:34 by Fake51
.386

.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.code
MsgCaption db "Iczelion's tutorial no.2",0
MsgBoxText db "Win32 Assembly is Great!",0
start:
invoke MessageBox, NULL,addr MsgBoxText, addr MsgCaption, MB_OK
invoke ExitProcess,NULL
end start
Posted on 2002-06-17 13:15:44 by stryker
hmm, but why do we use a .data section in other programs then if we can define our data in the .code section..

-NOP-
Posted on 2002-06-17 13:18:09 by NOP-erator
You might want to read this. :)
Posted on 2002-06-17 13:27:48 by stryker
thx, but i don't think that this thread answered my question why we use .data section when we can define data in the .code section.. i couldn't extract any answer of this thread...

thx,
-NOP-
Posted on 2002-06-17 13:40:36 by NOP-erator
I'm not sure since this hasn't happened to me yet...I've heard that there might be some problems during the loading of the .exe when the code section and the data sections are merged(the sample code of what just happened above or using the linker options /merge ...)
Posted on 2002-06-17 13:43:49 by stryker
ehm...does anyone have some linker parameters to merge the .text end the .rdata sections as described in the thread?


/Delight
Posted on 2002-06-17 13:44:49 by Delight
The problem with merging comes from the fact that by default the code section is read-only.

Try modifying the buffer in the code section and ka-boom!

You can change this at link time, but then there are security concerns... If the data is read-only by its nature (or use) as in this case, then there is no problem.

Mirno
Posted on 2002-06-17 13:47:19 by Mirno
MASM32\BIN\LINK.EXE /SUBSYSTEM:WINDOWS filename.obj /MERGE:.data=.rdata /MERGE:.data=.text

oh yeah, I remember most code sections are setup up like this(declaring segments the other way around)...

_TEXT SEGMENT READONLY PAGE PUBLIC USE32 'CODE'
_TEXT ENDS

try removing READONLY maybe it'll work...
Posted on 2002-06-17 13:48:48 by stryker
Back in the 16 bit days, code was usually addressed with the CS segment register, and data by the DS and/or ES segment register.

As mentioned, under Win, the code section is normally read-only, and the data section read/write. It makes no difference in your example, but it won't work if you try to modify a data item.

Also today's CPUs have a code cache and a data cache. In larger applications, mixing code and data results in inefficient cache utilization, and slows down the program. See Agner Fog's optimization guide for details.

:)
Posted on 2002-06-17 13:57:13 by S/390
Stryker, your parameters works great, but I have a new problem. linking with /section:.text,rwe dont work-

LINK : fatal error LNK1181: cannot open input file "rwe.obj"

What's wrong here??

/Delight
Posted on 2002-06-17 14:56:02 by Delight
You probably had a space character between the commas and rwe option.
Posted on 2002-06-17 15:11:02 by stryker
The problem only appears when using RadASM (by editing the .rap file). It looks like I have to link manually to solve this.


/Delight
Posted on 2002-06-17 15:19:34 by Delight
You can also use your own dos stub to cut down the size.
Posted on 2002-06-17 21:05:34 by C.Z.


sub edi,edi
push edi
push ofs MsgCaption
push ofs MsgBoxText
push edi
callW MessageBox
push edi
callW ExitProcess


altought we loose 2 bytes with the SUB EDI,EDI, we save 3, one for each PUSH EDI

result: 1 byte less :grin:

ancev
Posted on 2002-06-17 21:56:25 by ancev
it doesn't matter how small you get it. it's still gonna take up 4k on the disk. maybe 8k on fat32 i think
Posted on 2002-06-17 22:44:07 by Sloat


push 0 ; 6A 00
push ofs MsgCaption ; 68 xx xx xx xx
push ofs MsgBoxText ; 68 xx xx xx xx
push 0 ; 6A 00
jmp MessageBoxA ; E9 xx xx xx xx or FF 15 xx xx xx xx


is even smaller :), after box, kernel will jmp to the where ExitThread + xx address is stored and will exit our application

for bigger code i would use special table with offsets + constans



table label byte
lpMsg dd offset MsgCaption
lpText dd offset MsgBoxText

dd offset MessageBoxA
dd offset ExitProcess
...


or use structure, then load table address into EBX ESI or EDI, and call it using call dword ptr or call .lpMsg
Posted on 2003-04-22 16:06:06 by bart
sloat, default cluster size on fat32 is 4k. Won't matter (and is pretty damn silly) for normal executables, but if you're messing with injection or other size-limited coding (4k intros for instance), it can matter.

Be careful about depending on the "exitthread-offset-on-stack-at-program-entry" trick. First, I don't really know if this is ExitThread. Second, if it is, it's not guaranteed to be this. And even while it does exit your thread on all current win32 versions I know of, there's problems if you're doing MultiThreading.
Posted on 2003-04-22 16:11:32 by f0dder
ret trick does work with all current versions of Windows, when its used in thread, thread is terminated, if you dont belive me, or you have read it in some shitty ms bulletin, just trace it under si and you'll see :), its present in windows for so long, that's why i think it wont change in the future, but who knows :)

might be corrupted, when file is packed by some exe packer/protector eg. FSG, coz EBP is corrupted and after ret, kernel does mov ,reg32 :rolleyes:
Posted on 2003-04-22 16:25:07 by bart