Author Topic: Weird observation with Visual Studio [Mystery solved]  (Read 6587 times)

Ascended

  • Member
  • ***
  • Posts: 336
Weird observation with Visual Studio [Mystery solved]
« on: March 17, 2016, 06:07:31 PM »
Hi guys,

I was just playing with Visual Studio 2015 and I thought I'd take a look at the resultant ASM code for a very basic 'Hello world' style program.

Code: [Select]
int main()
{
return 0;
}

As you can see, it is basic as it gets.

VS did and awesome job and even did the XOR EAX,EAX trick on the zero return value.

Code: [Select]
.686P
.XMM

.model flat

INCLUDELIB OLDNAMES

_TEXT SEGMENT
_WinMainCRTStartup PROC
; Line 3
xor eax, eax
; Line 4
ret 0
_WinMainCRTStartup ENDP
_TEXT ENDS
END

This is where it gets interesting.

The output exe generated by VS.Net is 8.5 KB. But if you run the ASM generated file directly through ml.exe and link.exe the result is a file of only 1 KB.

Why is this?   :dazzled:
« Last Edit: March 18, 2016, 09:14:25 AM by Lonewolff »

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
Re: Weird observation with Visual Studio
« Reply #1 on: March 17, 2016, 06:30:19 PM »
"INCLUDELIB OLDNAMES" means the compiler adds code in a lib file. If you are curious, open the exe in Olly.

Ascended

  • Member
  • ***
  • Posts: 336
Re: Weird observation with Visual Studio
« Reply #2 on: March 17, 2016, 06:48:01 PM »
Why the huge size difference though?

[edit]
Just opened in Olly now. Can't tell how to find what lib it loaded in there though.

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
Re: Weird observation with Visual Studio
« Reply #3 on: March 17, 2016, 07:42:41 PM »
Instead of letting us guess what your problem is, why don't you zip the exe and attach it here? Would save a lot of time.

Ascended

  • Member
  • ***
  • Posts: 336
Re: Weird observation with Visual Studio
« Reply #4 on: March 17, 2016, 08:29:40 PM »
Instead of letting us guess what your problem is, why don't you zip the exe and attach it here? Would save a lot of time.

Um what? Not sure that you even read the OP.  ::)

There is no problem. Just an observation. This is why I posted in 'The Laboratory' and not as a question.

Compile in VS.net = 8.5 KB
Compile the resultant ASM file that VS.net supplies = 1.0 KB

Same end program but 7.5 KB difference in size.

GoneFishing

  • Member
  • *****
  • Posts: 1071
  • Gone fishing
Re: Weird observation with Visual Studio
« Reply #5 on: March 17, 2016, 09:36:34 PM »
I guess VS does a debug build with full debug info

Ascended

  • Member
  • ***
  • Posts: 336
Re: Weird observation with Visual Studio
« Reply #6 on: March 17, 2016, 09:38:00 PM »
Possibly. But it was in release mode with full optimisations enabled.

GoneFishing

  • Member
  • *****
  • Posts: 1071
  • Gone fishing
Re: Weird observation with Visual Studio
« Reply #7 on: March 17, 2016, 09:41:22 PM »
Ok, then use one of PE viewers and compare both variants
VS likes link crt libs too :-)

mabdelouahab

  • Member
  • ***
  • Posts: 454
Re: Weird observation with Visual Studio
« Reply #8 on: March 17, 2016, 11:30:43 PM »
Lonewolff
I tried the same thing with VS2012
Code: [Select]
int main()
{
return 0;
}
the output listing generated by vs is diferent
Code: [Select]
; Listing generated by Microsoft (R) Optimizing Compiler Version 17.00.50727.1

TITLE C:\foo.cpp
.686P
.XMM
include listing.inc
.model flat

INCLUDELIB MSVCRT
INCLUDELIB OLDNAMES

PUBLIC _main
; Function compile flags: /Odtp
_TEXT SEGMENT
_main PROC
; File c:\foo.cpp
; Line 2
push ebp
mov ebp, esp
; Line 3
xor eax, eax
; Line 4
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END


The output exe generated by VS  (6 KB) = The output exe generated by ml.exe (6 KB).
 

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
Re: Weird observation with Visual Studio
« Reply #9 on: March 18, 2016, 01:18:40 AM »
The output exe generated by VS  (6 KB) = The output exe generated by ml.exe (6 KB).

It will remain a mystery forever ::)

(unless, of course, somebody is courageous enough to zip the exe and attach it here)

Vortex

  • Member
  • *****
  • Posts: 2337
Re: Weird observation with Visual Studio
« Reply #10 on: March 18, 2016, 06:17:59 AM »
Hi Lonewolff,

The functions introduced by the C run-time libraries are the reason of the size increase of C executables. My solution is to use my own tiny C run-time library. Attached is an example displaying a blank windows coded with VC Express 2010 + Win 7 SDK. The executable is only 2 Kb.

Ascended

  • Member
  • ***
  • Posts: 336
Re: Weird observation with Visual Studio
« Reply #11 on: March 18, 2016, 07:13:40 AM »
Hi Lonewolff,

The functions introduced by the C run-time libraries are the reason of the size increase of C executables. My solution is to use my own tiny C run-time library. Attached is an example displaying a blank windows coded with VC Express 2010 + Win 7 SDK. The executable is only 2 Kb.

Ah, ok. Now that makes sense  :biggrin:

[edit]
Hang on a sec, no it doesn't - LOL

If the program is only doing this...

Code: [Select]
xor eax, eax
ret 0

How can it be requiring any lib functions behind the scenes?

Vortex

  • Member
  • *****
  • Posts: 2337
Re: Weird observation with Visual Studio
« Reply #12 on: March 18, 2016, 07:44:22 AM »
Hi Lonewolff,

Are you building a .NET executable? My method cannot be applied to .NET

Ascended

  • Member
  • ***
  • Posts: 336
Re: Weird observation with Visual Studio
« Reply #13 on: March 18, 2016, 07:47:10 AM »
No just a bare bones c++ application.

[edit]
After much playing around this morning it is all making sense now.

The entry point isn't actually what we see in our part of the source code (in this case '_main'). The entry point is '_mainCRTStartup' which is hidden away in the lib files.

It appears that _mainCRTStartup is doing all of the initialisation of the console etc.

So, even though I asked VS to compile a program that returns zero to the operating system, it is adding in all sorts of other crap assuming that I want this to all happen in a console window (which in this case is all additional unwanted stuff).

Mystery solved  8)
« Last Edit: March 18, 2016, 09:14:05 AM by Lonewolff »

jj2007

  • Member
  • *****
  • Posts: 10548
  • Assembler is fun ;-)
    • MasmBasic
Re: Weird observation with Visual Studio
« Reply #14 on: March 18, 2016, 12:21:18 PM »