NO

Author Topic: Bored?  (Read 442 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1642
Bored?
« on: September 20, 2017, 04:25:01 pm »
If someone have a cloudy day, check this forum chain:

http://masm32.com/board/index.php?topic=6546.msg70177#msg70177

You learn how asm programmers hate C ;)

I don't hate asm and i can compile that example C/C++ code with Visual Studio 2008 with Microsoft C/C++ compiler version 19.10 and with WinDDK 7600, and result exe is 7 kb using msvcrt.dll.

So C/C++ compiler isn't the problem, only the linked CRT.

« Last Edit: September 20, 2017, 05:27:36 pm by TimoVJL »
May the source be with you

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: Bored?
« Reply #1 on: September 21, 2017, 08:50:46 pm »
Hi Timo,

I think the main problem is that M$ is deliberately obfuscating their stuff, especially the header files. Very complicated files which are not easy to translate to other languages. The increasing level of obfuscation and complexity might be the reason of that animosity. On the other side, Pelles development suit is a nice package not complicated like M$'s Visual Studio. As you mentioned, C\C++ examples can be built with the minimal dependencies like msvcrt.dll  Sadly, not everyone is aware of this.
Code it... That's all...

Offline jj2007

  • Member
  • *
  • Posts: 471
Re: Bored?
« Reply #2 on: September 21, 2017, 09:24:03 pm »
Quote
Countless C/C++ coders searching the internet in a desperate attempt to understand their endless error messages. Trial and error as the ultimate programming paradigm

That is just a simple description of the situation. And btw, if I do something in C, normally I use Pelles C because it just works, instead of dumping incomprehensible error crap on the user. Practically all the projects you posted here just build "as is", without desperate google searches.

@JJ,
cl /GS- /TC /GL /analyze- /W3 /Gy /Zc:wchar_t /Zi /Gm- /O1 /fp:precise /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /Oi /MD /nologo /Fe /Fombr.obj mbr.cpp /link /OUT:mbr.exe /ENTRY:main /SUBSYSTEM:CONSOLE /DYNAMICBASE:NO /FIXED /MACHINE:X86 /OPT:REF /SAFESEH:NO /INCREMENTAL:NO kernel32.lib ucrt.lib

Builds a 3 KB exe.

I am sure you will not get it to work, as usual. :badgrin:

Isn't that "commandline" funny?
« Last Edit: September 21, 2017, 09:25:59 pm by jj2007 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1642
Re: Bored?
« Reply #3 on: September 22, 2017, 10:20:07 am »
messy, these options are important in that case:
cl /GS- /MD /ENTRY:main /FIXED

This Small Application Wizard could be useful.
« Last Edit: September 22, 2017, 10:31:04 am by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 471
Re: Bored?
« Reply #4 on: September 22, 2017, 03:06:37 pm »
messy, these options are important in that case:
cl /GS- /MD /ENTRY:main /FIXED

Sure, let's make a test:
Code: [Select]
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe" /GS- /MD /ENTRY:main /FIXED "Tmp.c" user32.lib kernel32.lib ucrt.lib

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/ENTRY:main'
Tmp.c
Tmp.c: fatal error C1083: Cannot open include file: 'XED': No such file or directory

So sad, as Donald Dumb would say :'(

But let's test also the long commandline of aw27:
Code: [Select]
J:\Masm32\MasmBasic\Res>"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe" /GS- /TC /GL /analyze- /W3 /Gy /Zc:wchar_t /
Zi /Gm- /O1 /fp:precise /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /
Oi /MD /nologo /Fe /Fombr.obj Tmp.cpp /link /OUT:mbr.exe /ENTRY:main /SUBSYSTEM:CONSOLE /DYNAMICBASE:NO /FIXED /MACHINE:X86 /OPT:REF /SA
FESEH:NO /INCREMENTAL:NO kernel32.lib ucrt.lib
Tmp.cpp
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(79): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(110): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(141): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(153): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(163): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(174): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(184): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(194): error C2059: syntax error: '['
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(206): error C2059: syntax error: '['

More trial and error...? Now I will tell you a secret: This ridiculously simple hello world proggie fails miserably with various versions of MCVC and GCC, despite the commandlines suggested by real expertsTM, but it builds fine with Pelles C - provided you do something very logical, that is, add a W to the line
HANDLE hFile = CreateFile(L"\\\\.\\PhysicalDrive0", ...

Of course, it will still fail miserably with the other compilers, but hey, wouldn't it be fun to spend some weeks on solving the puzzle? Especially if you are bored, of course 8)
« Last Edit: September 22, 2017, 03:26:49 pm by jj2007 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1642
Re: Bored?
« Reply #5 on: September 22, 2017, 05:54:59 pm »
Oops.
cl /GS- /MD test.c /LINK /ENTRY:main /FIXED /NOCOFFGRPINFO kernel32.lib

EDIT:
cl /GS- /MD test.c -link /ENTRY:main /FIXED /NOCOFFGRPINFO kernel32.lib
« Last Edit: September 23, 2017, 09:24:17 am by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 471
Re: Bored?
« Reply #6 on: September 23, 2017, 01:19:50 am »
Yeah, that looks ... different:
Code: [Select]
J:\Masm32\MasmBasic\Res>"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe" /GS- /MD test.c /LINK /ENTRY:main /FIXED /NO
COFFGRPINFO kernel32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/LINK'
cl : Command line warning D9002 : ignoring unknown option '/ENTRY:main'
cl : Command line warning D9002 : ignoring unknown option '/NOCOFFGRPINFO'
test.c
test.c: fatal error C1083: Cannot open include file: 'XED': No such file or directory

Of course, with a lot of experience and a bit of trial and errorTM, you can overcome error D9002 (hint: four chars need to be changed in the commandline). Once you have solved this issue, you will stumble into a new, amazing puzzle called "error C2059", and here I am bit unfair and offer you the solution right away:
Quote
I tracked down the reason of those sudden compiler errors.

In the great tradition of windows.h it appears that Microsoft introduced tokens that cause name clashes with our codebase.

In our precise case, we had: #define Null (void*)0

Somewhere in our code, we need to use the offsetof macro and to make it available we #include <stddef.h>

I tracked down stddef.h in turns includes crtdefs.h which ends up including sal.h where our Null macro seems to clash with source code annotation in MS headers...

As a workaround, we did:....

There is no Null macro in aw27's code, but ... just try yourself, it's fun. There are people who play Zelda or Super Mario, but IMHO compiling a hello world proggie with a Micros**t compiler can be even more fascinating and challenging 8)
« Last Edit: September 23, 2017, 01:57:09 am by jj2007 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1642
Re: Bored?
« Reply #7 on: September 23, 2017, 10:26:26 am »
Sure i made a lot of mistakes.

test.c
Code: [Select]
void __cdecl mainCRTStartup(void)
{
__declspec(dllimport)void __cdecl exit(int status);
int __cdecl main(void);
exit(main());
}
int __cdecl main(void)
{
return 0;
}
cl /GS- /MD test.c /link /FIXED /NOCOFFGRPINFO
creates 1.5 kb exe

test.cpp needs some changes
Code: [Select]
#ifdef __cplusplus
extern "C" {
#endif
void __cdecl mainCRTStartup(void)
{
__declspec(dllimport)void __cdecl exit(int status);
int __cdecl main(void);
exit(main());
}
#ifdef __cplusplus
}
#endif
int __cdecl main(void)
{
return 0;
}
so this is the C/C++ compiler part of this story.
headers and CRT's are the next part.

Windows Driver Kit Version 7.1.0 have headers and libs for WindowsXP/Windows 7.
I collected header set from there.
An another is Microsoft Windows SDK for Windows 7, but it don't have msvcrt.lib for msvcrt.dll.
« Last Edit: September 23, 2017, 12:41:44 pm by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 471
Re: Bored?
« Reply #8 on: September 23, 2017, 12:49:00 pm »
Sure i made a lot of mistakes.
...
so this is the C/C++ compiler part of this story.
headers and CRT's are the next part.

No, Timo, you didn't do any mistakes. And I don't hate C, but I despise those who claim that there is a C "standard", that it is "portable" and other hot air promises. When you present a Micros**t C compiler with a hello world proggie that was designed for a version that is two years older or younger than the current one, it complains bitterly that it doesn't know how to build it. Throw it at GCC, and it will complain with loads of incomprehensible error messages. The whole "compatible and portable" story is just fake, it is a ridiculous religious belief. Assembler may be restricted to Windows XP and higher, but a) that covers 95% of the desktop market, and b) all my code builds out of the box with ML 6.15, 8.0, 9.0, 10.0, ... and JWasm, AsmC, UAsm64 and UAsm32. That is a standard.

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: Bored?
« Reply #9 on: September 23, 2017, 12:59:05 pm »
Jochen is right. Masm and the compatible Macro Assemblers are processing the same source code without those adventures. The problem is that compilers like MS VC and MinGW are very complicated. Plus, the obfuscated and complicated header files like hieroglyphs is another serious problem.
Code it... That's all...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1642
Re: Bored?
« Reply #10 on: September 23, 2017, 04:54:42 pm »
If someone write portable C code, code is quite portable too.
Original example was for win32.
So i am bored to this topic.
« Last Edit: September 23, 2017, 05:10:21 pm by TimoVJL »
May the source be with you