Author Topic: High Level Language in MASM  (Read 80427 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5430
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: High Level Language in MASM
« Reply #45 on: November 23, 2012, 10:09:12 AM »
I am inclined to think that compilers and assemblers are different animals, you can get some overlap between them but the base architecture between them is fundamentally different. You can in fact get the best of both worlds by using both a C compiler and an assembler and isolate the code in object modules which you then link into a finished binary. This allows you to fully leverage what both do best, an optimising compiler is free to tweak anything without having to accommodate manual code and an assembler module can handle anything you write in it without the restrictions and irritations of a compiler.

I agree with Michael here in that while MASM is able to emulate many higher level techniques, it will never be a high level tool, likewise a C compiler can do many things that an assembler can do with its inline assembler but its specifications of preprocessing are toothless terrors alongside MASM. The idea is that while they overlap, neither will ever be the other. The assumption that you can only use one tool in binary production is flawed in that mixed language programming has been with us for a very long time and it allows you to get the advantages of multiple tools.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

nidud

  • Member
  • *****
  • Posts: 1507
    • https://github.com/nidud/asmc
Re: High Level Language in MASM
« Reply #46 on: November 23, 2012, 10:39:24 AM »
The problem as I see it is that no matter how much you massage the high-level syntax, or how creative your macros are, you still do not have anything close to a HLL syntax. To get the full benefit of coding in a HLL you need a HLL compiler. I think the time would be much better spent developing a compiler that would allow you to more or less freely mix HLL code with MASM/JWASM code and macros.
My idea was to remove the usage of macros, not to create new ones.

The reason why I stopped here was that I use my own C libraries (written in assembly), and I assumed the MASM32 package was the same, but it seems to be based on macros interfacing with C.

So, the argument that the use of macros should be limited, or not used at all will be hard to do here. :biggrin:

Sphinx C--
The plan was to improve (and synchronise) the already existing architecture at hand. Reducing the learning curve (and cost) by using the same abbreviations (strlen, memcpy, ..) and help system (msdn) as already established in C to increase readability and usage of assembly.

http://en.wikipedia.org/wiki/The_Mythical_Man-Month

I am inclined to think that compilers and assemblers are different animals, you can get some overlap between them but the base architecture between them is fundamentally different. You can in fact get the best of both worlds by using both a C compiler and an assembler and isolate the code in object modules which you then link into a finished binary. This allows you to fully leverage what both do best, an optimising compiler is free to tweak anything without having to accommodate manual code and an assembler module can handle anything you write in it without the restrictions and irritations of a compiler.

I agree with Michael here in that while MASM is able to emulate many higher level techniques, it will never be a high level tool, likewise a C compiler can do many things that an assembler can do with its inline assembler but its specifications of preprocessing are toothless terrors alongside MASM. The idea is that while they overlap, neither will ever be the other. The assumption that you can only use one tool in binary production is flawed in that mixed language programming has been with us for a very long time and it allows you to get the advantages of multiple tools.
Well put, but the possibility of narrowing the gap between them could be useful in some cases. And if it could be done without compromising existing code, I think one should explore this possibility.

dedndave

  • Member
  • *****
  • Posts: 8789
  • Still using Abacus 2.0
    • DednDave
Re: High Level Language in MASM
« Reply #47 on: November 23, 2012, 11:41:41 AM »
macros certainly have their place
they are great for saving keystrokes, especially during the debug phase
on the flip side, if you are trying to learn windows programming and masm, they can hide things you want to learn
if you know the OS and you know the assembler - they are great   :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8442
  • Assembler is fun ;-)
    • MasmBasic
Re: High Level Language in MASM
« Reply #48 on: November 23, 2012, 02:19:21 PM »
I use my own C libraries (written in assembly), and I assumed the MASM32 package was the same, but it seems to be based on macros interfacing with C.

Only a small part of the Masm32 package links to the C run-time library. MasmBasic has actually only one reference to crt_qsort, all the rest is pure assembler.

[rant]My impression is that you see lots of non-existent problems. Many people use HLL constructs in assembler, and do not start weeping when occasionally they forget that .elseif MyMacro(eax) won't work. That's about half as bad as C's habit to fall thru switches (shudder :dazzled:).

Every time I touch C code or, worse, C compilers, I get the creeps.

First, it never works from the start, because everybody uses a different compiler, different libraries, different switches. You spend a lot of time hanging on the web just to convince a hello world snippet to compile properly.

Second, it's soo sloooooow. Building a C snippet is a factor ten slower than the same in MasmBasic using JWasm. Visual C Express? What does "Express" mean? That you can go for an Espresso while the damn bloatware is loading??

Third, the horrible syntax with all those {brackets that cause Repetitive Stress Injury} and the strange obligation to tell the compiler that the end of a line is not 0D0Ah but rather a semicolon. And instead of printing, C prefers to cout. Hilarious.

Of course, everybody grows up with a different language. Over one Billion people believe that Chinese is an easy language. My languages are FORTRAN, Basic and assembler, and I firmly believe that assembler is a fantastic language, provided you know how to use macros wisely. A mix of MasmBasic, "pure" assembler and WinAPI calls is hard to beat in terms of productivity...
[/rant]

CommonTater

  • Guest
Re: High Level Language in MASM
« Reply #49 on: November 23, 2012, 04:15:29 PM »
Every time I touch C code or, worse, C compilers, I get the creeps.

ROFL... here we go...

(counter-rant)
Having (at this point) written something well in excess of a million lines of C code, I have to say my experience has been somewhat different...

C falls through switches by design... so you can do this:
Code: [Select]
switch (x)
  {
     case 1:
     case 7:
     case 2:
       puts( "1 7 or 2");
       break;
     case 4 :
       puts ("Definately 4);
       break;
     default:
        puts(" something else");
  }

The C language is relatively standardized by ISO C-99 and ISO C-11 ... properly written programs from any compiler using either of those standards will compile on any other compiler using the same standard.

Yes, C compilers are a bit slowish... but you only compile something once... from there the actual speed differences between well written C code and well written ASM code are trivial in most cases.  Especially true with an optimizing C compiler. 

That horrible syntax with all those brackets is not without purpose... The brackets mark "scopes" which serve to separate blocks of code... The open brace actually creates a stack frame ... the code runs on that... then the closing brace destroys it.  C rules of scope are very handy at times, especially when you have a long subroutine with several sets of independent variables...

In the case of VC++ "Express" means "Free".

The C language has it's place in the world, just like any other.   The real problem here, IMO, is that people just love trashing what they don't fully understand.
(/counter-rant)
 
 

nidud

  • Member
  • *****
  • Posts: 1507
    • https://github.com/nidud/asmc
Re: High Level Language in MASM
« Reply #50 on: November 23, 2012, 04:23:20 PM »
My impression is that you see lots of non-existent problems. Many people use HLL constructs in assembler, and do not start weeping when occasionally they forget that .elseif MyMacro(eax) won't work.
The janitor is standing on the roof with a shingle in his hand; desperately trying to fix the leak in the roof while the house is floating down the river. The architect is not to blame for this; his job is to design things. Neither is this the fault of the janitor; his job is to fix things. The problem was the location of the house; it was built on sand.

I have never seen any problems with regards to these issues, only opportunities; a chance; an opening; a break; a prospect. The problems you mention are a positive; not a negative. Without these problems the prospect of an opening to these possibilities will not exist.

So, relax JJ, the rain is poring down but there are no leakage  :biggrin:

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: High Level Language in MASM
« Reply #51 on: November 23, 2012, 05:11:45 PM »
C falls through switches by design... so you can do this:
Code: [Select]
switch (x)
  {
     case 1:
     case 7:
     case 2:
       puts( "1 7 or 2");
       break;
     case 4 :
       puts ("Definately 4);
       break;
     default:
        puts(" something else");
  }

By design, yes, but IMO the original authors did not spend enough time on the design. They could have provided more functionality with less code. For example Greg Falen’s switch/case macros:
Code: [Select]
;==============================================================================
    include \masm32\include\masm32rt.inc
;==============================================================================
    .data
    .code
;==============================================================================
start:
;==============================================================================
    FOR eax,<0,1,2,3,4,5,6,7,8,9>
        SWITCH eax
            CASE 1,2,3
                printf("1,2,3\n")
            CASE 4
                printf("4\n")
            CASE 5..9
                printf("5..9\n")
            DEFAULT
                printf("default\n")
        ENDSW
    ENDM
    inkey
    exit
;==============================================================================
end start

Or a mainstream BASIC:
Code: [Select]
for i as integer = 0 to 10
    select case i
        case 1,2,3
            print "1,2,3"
        case 4
            print "4"
        case 5 to 9
            print "5 to 9"
        case is > 9
            exit select
        case else
            print "else"
    end select
next
sleep
Well Microsoft, here’s another nice mess you’ve gotten us into.

CommonTater

  • Guest
Re: High Level Language in MASM
« Reply #52 on: November 23, 2012, 05:19:23 PM »
By design, yes, but IMO the original authors did not spend enough time on the design.

Don't get me wrong... I have my grumbles about C too...

In the case of C's switch statement, yes, we agree they could have done a LOT better. The compiler output is essentialy the same as for an if - else if chain, so there's really no penalty for it other than clumsy syntax.  For high level languages I think Pascal did this one best, with ranges, lists and enums in switches.

Now if someone could come up with a C compiler with **real strings**...  that would be a step in the right direction.




anta40

  • Member
  • ***
  • Posts: 299
Re: High Level Language in MASM
« Reply #53 on: November 23, 2012, 05:28:58 PM »
For high level languages I think Pascal did this one best, with ranges, lists and enums in switches.

Code: [Select]
#include <stdio.h>

int main(void){

    int num = 99;

    switch(num){
        case 1 ... 40:
            printf("< 50");
            break;
        case 49 ... 100:
            printf("> 50");
            break;
        default:
            printf("error");
            break;
    }

    return 0;
}

gcc will happily compile that code, and it will work as expected (like in Basic or Pascal)
http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Case-Ranges.html

of course, it's not a valid ISO C code  :(

Now if someone could come up with a C compiler with **real strings**...  that would be a step in the right direction.

Too late, maybe  :P

jj2007

  • Member
  • *****
  • Posts: 8442
  • Assembler is fun ;-)
    • MasmBasic
Re: High Level Language in MASM
« Reply #54 on: November 23, 2012, 07:05:43 PM »
Every time I touch C code or, worse, C compilers, I get the creeps.

ROFL... here we go...
...
C falls through switches by design... so you can do this:
Redmond speaking?
 
Quote
That horrible syntax with all those brackets is not without purpose... The brackets mark "scopes" which serve to separate blocks of code... The open brace actually creates a stack frame ... the code runs on that... then the closing brace destroys it.  C rules of scope are very handy at times, especially when you have a long subroutine with several sets of independent variables...

You mean something like this?
Quote
   if (myflag)   //C needs (lots) (of) (brackets) (everywhere)
      {
         cout << "flag was set" << endl;
         cout << "hooray" << endl;
      }
   else
      {
         cout << "flag was clear" << endl;
      }

IMHO the "design" could be improved:
Quote
   .if myflag   ; Masm is user-hands-friendly
      PrintLine "flag was set"
      Print "hooray"
   .else
      PrintLine "flag was clear"
   .endif
;)

P.S.:
> The open brace actually creates a stack frame
In Masm we use proc ... endp, which helps to distinguish code blocks that are called from those that are separated by branching (.if ... .elseif ... .endif). These so-called "procs" are very handy if you have long blocks of code ;-)

jj2007

  • Member
  • *****
  • Posts: 8442
  • Assembler is fun ;-)
    • MasmBasic
Re: High Level Language in MASM
« Reply #55 on: November 23, 2012, 07:16:44 PM »
    switch(num){
        case 1 ... 40:
            printf("< 50");
            break;
        case mystart ... myend:
            printf("> 50");
            break;
        default:
            printf("error");
            break;
    }

    return 0;
}

The green part would be easy to implement in Masm. What about the red part? Would GCC understand it?

anta40

  • Member
  • ***
  • Posts: 299
Re: High Level Language in MASM
« Reply #56 on: November 23, 2012, 07:44:46 PM »
You mean like this?
Code: [Select]
#include <stdio.h>

int main(void){

    int num = 99;
    int mystart = 49;
    int myend = 100;

    switch(num){
        case 1 ... 40:
            printf("< 50");
            break;
        case mystart ... myend:
            printf("> 50");
            break;
        default:
            printf("error");
            break;
    }

    return 0;
}

Doesn't work, unfortunately (tested with GCC 4.7.2)
Quote
switchtest.c: In function 'main':
switchtest.c:13:9: error: case label does not reduce to an integer constant


hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5430
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: High Level Language in MASM
« Reply #57 on: November 23, 2012, 08:16:30 PM »
 :biggrin:

DAMN, I knew it would happen !!!! HOLY WAR !   :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8442
  • Assembler is fun ;-)
    • MasmBasic
Re: High Level Language in MASM
« Reply #58 on: November 23, 2012, 08:49:55 PM »
You mean like this?
...
Doesn't work, unfortunately (tested with GCC 4.7.2)

What a pity. Try this:
Code: [Select]
include \masm32\include\masm32rt.inc
.code
start:
  mov eax, 8
  mov edx, 7
  mov ecx, 9
  switch eax
  case 1
  print "my var is one", 13, 10
  case edx .. ecx
  print "my var is between 7 and 9", 13, 10
  case 10 .. 15
  print "my var is between 10 and 15", 13, 10
  case 16
  print "my var is sixteen", 13, 10
  default
  print str$(eax), " is nowhere in the ranges above", 13, 10
  endsw
  inkey "OK?"
  exit
end start

;-)

CommonTater

  • Guest
Re: High Level Language in MASM
« Reply #59 on: November 23, 2012, 11:01:47 PM »
You mean something like this?
Quote
   if (myflag)   //C needs (lots) (of) (brackets) (everywhere)
      {
         cout << "flag was set" << endl;
         cout << "hooray" << endl;
      }
   else
      {
         cout << "flag was clear" << endl;
      }

Nope... that's C++ 
This is C...
Code: [Select]
if (Myflag)
  puts("flag was set\n hooray");
else
  puts("flag was clear");

FYI  they are two separate languages.