The MASM Forum Archive 2004 to 2012

Specialised Projects => Compiler Based Assembler => Assembler With Microsoft Visual C => Topic started by: Vortex on December 25, 2004, 02:36:17 PM

Title: Tiny C startup module to build smaller C executables
Post by: Vortex on December 25, 2004, 02:36:17 PM
Hi friends,

Here are two versions of my tiny C startup modules, one for console applications, the other one for GUI apps.
Both op the modules has the capacity of handling command line parameters.

Reducing the size of C executables, these modules are designed to use with Visual C and Pelle's C compilers.

Builded with these tiny modules, a simple application displaying a blank window is only 2 / 2.5 Kb


[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on December 27, 2004, 07:59:43 PM
Here is another version of the example compiled with Visual C, this one linked with Polink : final size = 1.5 Kb

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Opcode on December 28, 2004, 12:32:20 PM
Hi Vortex!

Very interesting work  :U

Can you explain me better the cl.exe option /Ogtyb2
I have readed the http://msdn.microsoft.com/library/en-us/vccore/html/_core_.2f.Og.asp, but
I don't have understood it very well...

Regards,
Opcode
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on December 28, 2004, 06:35:23 PM
Hi Opcode,

The switch /Ogtyb2 is a subset of /Ogityb2 maximizing optimizations. Type cl /? to see all the switches of the compiler coming with the free toolkit 2003 package:
Quote/O1 minimize space                       /Op[-] improve floating-pt consistency
/O2 maximize speed                       /Os favor code space
/Oa assume no aliasing                   /Ot favor code speed
/Ob<n> inline expansion (default n=0)    /Ow assume cross-function aliasing
/Od disable optimizations (default)      /Ox maximum opts. (/Ogityb2 /Gs)
/Og enable global optimization           /Oy[-] enable frame pointer omission
/Oi enable intrinsic functions
Title: Re: Tiny C startup module to build smaller C executables
Post by: Jibz on December 28, 2004, 08:08:30 PM
[plug]

In case anybody needs something similar, but is willing to sacrifice slightly more space to get support for more of the standard C runtime library, you might also want to check out LIBCTINY (http://www.wheaty.net/downloads.htm) by Matt Pietrek or WCRT (http://www.ibsensoftware.com/download.html) by me :U.

But Vortex' library gives the smallest executables (except for skipping startup code alltogether of course).

[/plug]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on January 02, 2005, 04:31:16 PM
Here are three new examples for PellesC, one for console applications, the other one for GUI apps and the last one for DLLs

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Xor Stance on March 17, 2005, 09:40:09 PM
Hi Jibz and Pelles C/C++ and Visual Studio :green, will their be an implement for C++ library?
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on March 18, 2005, 11:00:45 AM
Maybe :)
Title: Re: Tiny C startup module to build smaller C executables
Post by: mariø on March 18, 2005, 01:04:09 PM
thanks a lot Vortex :clap:
now i understand better what C must do at startup.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Xor Stance on March 18, 2005, 05:39:14 PM
 :U I forgot to mention Vortex, they're expertise in C/C++ and Visual Studio(maybe). :green
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on May 09, 2005, 05:42:48 PM
Here is an example for the Digital Mars compiler.

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on May 13, 2005, 09:59:19 AM
Is there anyone using Borland C++ 5.5? Maybe, I can try creating a startup module for this compiler.
Title: Re: Tiny C startup module to build smaller C executables
Post by: pbrennick on May 15, 2005, 04:31:29 AM
Vortex,
Thank you the the Digital Mars example.

Paul
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on May 22, 2005, 11:29:08 AM
Here is the same example compiled with GCC V3.2.3 from the MinGW package V3.1.0.1

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Phil on May 29, 2005, 02:11:31 AM
Thank you for sharing your code, Vortex. I have encapsulated it in the Fibonacci program I am working on. I still haven't quite figured out how you handle the quoted arguments so gracefully but I think your code is awesome! I'll credit you in my comments if its okay for me too keep your code there. (BTW, I'll eventually figure out how your xor is doing its magic but I just thought I'd let you know it wasn't obvious to me during the 2nd and 3rd reads! ;)
Title: Re: Tiny C startup module to build smaller C executables
Post by: rvalois on September 17, 2005, 05:24:40 AM
Vortex,
--------------------
Is there anyone using Borland C++ 5.5? Maybe, I can try creating a startup module for this compiler.
--------------------
I maybe late but if the offer still valid, I would like to have it.
Thank you,
RValois.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on September 17, 2005, 10:19:20 AM
Hi rvalois,

Welcome to the forum.

Here is an example package for you targetting both GUI and console applications.

Notice that I used the OMF linker from Digital Mars to create smaller executables compared to Borland's ilink32.exe

To get the Digital Mars C/C++ compiler package and tools :

http://www.digitalmars.com

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: rvalois on September 18, 2005, 11:15:28 AM
Hi Vortex,
-------------
Welcome to the forum.
-------------
Thank you but I have been here before. For some reason I had to re-register.


Unfortunately no luck compiling my code. Your example compiles fine.
This is what I got,
--------------------------------------------------------------------
G:\rvEd>f:\dm\bin\link -NOD -ENTRY:_WinMainCRTStartup -SU:WINDOWS -FIXE start.ob
j rved.obj,rved.exe,,kernel32.lib user32.lib comctl32.lib msvcrt.lib,rved.res
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

OPTLINK : Warning 23: No Stack
OPTLINK : Warning 174: 32-bit Segments Inappropriate for 16-bit Segmented output

start.obj(start)
Error 116: NEAR call to IMPORT at Relative 00003H  from
Segment _TEXT
FRAME  = Frame of Group FLAT 0000
TARGET = External Symbol GetModuleHandleA 00000H
FIXUPP Type = 32-bit Conditional JMP

G:\rvEd>
---------------------------------------------------------------------
This is what I'm trying to compile:http://www.rvalois.com.br/downloads/rvEd.zip
It is a skeleton dialog based windows application.

Another thing, What is necessary to compile VC and BCC DLLs ?

Sorry to bother you...

Thank you.
RValois.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on September 18, 2005, 05:34:20 PM
Hi RValois,

Here is the rebuilded project with the final executable sized 7Kb.
Run the batch file buildlib.bat to create the import libraries before building the project.

To build small DLLs , have a look at this thread:

Small DLLs without C startup code

http://www.masmforum.com/simple/index.php?topic=1121.0

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: rvalois on September 18, 2005, 10:33:36 PM
Duh, one comma.
Thank you very much.
Regards,
RValois.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on February 10, 2006, 07:39:07 PM
Here is a new library coded with POASM for Pelles C. The library should work also with VC++

http://vortex.masmcode.com/files/Wcrt0_5.zip
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on February 11, 2006, 05:18:05 PM
An example with MS VC++ Toolkit 2003

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on June 29, 2006, 06:46:28 PM
The command line parser could not handle tab characters separating command line arguments - bug fixed

http://www.vortex.masmcode.com/files/Wcrt0_6.zip
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 07:55:25 AM
Hi all.
Firstly let me apologize for posting this here, as opposed to creating a new thread, but i'm tired of seeing retards running around reminding everyone that this is not a C board. (Hutch, any chance of a C forum? ;))

Anyway, a friend of mine has been trying to teach himself C++ (with no previous programming experience mind, so you can imagine the kind of questions i get ;)). So he has been compiling small examples programs and sending them to me for a while, and I've been making them more standards compliant, and optimizing (if that's possible in cpp) etc.
The last program he sent me was a ~30 line cli program, consisting of a single line prompt, and based on the next typed character, it would reply with one of two answers, then exit.
After applying all the optimizations i could find in dev-cpp (using MinGW) i had an exe that was....wait for it.....FOUR HUNDRED AND SIXTY-THREE KILOBYTES. Needless to say i am less than impressed with this.
To get to the point, i wish to know if:
1) Is this within range of the size of an executable produced by a c++ compiler and
2) Can anyone suggest to me ways of shrinking it's size, or suggest a better compiler.

I have tried to compile said program in vc, but i end up with a blight of errors, which can be found in Errors.txt in the attached zip file none of which seem to make any sense to me. I am using the command line compiler that accompanies visual c express 2005, and the includes from the windows sdk 2003.
Any advice on this topic would be appreciated, as i simply do not find half a megabyte for a program of this caliber acceptable.
The attachment contains the source and compiled executable i refer to above.

Thanks, Ehtyar.

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: hutch-- on November 14, 2006, 09:14:35 AM
I just built your code using the older vctoolkit with this batch file. It builds at 73k.


@echo off

set pth=h:\vctoolkit\
set include=%pth%include\

if exist masm.exe del masm.exe
if exist masm.obj del masm.obj

:: speed
%pth%bin\cl /c /G7 /O2 /Ot /GA /EHsc masm.cpp

:: size
:: %pth%bin\cl /c /O1 /Os /EHsc masm.cpp

%pth%bin\Link /SUBSYSTEM:CONSOLE /libpath:%pth%lib masm.obj

dir masm.*

pause
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on November 14, 2006, 10:46:04 AM
Ehtyar,

A small note, my startup module is designed to be used with C code not C++

While proceesing C++ code, the compiler inserts some additional subroutines handling expections, streams, memory management etc. Dealing with all of them in C++ is more compicated compared to the C environment.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 10:58:39 AM
Yes Vortex, i am aware, i never tried to link your custom libs with the cpp program for that reason.

Hutch, i don't know what you did to make that work, but i have been trying for the past hour myself, and i still have errors from link.exe from the vctoolkit with ur bat file. Basically i have got it down to 6 unresolved externals:


masm.obj : error LNK2019: unresolved external symbol ___CxxFrameHandler3 referen
ced in function __ehhandler$??0?$ctype@D@std@@QAE@PBF_NI@Z
masm.obj : error LNK2019: unresolved external symbol __EH_epilog3 referenced in
function "public: __thiscall std::ctype<char>::ctype<char>(short const *,bool,un
signed int)" (??0?$ctype@D@std@@QAE@PBF_NI@Z)
masm.obj : error LNK2019: unresolved external symbol __EH_prolog3 referenced in
function "public: __thiscall std::ctype<char>::ctype<char>(short const *,bool,un
signed int)" (??0?$ctype@D@std@@QAE@PBF_NI@Z)
masm.obj : error LNK2019: unresolved external symbol __EH_prolog3_catch referenc
ed in function "public: bool __thiscall std::basic_istream<char,struct std::char
_traits<char> >::_Ipfx(bool)" (?_Ipfx@?$basic_istream@DU?$char_traits@D@std@@@st
d@@QAE_N_N@Z)
masm.obj : error LNK2019: unresolved external symbol __EH_epilog3_GS referenced
in function _main
masm.obj : error LNK2019: unresolved external symbol __EH_prolog3_GS referenced
in function _main
masm.exe : fatal error LNK1120: 6 unresolved externals


No matter what i link against (libcp, libcpd, libcpmt etc), i get all 6 of these, which are apparently error handling functions. Why i am getting them and not yourself i cannot say, but if you have any ideas...I'd be very appreciative.

Thanks, Ehtyar.
Title: Re: Tiny C startup module to build smaller C executables
Post by: sinsi on November 14, 2006, 11:04:31 AM
Quote from: Ehtyar on November 14, 2006, 07:55:25 AM
retards

that's a bit nasty isn't it?
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 11:09:29 AM
I believe it was yesterday that someone posted a C question in the workshop, and explicitly asked not to be reminded that this was an assembler board. The first post contained precisely such a reminder. I'd personally call that a little retarded, if not downright rude.

Ehtyar.
Title: Re: Tiny C startup module to build smaller C executables
Post by: sinsi on November 14, 2006, 11:17:36 AM
Well, maybe the  :wink should have been a  :bdg or  :lol - or maybe we need a :sarcastic
(that was my reading of it...)
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 11:28:03 AM
And maybe it just wasn't funny. *shrugs* Whatever floats your boat i guess, i just find it rather distasteful to directly contradict someone's request.

Ehtyar.
Title: Re: Tiny C startup module to build smaller C executables
Post by: sinsi on November 14, 2006, 11:34:41 AM
And I find calling people "retards" offensive...oh well
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 11:38:12 AM
I'll finish up now before i say something I'll regret later. Hutch, perhaps you have given some thought to a C forum?...

Night all, Ehtyar.
Title: Re: Tiny C startup module to build smaller C executables
Post by: hutch-- on November 14, 2006, 02:49:38 PM
Ehtyar,

All I did with the vctoolkit was to put the default C/C++ libraries and platformsdk libraries into the same LIB directory. The same with all of the includes into the include directory. From the errors you are getting it looks like you may not have the default C/C++ libraries available to your build.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 14, 2006, 09:38:27 PM
Well i am at a loss. The include lib and bin paths contain the vctoolkit, the 2003 SDK and the Vista SDK, and i have also tried using each individually with various results, none of which was success. Am i out of options?

Ehtyar.

[edit]
Seems I'm the only person on the planet with this problem, and no matter what i do, the errors return. I will try transferring all this to another machine soon, but till then, I'm out, bye all.
[/edit]

[edit2]
Well this is rather embarrassing, one of my batch files contained fully qualified paths for the lib, bin and include paths, so i had been using the wrong directory the entire time. Sorry to have wasted your time like that gents :(

Ehtyar.
[/edit2]
Title: Re: Tiny C startup module to build smaller C executables
Post by: Ehtyar on November 19, 2006, 10:39:45 AM
Sorry that it's taken me so long to get back to this topic, but i have made further advancements on this topic.
It seems iostream is a bone of great contention among c++ compilers as it contains most of the standard c++ functionality in itself alone. It seems that using certain defines helps your compiler decide what is necessary to include from iostream, and what is not.
I have used the following defines to shrink that 50k app to only 8k, a far more acceptable size for a program of such limited functionality. Naturally you could do it in MASM in about 2k, but that's the price one pays for using hlls right? ;)
#define WIN32
#define _CONSOLE
#define NDEBUG
These can be defined on the command line at compile time to make things easier: cl.exe /D "WIN32" /D "NDEBUG" /D "_CONSOLE" <....> mylittleprogram.cpp.
Of course, if you're creating a GUI application, simply switch _CONSOLE to _WINDOWS :)

Hope this helps, Ehtyar.
Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on August 03, 2007, 07:25:56 PM
Here is an example of building a small C executable with Vista SDK. Depending on your Vista SDK installation, you need to modify Variables.cmd and SetVars.bat to reflect the correct paths.

[attachment deleted by admin]
Title: Re: Tiny C startup module to build smaller C executables
Post by: GregL on August 30, 2007, 06:31:46 PM
Thanks for the example Vortex. It's interesting.

Title: Re: Tiny C startup module to build smaller C executables
Post by: Vortex on October 09, 2011, 06:34:01 PM
New version of the tiny C run-time startup library :

The NULL terminator character of the last command-line argument was not copied to the destination buffer by the module crt0cons. This bug is fixed now.

http://vortex.masmcode.com/files/Wcrt0_7.zip

ParseCmd5.zip : Command-line parser for Jwasm \ Masm \ Poasm console applications