Author Topic: MASM32 for Ubuntu Linux operating system?  (Read 25809 times)

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: MASM32 for Ubuntu Linux operating system?
« Reply #15 on: September 16, 2013, 04:05:48 AM »
Hi MichaelW,

Any chance to get a preview of the gcc backend?

Sorry, I didn’t reply at the time because I somehow missed your post. The gcc backend has been more or less operable and part of the distribution for several years IIRC. With the version 0.90.0 release in June, the developer who currently seems to be doing most of the compiler work stated that:
Quote
The C backend has become stable, with many bugs fixed

http://www.freebasic.net/forum/viewtopic.php?f=1&t=21321

And the 0.90.0 release was followed with 0.90.1 less than a month later to fix a few problems.

http://www.freebasic.net/forum/viewtopic.php?f=1&t=21382

Here is one of the examples included with the distribution, procedures.bas:
Code: [Select]
function AddNumbers( a as integer, b as integer ) as integer
return a + b
end function

sub hello( )
print "hello"
end sub

declare sub myprint( num as integer )

'' Code outside any procedures is the main part of the program
hello( )
print AddNumbers( 1, 1 )
myprint 5

sub myprint( num as integer )
print num
end sub

And the assembly output when compiled with 0.90.1 as a console app with the default -gen gas:
Code: [Select]
.intel_syntax noprefix

#procedures.bas' compilation started at 12:30:02 (FreeBASIC 0.90.1)

.section .text
.balign 16

.globl _ADDNUMBERS@8
_ADDNUMBERS@8:
push ebp
mov ebp, esp
sub esp, 4
mov dword ptr [ebp-4], 0
.Lt_0004:
mov eax, dword ptr [ebp+12]
add eax, dword ptr [ebp+8]
mov dword ptr [ebp-4], eax
.Lt_0005:
mov eax, dword ptr [ebp-4]
mov esp, ebp
pop ebp
ret 8
.balign 16

.globl _HELLO@0
_HELLO@0:
.Lt_0006:
push 1
push 5
push offset _Lt_0008
call _fb_StrAllocTempDescZEx@8
push eax
push 0
call _fb_PrintString@12
.Lt_0007:
ret
.balign 16

.globl _MYPRINT@4
_MYPRINT@4:
push ebp
mov ebp, esp
.Lt_0009:
push 1
push dword ptr [ebp+8]
push 0
call _fb_PrintInt@12
.Lt_000A:
mov esp, ebp
pop ebp
ret 4
.balign 16

.globl _main
_main:
push ebp
mov ebp, esp
and esp, 0xFFFFFFF0
sub esp, 4
mov dword ptr [ebp-4], 0
call ___main
push 0
push dword ptr [ebp+12]
push dword ptr [ebp+8]
call _fb_Init@12
.Lt_0002:
call _HELLO@0
push 1
push 1
push 1
call _ADDNUMBERS@8
push eax
push 0
call _fb_PrintInt@12
push 5
call _MYPRINT@4
.Lt_0003:
push 0
call _fb_End@4
mov eax, dword ptr [ebp-4]
mov esp, ebp
pop ebp
ret
#procedures.bas' compilation took 0.002902044813286153 secs

.section .data
.balign 4
_Lt_0008: .ascii "hello\0"

And the C output when compiled with 0.90.1 as a console app with -gen gcc:
Code: [Select]
// Compilation of procedures.bas started at 12:57:54 on 09-15-2013

typedef signed char byte;
typedef unsigned char ubyte;
typedef unsigned short ushort;
typedef signed int integer;
typedef unsigned int uinteger;
typedef unsigned long ulong;
typedef signed long long longint;
typedef unsigned long long ulongint;
typedef float single;
typedef struct _string { char *data; int len; int size; } string;
void __stdcall fb_PrintInt( integer, integer, integer );
void __stdcall fb_PrintString( integer, string*, integer );
string* __stdcall fb_StrAllocTempDescZEx( ubyte*, integer );
void __stdcall fb_Init( integer, ubyte**, integer );
void __stdcall fb_End( integer );
integer __stdcall ADDNUMBERS( integer, integer );
void __stdcall HELLO( void );
void __stdcall MYPRINT( integer );

integer __stdcall ADDNUMBERS( integer A$1, integer B$1 )
{
integer fb$result$1;
__builtin_memset( &fb$result$1, 0, 4 );
label$2:;
fb$result$1 = A$1 + B$1;
goto label$3;
label$3:;
return fb$result$1;
}

void __stdcall HELLO( void )
{
label$4:;
string* vr$3 = fb_StrAllocTempDescZEx( (ubyte*)(ubyte*)"hello", 5 );
fb_PrintString( 0, vr$3, 1 );
label$5:;
}

void __stdcall MYPRINT( integer NUM$1 )
{
label$6:;
fb_PrintInt( 0, NUM$1, 1 );
label$7:;
}

integer main( integer __FB_ARGC__$0, ubyte** __FB_ARGV__$0 )
{
integer fb$result$0;
__builtin_memset( &fb$result$0, 0, 4 );
fb_Init( __FB_ARGC__$0, __FB_ARGV__$0, 0 );
label$0:;
HELLO(  );
integer vr$5 = ADDNUMBERS( 1, 1 );
fb_PrintInt( 0, vr$5, 1 );
MYPRINT( 5 );
label$1:;
fb_End( 0 );
return fb$result$0;
}

// Total compilation time: 0.00207652090124455 seconds.

I recall the original developer of FreeBASIC and the gcc backend stating that the C compiler was being used as a sort of assembler. IIRC the emitted C code at that time was very different than the code above, and at least for me hard to follow, and, although I did not test this, looked like something that a compiler, tuned to optimize "normal" C code, would not be able to optimize well. But as stated in the FreeBASIC documentation, “the goal of the C emitter (when complete) will be to make it possible to port FreeBASIC-compiled programs (and fbc itself) to other platforms.”
Well Microsoft, here’s another nice mess you’ve gotten us into.