Hmm...

Using C i can create a simple messagebox program 1 kb small:



#include <windows.h>

#pragma comment(lib, "user32.lib")

#pragma comment(linker, "/release")
#pragma comment(linker, "/entry:main")
#pragma comment(linker, "/filealign:512")
#pragma comment(linker, "/merge:.rdata=.data")
#pragma comment(linker, "/merge:.text=.data")
#pragma comment(linker, "/warn")

int APIENTRY main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Small program", "Small", MB_OK);
return 0;
}


Using ASM i can get the program 1.5 kb small using some example code I found in the MASM package:



.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.code

start:

jmp @F
szDlgTitle db "Small program",0
szMsg db "Small program",0
@@:

push MB_OK
push offset szDlgTitle
push offset szMsg
push 0
call MessageBox

push 0
call ExitProcess

end start


Wasn't ASM supposed to be able to create smaller programs than other languages? Or have I missed something? Can someone help me to get the ASM program smaller than 1kb?

Thanks! :)
Posted on 2002-03-05 10:10:00 by Psionicist
Its because of...

#pragma comment(linker, "/filealign:512")
#pragma comment(linker, "/merge:.rdata=.data")
#pragma comment(linker, "/merge:.text=.data")

Those are linker options i think. Use them for the asm obj files.
Posted on 2002-03-05 10:13:51 by ChimpFace9000
this is how it should look:
: -----------------------------------------
: assemble small.asm into an OBJ file
: -----------------------------------------
\MASM32\BIN\ML.EXE /c /coff small.asm
if errorlevel 1 goto errasm

: --------------------------------------------------
: link the main OBJ file with the resource OBJ file
: --------------------------------------------------
\MASM32\BIN\LINK.EXE /SUBSYSTEM:WINDOWS small.obj /MERGE:.data=.rdata /MERGE:.data=.text
Posted on 2002-03-05 10:55:27 by NEMO
Nice!

Thanks!
Posted on 2002-03-05 11:15:20 by Psionicist
Why are you linking to kernel32.lib? I belive in MASM32 (if that is what you are using) that would add the library "linkage" into the file even if you dont use it. So that could incur some extra bytes :)
Posted on 2002-03-06 02:06:14 by Jag
Also I have noticed that you didnt change the alignment of the asm program. try
org 512

and see if it doesnt cut out some unused space. Also try using a Hex Viewer and see if there are alot of 00 in the exe, meaning that the section alignments are too high.
Posted on 2002-03-06 02:08:30 by Jag
firtsly: ExitProcess is api from KERNEL32.DLL so we need to link it too:grin:

secondly: if change align to 512 app our app wont run, i think that masm sets the align to theone which is perfect for our prog....... but i can be wrong:grin:

lastly: http://www.asmcommunity.net/board/index.php?topic=1953&highlight=size
Posted on 2002-03-06 03:47:24 by NEMO
If you must, use /FILEALIGN:512 . This is good for small apps, but
for larger apps the beneift shrinks (as well as the benefit of using
section merging), and you 'might' get additional overhead.
Posted on 2002-03-06 05:15:53 by f0dder
Sorry, Didnt think about ExitProcess :) Thanks NEMO for pointing that out :)
Posted on 2002-03-06 08:53:08 by Jag
Jag thanx.... at last some-one appreciate it:grin: :grin: :grin:

strange, i couldn't find /FILEALIGN: on LINK.EXE help and even in MASM611 - Reference's nothing is mention:confused:
Posted on 2002-03-06 10:31:31 by NEMO
Yeah, it's strange that they forgot to mention /FILEALIGN while they
do mention /ALIGN ... /ALIGN is only useful when dealing with VxDs.
Posted on 2002-03-06 11:25:50 by f0dder
ahhhh... thx Micor$oft -- you did it again:grin:
Posted on 2002-03-07 15:18:36 by NEMO