The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: dividebyzero on June 26, 2009, 06:16:48 PM

Title: Me comparing C++ and Assembly HelloWorld
Post by: dividebyzero on June 26, 2009, 06:16:48 PM
Hey I'm very new to assembly!
I made a simple helloworld program in both c++ and assembly to compare the .exe file size.

these were the codes:

assembly (MASM):
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
.data
a db "Hello World!"
.code
start:
    invoke StdOut, addr a
    invoke ExitProcess, 0
end start


C++ (Dev-C++):
#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World!";
  return 0;
}



HelloWorld.exe file sizes
C++: 474,990 bytes
ASM: 15,027 bytes


ASM code was roughly 31.6 times smaller than the C++ code.


WOWOWOWOWOWOW ASSEMBLY IS PRETTY COOL! haha

:cheekygreen:
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Vortex on June 26, 2009, 06:24:08 PM
Hi dividebyzero,

Welcome to the forum.

The result is normal as C\C++ run-time libraries are adding extra code to the final executable. By creating your own C run-time library or turning it off, you can create a tiny C executable having a size of 1536 - 1024 bytes. You need also to specify some special linker options.

EDIT : Building your code, I get an executable of 2560 bytes ( ml.exe V6.14 + link.exe V5.12 ) Does your executable contain debugging code?
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: dedndave on June 26, 2009, 06:32:14 PM
i could put that in a tiny model in under 100 bytes - lol
real-mode dos, of course
but, yes, 15027 bytes seems like something is not right
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: dividebyzero on June 26, 2009, 06:35:56 PM
Quote from: Vortex on June 26, 2009, 06:24:08 PM
EDIT : Building your code, I get an executable of 2560 bytes ( ml.exe V6.14 + link.exe V5.12 ) Does your executable contain debugging code?


In the MASM32 editor, I clicked on Project > CONSOLE Assemble & Link. Then I ran the executable.
Is that what I'm suppose to do?
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: dedndave on June 26, 2009, 06:50:14 PM
that sounds correct
i use batch files and do it from the command line, however
this is the one i use for console mode apps

@echo off
if "x%1"=="x" goto ascusage
if exist %1.asm goto ascasm
:ascusage
echo Usage: asc asmfile
echo "asmfile" = asmfile.asm
goto batchexit
:ascasm
if exist %1.obj del %1.obj
c:\masm32\bin\ml /c /coff %1.asm >c:\masm32\bin\asmbl.txt
if errorlevel 1 goto showtxt
if exist %1.exe del %1.exe
if not exist rsrc.obj goto nores
c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF %1.obj rsrc.obj >>c:\masm32\bin\asmbl.txt
goto showtxt
:nores
c:\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF %1.obj >>c:\masm32\bin\asmbl.txt
:showtxt
if exist %1.obj del %1.obj
type c:\masm32\bin\asmbl.txt
:batchexit
dir %1.*
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Vortex on June 26, 2009, 06:56:46 PM
Hi dividebyzero,

You should try dedndave's batch file. It should output a small executable. Be sure that you are running a clean operating system.
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: dedndave on June 26, 2009, 06:59:16 PM
there may be selectable options or preferences in the editor
someplace, you have the "generate extra large exe files" switch selected - lol
even with debug info, that file should be under 5K
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: mitchi on June 26, 2009, 07:43:36 PM
I strongly advise NOT to use the C++ library (STL)
If you use C++ with objects and stuff like that, do it with the C library.

With the GNU compiler, you can get a printf hello world down to a 6 ko exe easily.
If you play more with the linker afterwards, you can get it down to 1 ko exe.


//file:hello.cpp
//gcc -s hello.cpp -o hello.exe
//2009-06-26  21:42             5 632      hello.exe
#include <stdio.h>
int main(void)
{
printf("hello world");
return 0;
}

Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: RotateRight on June 27, 2009, 03:09:46 PM
Vortex,

Just wondering if you use link.exe V5.12 on a regular basis
as your main linker?  If so, why do you prefer it to "newer"
linkers from ms?
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: TmX on June 27, 2009, 05:49:57 PM
Quote from: mitchi on June 26, 2009, 07:43:36 PM
I strongly advise NOT to use the C++ library (STL)

why?
the STL provides some useful algorithms & data structures
pretty useful, i guess
at least no need to reinvent the wheel
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: dedndave on June 27, 2009, 06:07:06 PM
i reinvent the wheel all the time, TmX - lol
half the time, didn't know the previous wheel existed
the other half, i wanna know what makes it go around
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Vortex on June 27, 2009, 07:52:55 PM
Hi RotateRight,

My main preference is Pelle's Polink.exe  Sometimes, I am doing some testings with link.exe V5.12 as it's the main MS linker version supplied with the Masm32 package.
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: mitchi on June 28, 2009, 08:27:59 AM
Quote from: TmX on June 27, 2009, 05:49:57 PM
Quote from: mitchi on June 26, 2009, 07:43:36 PM
I strongly advise NOT to use the C++ library (STL)

why?
the STL provides some useful algorithms & data structures
pretty useful, i guess
at least no need to reinvent the wheel

The STL containers are slow, they produce horrible ASM code, huge binaries, they make compilation slower and the error messages are a disaster.
Everything you use from the C library will produce clean code, sometimes it will even be inlined by the compiler inside your code.
Making your own data structures is the best. It's not about reinventing the wheel. As a programmer, it's part of your education.

Having said that, I used the STL a few times for school homework back when I was a noob and I was glad to have that.

Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: TmX on June 29, 2009, 05:54:58 AM
Quote from: mitchi on June 28, 2009, 08:27:59 AM
Making your own data structures is the best. It's not about reinventing the wheel. As a programmer, it's part of your education.

Having said that, I used the STL a few times for school homework back when I was a noob and I was glad to have that.

yes i'm agree with you
writing your own data structure (and not just merely copy pasting) is the best way to understand it  :U

i quite like STL, actually, somehow it reminds me of Java generics
and STL itself is one of the reasons why I use C++ :green
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: bruce1948 on June 29, 2009, 09:29:03 PM
I quite like the STL as well. After 30 years I like to let some one write the structures for me!!! :dance:
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Mark Jones on June 29, 2009, 11:38:23 PM
STL and MFC are not included in the Express versions, and there is nothing more frustrating than finding code you want to check out, but can't, because it relies on STL or MFC. (This of course goes way back to the original intention of .DLL files...) Boy, what a long strange trip it's been...
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Jibz on June 30, 2009, 12:22:20 PM
Quote from: Mark Jones on June 29, 2009, 11:38:23 PMSTL and MFC are not included in the Express versions

I assume you mean ATL and not STL :U.
Title: Re: Me comparing C++ and Assembly HelloWorld
Post by: Mark Jones on July 01, 2009, 08:52:01 PM
(http://www.cartoonstock.com/lowres/bst0020l.jpg)