flat assembler
Message board for the users of flat assembler.

Index > Heap > Concept: How to write your_language2FASM compiler

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
This is very simple concept. Use a scripting language with powerful regexp and strings manipulation to translate your HLL code to FASM.

Example:

My compiler written in Tcl:
Code:
proc read_file f { read [open $f r] }

proc main {} {
       puts "Source code?"
       gets stdin source_code

  puts "Processing..."
      set contents [read_file $source_code]
       set fd [open output.asm w]
  
    puts $fd "include '%fasminc%\\win32ax.inc'"
   
    foreach line [split $contents \n] {
           set line [string map {"Function main" "main:"} $line]
             set line [string map {"End main" ".end main"} $line]
          set line [string map {"End If" ".endif"} $line]
               set line [string map {"If" ".if" "Then" ""} $line]
                if [regexp {(\s+)([a-zA-z]+)\((.*)\)} $line -> tab func params] {
                      set line "$tab invoke $func, $params"
             }
              puts $fd $line
      }
      
    close $fd
   puts "Done!"
      puts "Output: output.asm"
}

main
    


My basicASM test code:
test.basm
Code:
Function main
    MessageBox(0, "Do you like FASM?", "Test", MB_YESNO)
    If EAX = IDYES Then
         MessageBox(0, "Good boy! Smile", "Test", MB_OK)
    End If
      ExitProcess(0)
End main
    


The compiled example (output.asm):
Code:
include '%fasminc%\win32ax.inc'
main:
       invoke MessageBox, 0, "Do you like FASM?", "Test", MB_YESNO
    .if EAX = IDYES 
             invoke MessageBox, 0, "Good boy! Smile", "Test", MB_OK
    .endif
       invoke ExitProcess, 0
.end main
    


Assembled code:
Code:
File: output.exe
Path: \Desktop\
Size: 1.536
Type: PE
Base Of Code: 00001000, Code Size: 00000200
Base Of Data: 00001000, Data Size: 00000400
Image Base: 00400000
Entry Point: 00401000

Sections:

   Name   | VirtAddr | VirtSize |  Offset  | PhysSize |  Flags   | Code
----------+----------+----------+----------+----------+----------+------
 .flat    | 00401000 | 0000005E | 00000200 | 00000200 | E0000060 |  Yes
 .idata   | 00402000 | 00000092 | 00000400 | 00000200 | C0000040 |  

Section .flat

* Entry Point:

00401000: 6A04                 push 00000004
00401002: E805000000           call 0040100C
00401007: 54                   push esp
00401008: 657374               jnb 0040107F
0040100B: 00E8                 add al, ch
0040100D: 1200                 adc al, [eax]
0040100F: 0000                 add [eax], al
00401011: 44                   inc esp
00401012: 6F                   outsd 
00401013: 20796F               and [ecx+6F], bh
00401016: 7520                 jne 00401038
00401018: 6C                   insb 
00401019: 696B6520464153       imul ebp, dword ptr [ebx+65], 53414620
00401020: 4D                   dec ebp
00401021: 3F                   aas 
00401022: 006A00               add [edx], ch
00401025: FF157C204000         call USER32.MessageBoxA
0040102B: 83F806               cmp eax, 00000006
0040102E: 7526                 jne 00401056
00401030: 6A00                 push 00000000
00401032: E805000000           call 0040103C
00401037: 54                   push esp

* Jump:
 00401016(C) 

00401038: 657374               jnb 004010AF
0040103B: 00E8                 add al, ch
0040103D: 0D00000047           or eax, 47000000
00401042: 6F                   outsd 
00401043: 6F                   outsd 
00401044: 6420626F             and fs:[edx+6F], ah
00401048: 7921                 jns 0040106B
0040104A: 203A                 and [edx], bh
0040104C: 2900                 sub [eax], eax
0040104E: 6A00                 push 00000000
00401050: FF157C204000         call USER32.MessageBoxA

* Jump:
 0040102E(C) 

00401056: 6A00                 push 00000000
00401058: FF155E204000         call KERNEL32.ExitProcess
    


See the levels?
HLL -> Macro assembler -> pure assembly -> machine code

Of course this a very simple/stupid example. But it's easy to evolve into a complex compiler that targets FASM. Then use FASM to build the executable.
Post 21 Jun 2009, 19:10
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:50; edited 1 time in total
Post 21 Jun 2009, 23:09
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
asmcoder wrote:
better make self-learning AI. it will use much less memory, will be alot easier to do, and if you do it correctly efficient with 100% (no mistakes).
yeah it's so easy the world is full of Terminators Laughing

less memory, are you joking me? Do you know how many neurons you will need for that? lmao.

_________________
Previously known as The_Grey_Beast
Post 21 Jun 2009, 23:51
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:50; edited 1 time in total
Post 22 Jun 2009, 00:01
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6034
Location: Poland
MHajduk
OzzY, I think it would be cool if you write C/C++ compiler which could accept FASM as an inline assembler. Wink
Post 22 Jun 2009, 06:53
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
It should be better to start writing the code in fasm in the first place Wink

I think it is a good idea, if I already have some HLL code I would like to have a compiler to fasm. It is way much better than compiling to binary and then passing a disassembler.

asmcoder, did you realize that Ozzy said your_language2FASM, i.e., a SINGLE HLL language converted to fasm? It is out of scope writing a single tool that will interpret any HLL language correctly (publicly available and customized ones).
Post 22 Jun 2009, 15:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.