flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Flat C

Author
Thread Post new topic Reply to topic
bazizmix



Joined: 15 Jan 2016
Posts: 44
bazizmix
This is the next step from FlatC-- (https://board.flatassembler.net/topic.php?t=19700)
and FlatC- (https://board.flatassembler.net/topic.php?t=20153).
Flat assembler used as backend.

Practically identical to standard C.
Additions:
1) Directive #debug - output debug in x64dbg format (dd32 or dd64)
2) Directive #gui - compiling in windows gui format (default is console format)
3) Directive #dll - compiling in DLL format
4) Directive #elf - format ELF for Linux
5) The asm operator may have a list of used variables -
asm (var1, var2, ...) {...} if variables are not used anywhere else.
See in test.c example the strlen() function.
Command line:
Usage: fc.exe [/ 64] [/ debug] <source>
/ 64 generate 64-bit program
/ debug generate debug info for x64dbg
(by default - generate 32-bit program)
Look results from tst.bat and tst64.bat


Description: Flat ะก v.0.022
Download
Filename: FlatC.7z
Filesize: 375.47 KB
Downloaded: 433 Time(s)

Post 26 Nov 2018, 14:19
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
Wow ! Output asm code from c code ! Super-druper-amazing !
SSE\AVX support ?
Post 20 Jan 2019, 14:14
View user's profile Send private message Reply with quote
bazizmix



Joined: 15 Jan 2016
Posts: 44
bazizmix
Only SSE for floating point expressions:
addss,addsd,subss,subsd,mulss,mulsd,divss,divsd,pxor,ucomiss,ucomisd,comiss,comisd,
cvtsi2ss,cvtsi2sd,cvtss2sd,cvttss2si,cvtsd2ss,cvttsd2si,movss,movsd .
All other instructions can be included via asm operator
Post 20 Jan 2019, 15:08
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
I mean this:
_mm_shuffle_ps like in c++

And support this includes:
<xmmintrin.h> SSE
<emmintrin.h> SSE2
<pmmintrin.h> SSE3
<tmmintrin.h> SSSE3
<smmintrin.h> SSE4.1
<nmmintrin.h> SSE4.2


Last edited by Roman on 20 Jan 2019, 21:40; edited 1 time in total
Post 20 Jan 2019, 15:35
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
What is the code size limit ?
Post 20 Jan 2019, 21:39
View user's profile Send private message Reply with quote
bazizmix



Joined: 15 Jan 2016
Posts: 44
bazizmix
Quote:

And support this includes:
<xmmintrin.h> SSE
<emmintrin.h> SSE2
<pmmintrin.h> SSE3
<tmmintrin.h> SSSE3
<smmintrin.h> SSE4.1
<nmmintrin.h> SSE4.2

No
Quote:

What is the code size limit ?

I suppose - limit is the same as in fasm1
Post 21 Jan 2019, 05:44
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
I found error.
float x = 55.0f + CamX;
float z = 55.0f - CamZ;
x /= 6.327272f; miss in asm code !
x = x / 6.327272f; miss in asm code !
z /= 6.327272f; miss in asm code !
z = z / 6.327272f; miss in asm code !

This code correct in asm code.
float x = (55.0f + CamX) / 6.327272f;
float z = (55.0f - CamZ) / 6.327272f;


C flat get asm code:
comiss xmm1,dword [xmm4] Fasm error ! Must be comiss xmm1,xmm4 !
Post 19 Jul 2019, 02:04
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
C flat get asm code from my example:
comiss xmm0,dword[xmm6] Fasm error ! Must be comiss xmm0,xmm6 !
Code:
// Simple windows gui program
#debug
#gui
#include "kernel32.h"
#include "user32.h"
#include "msvcrt.h"
char *msg = "Hi! I'm the example "
#ifdef _WIN64
"64"
#else
"32"
#endif
"-bit program!";
float floorf (float wx) {
      asm{  roundss xmm1,[wx],9
            movss  [wx],xmm1  }
        return wx;  }
//float lerpA(float a,float b,float t){ return a-(a*t)+(b*t); }
float Lerp(float a,float b,float t){ return (b*t); }
typedef struct vec4 {
    float x;
    float y;
    float z;
    float w;
} vec4;
void main(){
    float fir = 4;
    vec4  dif = {11,2,3,5};
    dif.x += fir;
    dif.y += fir;
    dif.z += fir;
    dif.w += fir;
    float height;
    float LvlSize = 110.0f;
    float CamX = 50.0f;
    float CamZ = 50.0f;
    float x = (55.0f + CamX) / 6.327272f;
    float z = (55.0f - CamZ) / 6.327272f;

    float col = floorf(x);
    float row = floorf(z);

    float A = 1.0f;
    float B = 2.0f;
    float C = 3.0f;
    float D = 4.0f;

    float dx = x - col;
    float dz = z - row;

    if(dz < 1.0f - dx)
     { float uy = B - A;
       float vy = C - A;
       height = A + Lerp(0.0f, uy, dx) + Lerp(0.0f, vy, dz);
      }
    else {
       float uy = C - D;
       float vy = B - D;
       height = D + Lerp(0.0f,uy,1.0f - dx) + Lerp(0.0f, vy, 1.0f - dz);
       }
    char rrt[100];
    char *tname = "result: %1.4f";
    sprintf(rrt,tname,height);
    MessageBox(HWND_DESKTOP,rrt,MB_OK,MB_OK);
    ExitProcess(0);
}
    
Post 19 Jul 2019, 02:10
View user's profile Send private message Reply with quote
bazizmix



Joined: 15 Jan 2016
Posts: 44
bazizmix
Sorry for the delay, Roman. Here is a new version with bug fixes. Thanks for the feedback.


Description:
Download
Filename: FlatC_v024.7z
Filesize: 39.84 KB
Downloaded: 125 Time(s)

Post 23 Oct 2019, 06:56
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 557
Roman
Thanks.
Post 16 Nov 2019, 07:51
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4226
Location: 2018
edfed
hello, is there any ".zip" with all ready to go package? (fc.exe, fasm.dll, includes, ...)

or at least, where to find the windows.h needed in the example from http://abreojosensamblador.epizy.com/?Tarea=5&SubTarea=5&Lang=1&nivel=0&padre=0&accion=0

i am curious to see if it can be compiled with flatC
Post 09 Dec 2019, 13:28
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 37
Location: Germany
rc
Hello,

i just wanted to test flatc as i thought it might be a really awesome teaching tool for beginners as they can see how simple c code would look like in fasm.

So i have written a really simple program in c to test flatc:
Code:
int foo = 12;

int main()
{
    return foo;
}
    


But there are 2 strange things happening.
At first i post the exact output of the flatc compiler:
Code:
format PE  
include 'include\win32ax.inc'
entry main
section '.code' code readable executable

main:
        push    ebp
        mov     ebp,esp
.l0:
        mov     eax,dword[foo]
        mov     esp,ebp
        pop     ebp
        ret     0
section '.data' data readable writeable

__d_1   dq      1.000000

__f_1   dd      1.000000



section '.reloc' fixups data readable discardable
if $=$$
dd 0,8
end if
    


First strange thing is, that "foo" is not created in the "data" section and therefore doesn't exist in main.
Code:
mov     eax,dword[foo]    


Second strange thing is the error i get from fasm:
Code:
Symbol foo not used    


It is the exact opposite: foo is not defined but used.

To make the code work i just defined foo in the "data" section.

But my question is, is this a bug in flatc for not creating foo and also is this a bug in fasm for giving the wrong error message?
Or am i missing something?

Thanks!


[EDIT]

Ok this misleading error message:
Code:
Symbol foo not used    
is comming from flatc:
Code:
FlatC v.0.22 (fasm v.1.73)
fasm error#2 (minimal.asm:10)
Symbol foo not used
    


When i try to compile the produced asm file with fasm directly i get:
Code:
undefined symbol 'foo'    


So the bug is probably from flatc.
Post 07 Feb 2020, 15:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17063
Location: In your JS exploiting you and your system
revolution
Also flatc isn't properly terminating the code. Using a simple 'ret' only works if you have no other threads running. For maximal robustness ideally it would call ExitProcess.

And, yes, this is a real problem even if you don't use threading because some libraries that you might link to do initiate other threads.
Post 07 Feb 2020, 16:18
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 37
Location: Germany
rc
Ah OK, good to know. Thanks for pointing that out.
Post 07 Feb 2020, 16:46
View user's profile Send private message Reply with quote
rc



Joined: 03 Nov 2019
Posts: 37
Location: Germany
rc
Does somebody know if bazizmix is still working on flatc, or if not, if he would like to share the source code? I think this is a great project and has potential for others that are already familiar with hll programming and want to learn fasm/assembly.

And another question is: what is this part for? What is it doing?
Code:
section '.reloc' fixups data readable discardable
if $=$$
dd 0,8
end if 
    
Post 08 Feb 2020, 09:00
View user's profile Send private message Reply with quote
bazizmix



Joined: 15 Jan 2016
Posts: 44
bazizmix
Thank you for your interest in my compiler, rc.
I regret that I do not immediately answer, but I rarely visit the forum, and I do not receive alerts about new messages.
I fixed the error.
"The devil is hiding in the details" Sad


Description:
Download
Filename: FlatC_v025.7z
Filesize: 39.83 KB
Downloaded: 55 Time(s)

Post 18 Feb 2020, 07:30
View user's profile Send private message Reply with quote
rc



Joined: 03 Nov 2019
Posts: 37
Location: Germany
rc
bazizmix wrote:
Thank you for your interest in my compiler, rc.
I regret that I do not immediately answer, but I rarely visit the forum, and I do not receive alerts about new messages.
I fixed the error.
"The devil is hiding in the details" Sad


Oh, i totally missed your answer. Thanks for the new update.
Nice you still working on it Smile. Please keep it up Smile
Post 25 Mar 2020, 20:37
View user's profile Send private message Reply with quote
rc



Joined: 03 Nov 2019
Posts: 37
Location: Germany
rc
I wanted to test flatc_v025 and i came across a new strange behavior/maybe bug.

My file structure is as follows:

+ project folder
---- msvcrt.h (comming with flatc)
---- array.h (simple dynamic array implementation of mine)
---- stddef.h (standard definitions by me bc they are not comming with flatc)
---- test.c (my test file)


Content of the files:

msvcrt.h (comming with flatc)

array.h: (code is probably not important for the error except the line #include "stddef.h")
Code:
#ifndef _ARRAY_H_
#define _ARRAY_H_

#include "stddef.h"  // including my stddef.h, this causes the strange error


typedef struct Array
{
        int *array;
        size_t used;
        size_t size;
} Array;

void arrayInit(Array *a, size_t initialSize)
{
        a->array = (int *)malloc(initialSize * sizeof(int));
        a->used = 0;
        a->size = initialSize;
}

void arrayInsert(Array *a, int element)
{
        if(a->used == a->size)
        {
                a->size *= 2;
                a->array = (int *)realloc(a->array, a->size * sizeof(int));
        }
}

void arrayFree(Array *a)
{
        free(a->array);
        a->array = NULL;
        a->used = a->size = 0;
}

#endif    


stddef.h
Code:
#ifndef _STDDEF_H
#define _STDDEF_H

#define NULL (void*)0
#define NUL 0x00

#endif    


test.c
Code:
#include "msvcrt.h"
#include "array.h"

int main()
{
        return 0;
}    


When i run flatc to compile it, i get the following output: (and btw i renamed fc.exe to flatc.exe because on windows the command fc is a build in command for file compare)
Code:
>flatc test.c
FlatC v.0.25 (fasm v.1.73)
(msvcrt.h,71): void srand(unsigned int);
error:maybe you accidently add the ;    


Two things that are strange here:
1. msvcrt.h has no line 71 (it ends on line 70) (also the mentioned forward declaration void srand(unsigned int); is on line 69)
2. when i put the content of stddef.h directly into array.h instead of including stddef.h there, then it works like a charm.

So, i changed array.h from this:
Code:
#ifndef _ARRAY_H_
#define _ARRAY_H_

#include "stddef.h" // <---


typedef struct Array
{

...

#endif    

to this:
Code:
#ifndef _ARRAY_H_
#define _ARRAY_H_

#define NULL (void*)0 // <---
#define NUL 0x00 // <---

typedef struct Array
{

...

#endif    

And it works perfectly.

I guess there is a bug in the process of parsing/lexing the preprocessor commands
Would be nice if you could fix this.
If you need the project for debugging, i can send it to you if you like.

Also shipping a "stddef.h" with flatc would be very handy. With all the standard definitions like NULL, NUL True, False, EOF etc.

And a little suggestion:
You ship a gdidemo.c where you define NULL 0.
This is slightly incorrect. As NULL in standard c does not refer to the integer constant 0. So NULL == 0 is false.
However, i don't know how you manage things internally in your flatc compiler. Just wanted to mention this. Smile
Post 26 Mar 2020, 10:24
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 © 1999-2020, Tomasz Grysztar.

Powered by rwasa.