flat assembler
Message board for the users of flat assembler.

flat assembler > Projects and Ideas > the worse x86 emitter ever seen

Author
Thread Post new topic Reply to topic
pber



Joined: 23 Jul 2017
Posts: 4
And it`s all mine!
Ladies and gentelemen, the source:

Code:
c=1 while c < 10000000 do c = c + 1 end c

and its fasm translation (the funny part) :

Code:
push ebx push ecx enter 4,0 ;--------------- push const push dword 1 ;--------------- setlocal c pop eax mov dword [ebp - 4], eax LOOP_START_1: ;--------------- mov_reg mov eax, [ebp - 4] ;--------------- mov_reg mov ecx, 10000000 cmp eax, ecx jnl LOOP_END_1 ;--------------- mov_reg mov eax, [ebp - 4] ;--------------- mov_reg mov ecx, 1 ;--------------- addict add eax, ecx push eax ;--------------- setlocal c pop eax mov dword [ebp - 4], eax jmp LOOP_START_1 LOOP_END_1: ;--------------- mov_reg mov eax, [ebp - 4] leave pop ecx pop ebx ret

Then, the output:

Code:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 02BC7F38 53 51 C8 04 00 00 68 01 00 00 00 58 89 45 FC 8B 02BC7F48 45 FC B9 80 96 98 00 39 C8 7D 11 8B 45 FC B9 01 02BC7F58 00 00 00 01 C8 50 58 89 45 FC EB E3 8B 45 FC C9 02BC7F68 59 5B C3 -- -- -- -- -- -- -- -- -- -- -- -- -- 10000000 ---- time: 0.062

The timing includes:

+ loading of a modified (and super heavy) Lua 5.3
+ Lua`s loading of my AL (the asbtract language)
+ ...and the newest x86 emitter
+ obviously: the translation of the AST into fasm assembler
+ sending of the assembler source to FASM.DLL (linked into Lua)
+ the receiving of authentic and powerful machine code (...a myth!)
+ the minimal work WinXP had to do to make me happy!

I`m going to implement the JIT compiler
for an OO lang. It must allow me to recompile
methods and classes without stopping the whole system,
as happen in Smalltalk.

Hi all,
and hi Tomasz: thanks for your great work!

cheers
Paolo
Post 26 Jul 2017, 19:18
View user's profile Send private message Reply with quote
guignol



Joined: 06 Dec 2008
Posts: 267
It's like at some point in life you actually start hatin' Roman languages
Post 27 Jul 2017, 07:35
View user's profile Send private message Reply with quote
pber



Joined: 23 Jul 2017
Posts: 4
I studied Roman: its similar to CommonLisp.
Post 27 Jul 2017, 08:27
View user's profile Send private message Reply with quote
pber



Joined: 23 Jul 2017
Posts: 4
...the second try does not use FASM.DLL but OLLY.DLL.

Now I can control each single opcode and re-implement my mini-assembler in Lua.
This version includes also some peep-hole optimizations.

The source:
Code:
fmt = "<< %s %d >>\n" a = 0 b = 3 c = 1 a = 123 * b s = "Hello world" while c<=4 do printf(fmt, s, c) c=c+1 end

The dump:
Code:
01 [2AD1860]. 53 push ebx ; 02 [2AD1861]. 51 push ecx ; 03 [2AD1862]. C8 28 00 00 enter 28, 0 ; 04 [2AD1866]. C7 C0 00 00 00 00 mov eax, 00h ; 05 [2AD186C]. C7 45 FC 98 18 AD 02 mov dword [ebp - 04h], 02AD1898h ; !glob.str #1 06 [2AD1873]. C7 45 F8 A4 18 AD 02 mov dword [ebp - 08h], 02AD18A4h ; !glob.str #2 07 [2AD187A]. C7 C0 7B 00 00 00 mov eax, 07Bh ; 08 [2AD1880]. 89 45 F4 mov [ebp - 0Ch], eax ; >a 09 [2AD1883]. 50 push eax ; 10 [2AD1884]. 8B 45 F8 mov eax, [ebp - 08h] ; s> 11 [2AD1887]. 50 push eax ; 12 [2AD1888]. 8B 45 FC mov eax, [ebp - 04h] ; fmt> 13 [2AD188B]. 50 push eax ; 14 [2AD188C]. E8 D9 FF 16 75 call 077C4186Ah ; call: printf 15 [2AD1891]. 58 pop eax ; 16 [2AD1892]. 58 pop eax ; 17 [2AD1893]. 58 pop eax ; 18 [2AD1894]. C9 leave ; 19 [2AD1895]. 59 pop ecx ; 20 [2AD1896]. 5B pop ebx ; 21 [2AD1897]. C3 ret ; 23 [2AD1898]. global_001: 3C 3C 20 25 73 20 25 64 20 3E 3E 00 db '<< %s %d >>' ; 25 [2AD18A4]. global_002: 48 65 6C 6C 6F 20 77 6F 72 6C 64 00 db 'Hello world' ;

The happiness:
Code:
<< Hello world 1 >>\n<< Hello world 2 >>\n<< Hello world 3 >>\n<< Hello world 4 >>\n>> 5

Ah ah...if it was C a back-slash plus one "n" could had been made a LF

bye bye
paolo
Post 28 Jul 2017, 20:49
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 cannot attach files in this forum
You can download files in this forum


Copyright © 2004-2018, Tomasz Grysztar.

Powered by rwasa.