flat assembler
Message board for the users of flat assembler.

Index > Linux > brainfuck compiler

Author
Thread Post new topic Reply to topic
klavs.pr



Joined: 20 Apr 2010
Posts: 20
Location: Latvia
klavs.pr
Hello.
I am writing brainfuck compiler in high level language. It converts brainfuck source into fasm source file. But I have some problems in fasm part.
1) (problem due to lack of experience) How should I allocate memory (30000 cells/bytes)? It seems that I must allocate it using libc or should I use some kind of syscall? I have read that linux does not have memory allocation mechanism built in - that is why I am confused.
2) How can I read single char from console/terminal? Syscall #3 is not very useful, it does not stop after reading a char.
Post 26 Mar 2011, 15:36
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
1. Linux kernel doesn't have heap manager (common memory allocation mechanism for applications) built in. Instead, it expects every application to link to libc. Best way is to link your asm source with libc and use libc for I/O.

2. Again, use libc if you can. You will save yourself many problems.
Post 26 Mar 2011, 18:16
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
klavs.pr



Joined: 20 Apr 2010
Posts: 20
Location: Latvia
klavs.pr
So... I have to use malloc() and free() to manage memory. But another question - can I use getchar() or fgetc(FILE*) is more suitable for asm? It is written, that in c stdin is already initialized (when program starts), so I guess stdin is not initialized in asm and I can't use getchar() directly. Or can I?
Post 26 Mar 2011, 19:17
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Your program definitively should invoke libc startup code to initialize stdin. However I have to admit I don't know how dynamic linking works in Linux (I assume you are linking to glibc dynamically).
Post 26 Mar 2011, 21:39
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
klavs.pr



Joined: 20 Apr 2010
Posts: 20
Location: Latvia
klavs.pr
Ok, thanks vid.
I did it just like libcdemo example does it.(i guess it is not really dinamic linking)
Now i have memory allocated and freed, and getchar() works without any manual initialization. But the problem remains - this function does not read single char AND then stop reading.
Post 27 Mar 2011, 06:08
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
If you *really* only want to read char from stdin, you could use handle I/O instead of stream I/O (read() from handle 1, instead of fgetc() from FILE* stdin). But I would suggest to rather look into proper linux libc usage than workarounding problems.
Post 27 Mar 2011, 11:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
You could link your program with crtbegin.o and crtend.o. These can be found in /usr/lib/gcc/?/?/.
Post 27 Mar 2011, 16:19
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Dumb answer, but hey, what'd you expect from me? Razz

Code:
db 30000 dup(0)
    


Or preferably even in .bss or wherever.
Post 28 Apr 2011, 02:17
View user's profile Send private message Visit poster's website 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.