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

frktons

Good to know.

I'm just reading some barebone "C" syntax, as I don't like the idea of going
straight into objects and all the C++ stuff. I just need an intermediate level language
to prepare the backbone of applications and then connect it with Assembly
routines doing the "hard job". Being not so young, I cannot spend the rest of my life
to do everithing I'd like to in Assembly, my free time is limited, and family, and friends,
and travels, and hobbies demand for their space-time-energy stuff.  :P

"C" is a new language for me and different from all the previous ones I've used so far.
Assembly takes its time to be digested. 1+1=2. It's enough for the time being.  :lol

I considered also the pros of using C++ instead of C.
A very good and professional IDE from Microsoft, debugger, help, editor
all included and free.  :bg

If I use C I have to stick with some quite nice IDE like Pelle's C or others.
But I'd miss some extra (inline API documentation for example) features.

By the way. At the moment I think C is good enough, and it will take several months
at my slow pace to understand enough to start doing something useful. :eek

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

TmX

Quote from: frktons on May 23, 2010, 03:22:35 PM
I'm just reading some barebone "C" syntax, as I don't like the idea of going
straight into objects and all the C++ stuff.

You can always compile C codes using C++ compiler (not the other way around, though  :bg )

frktons

Quote from: TmX on May 23, 2010, 03:26:43 PM

You can always compile C codes using C++ compiler (not the other around, though  :bg )

Yes, of course if you like the console tools: CL.EXE and the like.  :P
Mind is like a parachute. You know what to do in order to use it :-)

GregL

Quote from: frktonsI considered also the pros of using C++ instead of C.
A very good and professional IDE from Microsoft, debugger, help, editor
all included and free.  BigGrin

You can use the Visual C++ IDE, debugger, help, editor etc. for C too.


hutch--

I echo the comments on how C is written, my generation wrote K & R formatting and it sunk in. Compacted blocks of C are close enough to unreadable as are non-matched indent braces.

These days I am so rusty I have to look up the notation to write a switch block as I have used other languages so long the notation format of C with "break" placement is the odd one out.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

redskull

Quote from: hutch-- on May 23, 2010, 04:41:26 PM
...the notation format of C with "break" placement is the odd one out.

Last year i had the privilege of attending a small Q&A with brian kernighan, and when someone asked the question 'Is there anything you wish you could go back and change about the C language', his only reply was "Case statments falling through".  :lol

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

tenkey

As for the keyboard problem, "The Annotated C++ Reference Manual" by Ellis & Stroustrup has an example of what C looks like on a Danish system using the ISO 646 (7-bit ISO) character set. It's in the section on trigraph sequences.

-----

C is pretty much in line with COBOL, PL/I, and Basic, in that it is a "procedure-oriented" language.

Some of the PL/I (a 1960's vintage language) terminology landed in C.
Examples:
  PL/I "structure" vs. Pascal and Cobol "record"
  "extern" and "static" storage attributes

-----

C is also a "low level" language, like BLISS, and a host of other "system implementation" languages. The other "SILs" have dropped out of sight.

C has explicit pointers with pointer arithmetic. This allows writing loops in an assembly language style, where you can increment a pointer to step through an array, rather than recalculating the address each time through a loop.

C makes no distinctions between integers and bit vectors that fit in the "native" data sizes. This allows assembly language-like reinterpretation of data.

If the integer size is big enough, C can explicitly copy pointers to integers and back again. Thus you can play games such as adding "marker" bits to addresses or XOR addresses together. (without using "unions")

C, as opposed to C++, has no object system, so any significant hidden overhead will be in functions you don't control (such as the standard file access and string formatting functions).

-----

As with assembly language, you can implement infrastructure for things like object systems, but you will also need to discipline yourself to avoid violating assumptions made by your infrastructure.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

frktons

#22
Quote from: Greg Lyon on May 23, 2010, 04:15:06 PM

You can use the Visual C++ IDE, debugger, help, editor etc. for C too.


I was not aware of that, it should not be straight on, maybe it is needed some
tweaks or customization? Let me know how to do that, it could be useful.  :U



Among the first things I've learned about C so far are the strange
behaviour of scanf() and getchar() of the standard ANSI library.

Things like "Remember to flush when you finish...".  :lol

After more or less 40 years of C being around there are so many
nasty tricks you have to be aware of in order to accomplish even
the most basical processing, that sometimes I wander why it has
became the "standard for system programming". Are we all masochistic?  :dazzled:

Surely the closeness to Assembly logic and the presence of HLL
control structures are good points in its favour.

It was created to simpify the life of Assembly Programmers, and it looks
like it won the game.  :P


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

oex

I learnt it all backwards starting with HTML then Perl then JS then PHP then VB then C then ASM due to my age, from this spread I'm of the opinion that what you want to achieve is writing it all yourself from scratch, none of these languages actually either do what you need or make any logical sense to purpose, consider the English language, even it fails to accomplish everything, it is still being added to and updated over time.... We no longer go down to Ye olde inn but down the pub :lol.... C is like a child writing shakespeare, it is a generic worldwide language and though based on logic has many characteristics that have dubious logic, to truely master programming one must have a good understanding of all languages and the math/logic behind them (that's when you see the Neo numbers and control all of space and time :lol) few even in this forum have seen the light, most of us simply meer glimpses....

Basically what I'm trying to say if I can master a little focus is that all languages serve the purpose of their creators, what you might do in c is useless to a large degree when writing programs for a graphics card or for a website it's like speaking Chinese to an Englishman 他不會明白 (he will not understand).... You can cobble something together 但譯者是胡扯 (But the translator is crap)

PS I thing google substituted rubbish for crap :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

Quote from: oex on May 23, 2010, 09:29:45 PM
I'm of the opinion that what you want to achieve is writing it all yourself from scratch, none of these languages actually either do what you need or make any logical sense to purpose.

If [what a useful token in all languages] I had enough time - space - energy then
{
    I'd redesign the structure of the universe;
}

Well I omitted the #include <stdio.h> and int main() and some minor things,
but it should run anyway  :lol

To be honest, when I look at 100 programming languages and maybe more
I recall what I've always imagined as a CODER DREAM:

A very wide library of well optimized code doing small but smart processing stuff
and a language that can easily access those stuff without having to loose one's head,
with an elegant syntax much more similar to human language than anything around.

Time by time old optimized stuff inside the library is updated with newer and better
ones, expanding the capacities of the language itself.

The .NET platform is doing something like that but in the worst imaginable way.  :eek

But, as you rightly pointed the attention to:

Quote
Basically what I'm trying to say if I can master a little focus is that all languages serve the purpose of their creators.

That's exactly the point. Languages are created for their own purposes [business, science, IT stuff, graphics...]
and from the point of view of the one or few people who undertake the task of doing it.

Considering the savage condition of the planet what else could be done?

So, not being in the CODER DREAM, I prefer to use a large part of what already
exists, trying to select what suits best my needs, and doing the remaining by myself.  :lol



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

GregL

frktons,

To use the Visual Studio with C does not take any special tweaks or customizations.  Just write C code and compile.  I name my files .c instead of .cpp.  There is an option under Project Properties -> C/C++ -> Advanced to "Compile as C Code" (/TC) that you can set too, although it's not required.  Just don't use any C++ features.




frktons

Quote from: Greg Lyon on May 23, 2010, 10:36:10 PM
frktons,

To use the Visual Studio with C does not take any special tweaks or customizations.  Just write C code and compile.  I name my files .c instead of .cpp.  There is an option under Project Properties -> C/C++ -> Advanced to "Compile as C Code" (/TC) that you can set too.  Just don't use any C++ features.

I promise I'll never use any C++ feature at least for 1 year  :lol
Thanks Greg  :U
Mind is like a parachute. You know what to do in order to use it :-)

hutch--

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

  }

/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

joemc

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. 

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

  }

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


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 ?

Frank

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