The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: FritzGamaliel on March 26, 2010, 04:56:19 AM

Title: How to make a new programming language with MASM32v10?
Post by: FritzGamaliel on March 26, 2010, 04:56:19 AM
We must followed MASM32v10' grammar if we want to create a program in MASM32v10.

Example:
if we want to do:    number1 + number2  ----> result
We must call a function:
invoke add,ADDR number1,ADDR number2,ADDR result
But, I want to know how to make that line same with the code below:
number1 + number2 = result




And, I want to know how MASM32v10 create it's grammar(example: invoke followed by functionname and parameters)
and it's skeleton(.data, .data?, .code):
;=================================
include myFile.inc

.data

.data?

.code
start:
     invoke functionname,ADDR par1,ADDR par2,ADDR par3             ======> MASM32'grammar
end start
;=================================


Thank you
Fritz
Title: Re: How to make a new programming language with MASM32v10?
Post by: donkey on March 26, 2010, 06:18:35 AM
MASM32 is a collection of libraries, example code, macros and equates, it is not a programming language. MASM, the assembler distributed with MASM32 is the Microsoft Macro Assembler (ml.exe) and its syntax (grammar) for the most part comes from Intel and Microsoft. If you would like to know how to write a compiler you can start with a primer, this one isn't bad Crenshaw (http://compilers.iecc.com/crenshaw/). There is also the ability in MASM to mimic a compiler using high level macros, there are a few examples of macros that do this in the MASM32 examples in the distribution package.
Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 07:03:20 AM

You mean something like this: Let My$(123)=MyOther$(ebx) ?
Almost everything is possible with macros. Have a look at the attachment, it's all Pure MasmTM (although it will also assemble with JWasm)

Title: Re: How to make a new programming language with MASM32v10?
Post by: MichaelW on March 26, 2010, 08:01:46 AM
How about a simple BASIC done in MASM:

http://ppanks76.tripod.com/mbi.html?201026

I seem to recall Franck Charlet posting his version on the old forum, but it does not appear to be in the archive.

Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 09:07:32 AM
Quote from: MichaelW on March 26, 2010, 08:01:46 AM
How about a simple BASIC done in MASM:

http://ppanks76.tripod.com/mbi.html?201026

I seem to recall Franck Charlet posting his version on the old forum, but it does not appear to be in the archive.


Michael,

I found one version with source code on my hard disk - I hope I don't violate any copyrights if I post it here. The MiniBas.asm is slightly adapted to make it run on Masm32 v10. Extract to some folder, go there, assemble & run, then use
Load castle
Run

to see what it can do:
MINI-BASIC ASCII Art

|---| |---| |---|                 |---| |---| |---|
|   ---   ---   |                 |   ---   ---   |
\              /                  \               /
|            |                    |             |
|            | |---| |---| |---|  |             |
|            |--   ---   ---    --|             |
|                                               |
|                                               |
|                                               |
|             /------------------\              |
|             |[][][][][][][][][]|              |
|             |[][][][][][][][][]|              |
|             |[][][][][][][][][]|              |
|             |[][][][][][][][][]|              |
|             |[][][][][][][][][]|              |
-------------------------------------------------
Castle by dunric


MiniBasic is a nice demo, but what you get is a crippled programming language. That's why I chose to do it the other way round: MasmBasic (http://www.masm32.com/board/index.php?topic=12460) is not a programming language but rather a library with 70+ commands that looks like Basic and feels like Basic but has in addition everything, virtually everything that Windows (32-bit) can offer - because you never left assembler. Call it an "inline Basic"...
Title: Re: How to make a new programming language with MASM32v10?
Post by: MichaelW on March 26, 2010, 10:04:56 AM
Franck Charlet's MBasic is a great deal less "crippled".

Version 1.8 is supposed to be available here:

http://pagesperso-orange.fr/franck.charlet/misc.html

But the download was not working when I tried.

Version 1.0 is here:

http://www.exmortis.narod.ru/src_inter_eng.html
Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 10:22:45 AM
Quote from: MichaelW on March 26, 2010, 10:04:56 AM
Franck Charlet's MBasic is a great deal less "crippled".

Yes, I found it in the meantime, and it is indeed less "crippled". Especially the math functions are quite impressive. But it is still "only" a BASIC language, not assembler... MasmBasic is both ;-)

Attached the download with some tests of mine. The original version does not assemble, there is an odd error maybe deliberately introduced to keep noobs at large...

EDIT: More tests added. For an interpreter, MBasic is indeed very fast.
Title: Re: How to make a new programming language with MASM32v10?
Post by: FritzGamaliel on March 26, 2010, 10:48:39 AM
The first thing I want to know is:
how to make the code below:
invoke add,ADDR number1,ADDR number2,ADDR result   ==========>MASM32v10' grammar (invoke' keyword followed by functionName)

same with:
number1 + number2 = result   ==========> my' grammar

Thank you
Fritz

Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 11:01:58 AM
Quote from: FritzGamaliel on March 26, 2010, 10:48:39 AM
number1 + number2 = result   ==========> my' grammar

Not possible with macros. You need at least one initial keyword, e.g.
Let number1 + number2 = result
Title: Re: How to make a new programming language with MASM32v10?
Post by: dedndave on March 26, 2010, 11:55:40 AM
        mov     eax,number1
        add     eax,number2
        mov     result,eax


:P
Title: Re: How to make a new programming language with MASM32v10?
Post by: qWord on March 26, 2010, 12:57:23 PM
Quote from: jj2007 on March 26, 2010, 11:01:58 AMYou need at least one initial keyword
by choosing a keyword that is nearly not visible you can do such stuff  :toothy
_ macro expr:=< >
    pos1 INSTR 1,<&expr>,<=>
    pos2 INSTR 1,<&expr>,<+>
    pos3 INSTR 1,<&expr>,<->
    pos4 INSTR 1,<&expr>,<*>
    pos5 INSTR 1,<&expr>,</>
    IF pos1 EQ 0 OR (pos2 EQ 0 AND pos3 EQ 0 AND pos4 EQ 0 AND pos5 EQ 0)
        .err <invalid expression>
        EXITM
    ENDIF
    pos2 = pos2 OR pos3 OR pos4 OR pos5   ;-)
    larg SUBSTR <&expr>,1,pos2-1
    rarg SUBSTR <&expr>,pos2+1,pos1-pos2-1
    res  SUBSTR <&expr>,pos1+1
    IF pos2 NE 0
        mov eax,larg
        add eax,rarg
        mov res,eax
    ELSEIF pos3 NE 0
        mov eax,larg
        sub eax,rarg
        mov res,eax
    ELSEIF pos4 NE 0
        mov eax,larg
        mul rarg
        mov res,eax
    ELSEIF pos5 NE 0       
        mov eax,larg
        xor edx,edx
        div rarg
        mov res,eax
    ENDIF   
endm


_ edx+ecx = ebx
Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 02:01:09 PM
Quote from: qWord on March 26, 2010, 12:57:23 PM
by choosing a keyword that is nearly not visible you can do such stuff  :toothy

You cheat :green

include \masm32\include\masm32rt.inc

Calc MACRO args
LOCAL posplus, posequ, dest, sources, src
  posequ INSTR <args>, <=>
  dest SUBSTR <args>, posequ+1
  sources SUBSTR <args>, 1, posequ-1
  posplus INSTR sources, <+>
  if posplus
src SUBSTR sources, 1, posplus-1
sources SUBSTR sources, posplus+1
push src
While posplus
posplus INSTR sources, <+>
if posplus
src SUBSTR sources, 1, posplus-1
sources SUBSTR sources, posplus+1
else
src SUBSTR sources, 1
endif
if ((opattr(src)) and 127) eq 48 ;; register?
add [esp], src
else
mov eax, src
add [esp], eax
endif
Endm
  else
push sources
  endif
  pop dest
ENDM

.data
result dd 0
number1 dd 111
number2 dd 222
number3 dd 333

.code
start:
Calc number1 + number2 + number3 + number3 = result
; Calc number1 = result ; works, too
print "The result is "
inkey str$(result)
exit

end start
Title: Re: How to make a new programming language with MASM32v10?
Post by: qWord on March 26, 2010, 02:20:24 PM
writing macros is cheating  :green

BTW: the stack usage is an nice idea
Title: Re: How to make a new programming language with MASM32v10?
Post by: jj2007 on March 26, 2010, 03:17:50 PM
Quote from: qWord on March 26, 2010, 02:20:24 PM
writing macros is cheating  :green
Nooooo... :red

Quote
BTW: the stack usage is an nice idea

Thanks :bg
That's how I do the offsets in the for loop:
QuoteFor_ n=0 To eax-1
      dec eax ; no problem
   Next
Title: Re: How to make a new programming language with MASM32v10?
Post by: FritzGamaliel on March 28, 2010, 06:47:06 AM
But we still use FileName.asm
Can we use new file' type
Example:
FileName.myType

Then, we create our grammar?

Thank you
Fritz.
Title: Re: How to make a new programming language with MASM32v10?
Post by: TmX on March 28, 2010, 06:52:27 AM
Quote from: FritzGamaliel on March 28, 2010, 06:47:06 AM
But we still use FileName.asm
Can we use new file' type
Example:
FileName.myType

doesn't really matter, anyway
a compiler/interpreter should be able the parse the content of the file, regardless of its extension

Quote from: FritzGamaliel on March 28, 2010, 06:47:06 AM
Then, we create our grammar?

take a look at The Dragon Book (http://dragonbook.stanford.edu/)
it's the de facto reference in writing compilers
Title: Re: How to make a new programming language with MASM32v10?
Post by: donkey on March 28, 2010, 06:58:01 AM
Quote from: FritzGamaliel on March 28, 2010, 06:47:06 AM
But we still use FileName.asm
Can we use new file' type
Example:
FileName.myType

Then, we create our grammar?

Thank you
Fritz.

No, it doesn't make any difference just modify your build command.

ML.EXE /c /coff /Cp /I"C:\masm32\Include" "SomeSource.MOE"
Title: Re: How to make a new programming language with MASM32v10?
Post by: hutch-- on March 28, 2010, 02:06:14 PM
Fritz,

To address your basic question, there are two choices, use the MACRO system in MASM and within those limits of notation, design you own macro/language system. The other choice is to write either and assembler or compiler in MASM where you have absolute control of the notation but at the price of having to do it all from scratch. The latter is a very advanced task usually done by teams of programmers with a lot of experience being funded by a large corporation.
Title: Re: How to make a new programming language with MASM32v10?
Post by: FritzGamaliel on March 29, 2010, 04:43:19 AM
Hutch,
Now, I can create accounting application in MASM32v10 with MicrosoftAccess2003 as it's database.
Then, I dis-assemble it exe file with   MASM32v10'qeditor
Then, I dis-assemble ml.exe of MASM32v10 with MASM32v10'qeditor
I compared it result..
To understand how to create a compiler easily, can I use my method?
But, it still hard for me to understand how to create a compiler...Do you have other advice?

Thank you
Fritz
Title: Re: How to make a new programming language with MASM32v10?
Post by: donkey on March 29, 2010, 06:43:29 AM
Fritz,

Creating a compiler is an extremely complex task. You will have to know a lot about binary trees and hashing to build and search symbol tables, you will need to completely understand the x86 instruction set, you will have to have at least a very good knowledge of tokenization and parsing as well as about 1000 other things. It is not for a novice or even an intermediate programmer, it falls squarely in the advanced family of coding. I would take Hutch's advice and build a macro library that will meet your needs, it may not be exactly what you want but MASMs macro facilities are without equal in the assembly world and I'll bet you can come close. Doing it through macros passes off all of the really hard stuff to MASM and leaves you only to work out the syntax and logic. If you really want to go the compiler route, I posted a link in my first post in this thread that is a pretty good primer for compiler writing:

http://compilers.iecc.com/crenshaw/
Title: Re: How to make a new programming language with MASM32v10?
Post by: sinsi on March 29, 2010, 06:49:20 AM
QuoteMASMs macro facilities are without equal in the assembly world
I would argue that fasm's macro language is a lot more advanced (even though I don't use them) :bg
Title: Re: How to make a new programming language with MASM32v10?
Post by: tenkey on March 29, 2010, 07:55:15 AM
There are many ways to build a compiler. They all require some basics:

1) the task of analyzing the written code to discover what the programmer has asked the computer to do,
2) the task of building tables of symbols, to hold names and properties.
3) the task of creating the new code, which is a translation of what was written.

The difficulty depends on the demands of the language you create.

How much performance do you want?
It is easy to produce correct code which is many times slower than what other compilers, or a competent assembly language programmer can generate. The simplest compilers will also create redundant code. Creating faster and nonredundant code requires more complex algorithms. Some algorithms are very advanced and require knowledge of compiler theory.

What kinds of data do you want to provide?
Most users will want numbers with fractional values. Read up on floating-point arithmetic to handle them. In addition to numbers, you will probably want to provide arrays.  The nonresizable array is simple to implement, and is sufficient for many applications. Most users will also want to have strings. Strings, like the ones in Basic, PHP, or .NET, require runtime memory management. If you want objects, you are getting fancy. Objects require a complex compiler table structure to disambiguate names and track what is valid and visible to other code. Modern objects require runtime memory management that is more complex than the management needed for strings-only.

What is your target code?
Assuming the final code will be an "unmanaged" executable, you can generate MASM code, and let the assembler handle the task of generating a binary file. Most compiler writers would want to directly create at least a COFF (OBJ) file that is usable by the linker. In addition to handling the forward reference problem, you will need to create the relocation and linking information required by the linker. Or you can attempt to create the final PE-COFF (EXE) file directly. In addition to the binary translation, you will need to supply the PE header information required by the OS loader.

How much error checking do you want to do?
The simpler the language, the fewer error messages you need to provide. But you must provide meaningful error messages for "compiler doesn't understand", "compiler does not allow", and "no more room (overflow)" events.

How much debugging help do you want to provide?
As a beginning compiler writer, you probably will want to avoid this until you have built one or two working compilers. It's something to put on the list of what users might expect.
Title: Re: How to make a new programming language with MASM32v10?
Post by: hutch-- on March 30, 2010, 12:22:41 PM
Fritz,

The answer to your question is simply NO, a compiler/assembler is some powers more com0pliczated than you think and if you read the postings here by some very experienced people you will get some idea of just how complicated they are. Just parsing the code that you design the language for is a sophisticated task and there are many more things you would have to do to succeed, a pre-processor is another language again, then you have opcode generation and with a bare assembler you then have to do some jump langth optimisation to compact the code. A compiler that performs wider ranging internal optimisation is a power more complicated again.

You will do OK by learning the MACRO engine in MASM and writing a set of custom macros and while the MASM macro system is a bit long in the tooth and piggy here and there it is reasonably powerful if you can master it properly and if you combine it with your own library of procedures you come close to a custom language.
Title: Re: How to make a new programming language with MASM32v10?
Post by: qWord on March 30, 2010, 01:32:30 PM
Fritz,
if you decide to use macros, you will find an good starting point in MASM Programmers Guide , Chapter 9 (http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ProgrammersGuide/Chap_09.htm) . For examples how creating/using macros take a look in \masm32\macros\macros.asm.
There is nearly nothing that you can't do with macros, as long as there is a will  :bg