News:

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

Assembly prerequisites - why C ?

Started by frktons, May 20, 2010, 08:51:56 PM

Previous topic - Next topic

joemc

windows.h has many sub-includes inside of it.  An implementation of the c runtime library is normally included automatically unless you turn it off (which is tricky to find this option sometimes btw).  Many things in C are compiler dependent.

redskull

Quote from: joemc on May 24, 2010, 01:42:58 AM
i would pick up one or two pieces of C++ at a time, not all of it is object oriented.  For example sometimes i prefer to declare a variable where i use it instead of at the beginning of a block. 

To each his own, but this is the exact thing I would try and discourage.  A C++ program is, by definition, 100% object oriented.  While things like declaring variables elsewhere within the code and even the increment operator are certainly convienient, they don't make for an OO C++ program.  I've met many a "experienced object oriented C++ programmer" who didn't even know what STL or UML was, and that just makes things that much more confusing for the poor guys starting out.  IMHO, if you use any of the C libraries, then you don't have a C++ program, plain and simple.  But again, just my biased opinion, so go with whatever helps you learn.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

joemc

Quote from: redskull on May 24, 2010, 02:18:15 AM
A C++ program is, by definition, 100% object oriented. 

Where is that definition listed?  When i search i find: C++ (pronounced "See plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. I am just kidding i have seen many with your view.  But i disagree. I don't have to use the heated seats in my Cadillac for it to be a Cadillac. It is a feature.  I view C++ as having the ability to program in more than one model.  I do discourage mixing them all together.  But i do use C++ features in a modular program regularly.  To each their own.   

Little side note, coming from C++ to MASM it is nice if you accidentally press ; at the end of a line it is just a comment :)

hutch--

Frank,

Quote
So, in order to optimize size , and probably speed as well, you have
to access the Windows API skipping the ANSI C library altogether?
I mean "MASM32 MSVCRT library" refers to Windows API call customized
for MASM32 ?

Not exactly, it comes from knowing that with later C compilers from Microsoft that if you call the provided MSVCRT you get one of the later side by side DLLs instead where if I use the library from MASM32 I get the original MSVCRT which is installed on every computer since win95b with IE loaded.

The C compiler is fine but the runtime libraries are badly designed and have too many functions bundled together so if your code calls one function you get a heap of other stuff as well.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: redskull on May 24, 2010, 02:18:15 AM

... go with whatever helps you learn.


I think it is a good point.  :lol

Quote from: joemc on May 24, 2010, 02:20:52 AM

C++ (pronounced "See plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. I am just kidding i have seen many with your view...   I view C++ as having the ability to program in more than one model. 

Yes there is a lot of debate about OOP OR NOT OOP with CPP, probably each person
choose his own model and stick with it until the model changes in his mind.  :P

Quote from: hutch-- on May 24, 2010, 03:14:44 AM

The C compiler is fine but the runtime libraries are badly designed and have too many functions bundled together so if your code calls one function you get a heap of other stuff as well.

I understand why you Assembly Masters switch to build your own libs,
the only problem is: everyone has his own libs, and they don't interact
easily or at all. Thousands of coding hours per hundreds of Asm coders
to buid 80-90% of the same thing  ::)

Does the "Don't reinvent the wheel" thing exist anymore?

Probably there is no solution at all. We like to build our own coding world.  :lol

Some twenty years ago I spent 3 years building a complete programming
environment in Clipper with all the stuff to build any application I had to
in few hours, it was a sort of virtual programming. It was DOS time and
If I had to rebuild those stuff for windows it'd take more time than the original
project. Things has became more complex and difficult to master actually.  :(

Reading the 14th chapter of "C for dummies"...  :P
So far only strange stuff and a humour I can't understand  :lol


Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

Its not that type of problem, with existing DLLs in Windows you are only creating IMPORT libraries, the function is in fact in the DLL so a call to SetWindowText() or any other API in the system is made directly to the DLL that contains it. All the masm32 version of MSVCRT does is build an import li8brary for the correct DLL, not a later side by side version. Also the build technique for MSVCRT is contained in the project so it is easy enough to understand.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

redskull

Quote from: joemc on May 24, 2010, 02:20:52 AM
I do discourage mixing them all together.

That's the key right there; it's nice to be able to do either mode in C++, and even nicer for experienced developers to mix the two a little bit when it benefits them.  But when those experienced developers write books for newcomers, it just makes life all the more difficult to know what's what.  I have three C++ books on my shelf; one goes to great lengths talking about how great pointers are, while the second says to avoid them like the plauge in favor of references, and the third says to eschew both for auto_ptr's. Three books, each with three conflicting "rules" about how to be a C++ programmer.  Several other of my C++ books include classes at the end, seemingly as an afterthought.

And while OOP is not required, C++ most certainly *implies* object orientation; for example, one could simply change his #include statments from 'stdio.h' to 'cstdio', and show the source as an example of "a program written entirely in an object oriented language", and be 100% correct, while at the same time being 100% wrong.  Any n00B trying to learn OOP from that type of program (or book) would most likely be hopelessly lost.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

frktons

Quote from: hutch-- on May 24, 2010, 01:01:28 AM
This is a bare C template I did some months ago. Sad to say the standard C runtime libraries are very poorly designed and bloated the file out to over 30k so I used the MASM32 MSVCRT library and it now builds at 4.5k which is competitive to a MASM executable.

Just the addition of the division (Sw/2-wid/2,) sucked in a maths library that blew its size out so the MSVCRT library is probably a must for minimum sized code in VC.

K & R notation filtered through the original Microsoft SDK layout style for API code. It builds but issues a warning on the change from FP to integer.


/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */

      #include <windows.h>

      LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

      HANDLE hInstance;
      HANDLE hWnd;                                  // global main window handle
      HICON  hIcon;                                 // local icon handle
      HANDLE hMenu;

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */

void main()

    {
      LPMSG         pmsg;                           // pointer for MSG structure
      WNDCLASSEX *  pwc;                            // pointer to WNDCLASSEX structure
      MSG           msg;                            // local msg structure
      WNDCLASSEX    wc;                             // local structure for RegisterClassEx
      int           wid;
      int           hgt;
      int           Sw;
      int           Sh;
      char          szTitle[]       = "Microsoft C Template";
      char          szWindowClass[] = "C_Template";

      /* --------------------------------
      get the application instance handle
      -------------------------------- */
      hInstance = GetModuleHandle(NULL);

      /* ---------------------------------
      Copy structure addresses to pointers
      --------------------------------- */
      pmsg = &msg;
      pwc  = &wc;

      hIcon = LoadIcon(hInstance,(LPCTSTR)5);

      wc.cbSize           = sizeof(WNDCLASSEX);
      wc.style            = CS_BYTEALIGNWINDOW | CS_BYTEALIGNCLIENT;
      wc.lpfnWndProc      = (WNDPROC)WndProc;
      wc.cbClsExtra       = 0;
      wc.cbWndExtra       = 0;
      wc.hInstance        = hInstance;
      wc.hIcon            = hIcon;
      wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
      wc.lpszMenuName     = NULL;
      wc.lpszClassName    = szWindowClass;
      wc.hIconSm          = hIcon;

      RegisterClassEx(pwc);     

      Sw = GetSystemMetrics(SM_CXSCREEN);              // screen width
      Sh = GetSystemMetrics(SM_CYSCREEN);              // screen height

      wid = Sw * .70;
      hgt = Sh * .70;

      /* -------------------------------
      limit the aspect ratio to 1.4 to 1
      ------------------------------- */
      if (wid > hgt * 1.4)
        {
        wid = hgt * 1.4;
        }

      hWnd = CreateWindowEx(WS_EX_LEFT,
                            szWindowClass,
                            szTitle,
                            WS_OVERLAPPEDWINDOW,
                            Sw/2-wid/2,
                            Sh/2-hgt/2,
                            wid,hgt,
                            NULL,NULL,hInstance,NULL);


      hMenu = LoadMenu(hInstance,(LPCTSTR)10);
      SetMenu(hWnd,hMenu);

      ShowWindow(hWnd,SW_SHOW);
      UpdateWindow(hWnd);

      while (GetMessage(pmsg,NULL,0,0))
        {
        switch (msg.message)
          {
          case WM_KEYUP:
            {
            switch (msg.wParam)
              {
              case VK_F1:
                {
                MessageBox(hWnd,"You pressed the F1 key","Howdy",MB_OK);
                break;
                }

              case VK_F2:
                {
                MessageBox(hWnd,"You pressed the F2 key","Howdy",MB_OK);
                break;
                }
              }
            }
          }
        TranslateMessage(pmsg);
        DispatchMessage(pmsg);
        }

    }

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */

LRESULT CALLBACK WndProc(HWND hWin,UINT uMsg,WPARAM wParam,LPARAM lParam)

  {
      switch (uMsg)
      {
        case WM_COMMAND:
        {
        switch (wParam)
            {
            case 1010:
              MessageBox(hWnd,"Open File","Howdy",MB_OK);
              break;

            case 1020:
              MessageBox(hWnd,"Save File","Howdy",MB_OK);
              break;

            case 1090:
              SendMessage(hWnd,WM_SYSCOMMAND,SC_CLOSE,(LPARAM)NULL);
              break;

            case 5000:
              MessageBox(hWnd,"C template with no STATIC C runtime library","About",MB_OK);
              break;
            }
        break;
        }

        case WM_CLOSE:
          MessageBox(hWnd,"WM_CLOSE Here","Howdy",MB_OK);
          break;

        case WM_DESTROY:
          PostQuitMessage(0);
          break;
      }

    return DefWindowProc(hWin,uMsg,wParam,lParam);

  }

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */


Sorry HUTCH, I tried to compile this example, provided by you, with Pelle's C
but I probably missed something. During the compilation it says:

C:\Documents and Settings\My Documents\Pelles C Projects\Samples\Win_API_HUTCH.c(16): warning #2181: Incorrect signature for entry-point 'main'; expected 'int __cdecl function(void)' but found 'void __cdecl function(void)'.
Building Win_API_HUTCH.exe.
POLINK: error: Unresolved external symbol '_WinMain@16'.
POLINK: fatal error: 1 unresolved external(s).
*** Error code: 1 ***
Done.


What did I forget?

Frank
Mind is like a parachute. You know what to do in order to use it :-)

TmX

Quote from: frktons on May 25, 2010, 08:04:40 AM
What did I forget?

rename void main() into int WinMainCRTStartup()
that works for me...

hutch--

Frank,

You probably don't have the right stuff to build it. Its done with the VCTOOLKIT and the matching SDK and C libraries.

I use this batch file.


@echo off

set lib=h:\vctoolkit\lib\
set include=h:\vctoolkit\include\

h:\vctoolkit\bin\rc.exe rsrc.rc

if exist project2.exe del project2.exe
if exist project2.obj del project2.obj

h:\vctoolkit\bin\cl /c /O1 /Os /TC project2.c
h:\vctoolkit\bin\Link /SUBSYSTEM:WINDOWS /entry:main /libpath:h:\vctoolkit\lib @project2.rsp rsrc.res

    : h:\vctoolkit\bin\cl /c /G7 /O2 /Ot /GA /TC /W3 /FA project2.c
    : h:\masm32\bin\PoLink /SUBSYSTEM:WINDOWS /entry:main /libpath:h:\msc6\lib @project2.rsp

dir project2.*

pause


and this response file saved as "project2.rsp".


project2.obj
kernel32.lib
user32.lib
gdi32.lib
msvcrt.lib


With Microsoft VC if you spec the main function as void you miss the C runtime library which is what I was after with this build. It uses the masm32 MSVCRT instead for the simple arithmetic operators.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

#40
Quote from: TmX on May 25, 2010, 12:03:08 PM
Quote from: frktons on May 25, 2010, 08:04:40 AM
What did I forget?

rename void main() into int WinMainCRTStartup()
that works for me...

It worked for me too  :U

It gives a couple of warnings, and builds the prog successfully.  :P

Building win_template.obj.
C:\Users\Documents\Pelles C Projects\Samples\Win_template\win_template.c(18): warning #2027: Missing prototype for 'WinMainCRTStartup'.
C:\Users\Documents\Pelles C Projects\Samples\Win_template\win_template.c(16): warning #2096: Missing return value.
Building win_template.exe.
Done.



Quote from: hutch-- on May 25, 2010, 12:33:06 PM
Frank,

You probably don't have the right stuff to build it. Its done with the VCTOOLKIT and the matching SDK and C libraries...

I think all the needed stuff is already there, in fact I've installed vc2003/vc2008 and vc2010 and a
lot more tools [the latest from MS SDK...].

By the way the size of the EXE is: 3.072 byte, and so your aim was achieved I guess.  :P

The program displays an empty window, and when I try to close it, a Message Box appears
with the usual button for closing it all.
F1 and F2 make more Message Boxes to appear.

If the program was intended to do that, it works fine.  :U

Thanks to both of you for assisting me in my fisrt steps into "C" as well  :bg

Frank


Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

Here is the resource file to link in with it.


/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */

    #include "\vctoolkit\include\windows.h"

    5 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "PROJECT.ICO"

    10 MENUEX MOVEABLE IMPURE LOADONCALL DISCARDABLE
    BEGIN
        POPUP "&File", , , 0
        BEGIN
            MENUITEM "&Open File", 1010
            MENUITEM "&Save File", 1020
            MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/
            MENUITEM "&Exit", 1090
        END
        POPUP "&Help", , , 0
        BEGIN
            MENUITEM "&About", 5000
        END
    END

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */


Make yourselrf a normal 766 byte icon and it will build at 4.5k.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: hutch-- on May 26, 2010, 01:31:46 AM
Frank,

Here is the resource file to link in with it.


/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */

    #include "\vctoolkit\include\windows.h"

    5 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "PROJECT.ICO"

    10 MENUEX MOVEABLE IMPURE LOADONCALL DISCARDABLE
    BEGIN
        POPUP "&File", , , 0
        BEGIN
            MENUITEM "&Open File", 1010
            MENUITEM "&Save File", 1020
            MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/
            MENUITEM "&Exit", 1090
        END
        POPUP "&Help", , , 0
        BEGIN
            MENUITEM "&About", 5000
        END
    END

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */


Make yourselrf a normal 766 byte icon and it will build at 4.5k.

Strange things have happened when I have compiled the resource file, added an icon
and build the whole project with Pelle's C.

1 - AVG says the program contains a trojan: Cryptic.GS
2 - Win7 tells me that I cannot access the device or the path/folder required

I attach all the files so that somebody can help me understand what's happening.

Frank
Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

It sounds like you need to shoot AVG. It does have its share of bad habits like this.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php