News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Me comparing C++ and Assembly HelloWorld

Started by dividebyzero, June 26, 2009, 06:16:48 PM

Previous topic - Next topic

dividebyzero

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:

Vortex

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?

dedndave

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

dividebyzero

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?

dedndave

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.*

Vortex

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.

dedndave

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

mitchi

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;
}


RotateRight

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?

TmX

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

dedndave

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

Vortex

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.

mitchi

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.


TmX

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

bruce1948

I quite like the STL as well. After 30 years I like to let some one write the structures for me!!! :dance: