flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > Befunge Interpreter

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 26 Aug 2009, 13:34
Lately I've been interested in befunge, and made a simple interpreter for in in FASM.

This was originally just a side-note from a topic i posted in the Heap section - Esoteric Programming Challenges! and a new topic here was created mainly because MHajduk suggested i post it here, and the topic was being lead off-topic in talks about improvement for the interpreter, instead of the challenges.



List of updates:
Code:
10-10-09: Major update: Complete rewrite, is now 100% bug free as far as I know. The core is separated from OS-specific stuff.
08-09-09: Bug fix: The "." command now prints a space after the number it pops off the stack. Thanks to windwakr for finding out that the specifications say to do this.
          Bug fix: Revolution noticed the bounds checking of g and p instructions wasn't quite right and provided a fix.
07-09-09: Bug fix: The fungespace is now stored in 32 bit cells instead of 8 bit. This means values > 255 can now be stored on the fungespace.
06-09-09: Update : A small help message is now displayed if the executable is run with no paramaters.
          Bug fix: Division by zero now works as per the language specifications.
          Bug fix: Stack overflow now shows an error and quits the script instead of crashing.
          Bug fix: Greater than instruction didnt work correctly with negative values. Thanks to windwakr for helping find this.
          Bug fix: modulus and division of negative numbers. Found and fixed by windwakr.
31-08-09: Bug fix: ? instruction wasn't really random and didn't go up at all.
26-08-09: Initial release.    



Known bugs:
No known bugs currently! Please test and find some for me to fix! Smile



Planned updates (No idea when I will do these, others are welcome to do so and provide the code):

  • MHajduk and I have come up with the idea of creating standalone interpreters for scripts. At the moment, the idea is to allow for a /c switch to compile (fake compile at least) a standalone interpreter by copying the interpreter and appending a script to it. This could easily allow for context menu integration to .bf and .bf files for something like "Create Standalone Interpreter" to automate the process completely. Edit: after the rewrite, core.asm will need to be updated if this is to happen. I doubt that I will ever do this.




Comments, constructive criticism and discussion about the befunge language and this interpreter are all welcome.


Description:
Download
Filename: Befunge.zip
Filesize: 62.39 KB
Downloaded: 1190 Time(s)



Last edited by Pinecone_ on 09 Oct 2009, 15:45; edited 60 times in total
Post 26 Aug 2009, 13:34
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 26 Aug 2009, 13:50
(No more discussions about this interpreter should be continued in the Befunge Programming Challenge thread now. Please use this thread instead.)

From the previous thread:

MHajduk wrote:
Pinecone_ wrote:
Something else that's been on my mind... add a check for /c switch (for compile) so i can call befunge.exe /c script.bf script.exe and it will copy itself and the script to script.exe.
It seems to be the best solution so far.

You could also consider another way of compilation of Befunge programs: because your interpreter is open source and you attach FASM compiler to your distribution, you can just add desired Befunge program code to the interpreter source and compile it with FASM to obtain "properly" build exe file (Befunge executable).


Yes, but then nothing could be compiled without the whole package. With the /c compile option, all that would be needed is the interpreter itself, which would be better for people who are interested in befunge, but not FASM.
Post 26 Aug 2009, 13:50
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 29 Aug 2009, 03:32
I imagine that you can do two compiles: one for the befunge.exe interpreter proper and another (using "file" and "virtual") to append the script to the end of the .EXE. But that's just my guess.
Post 29 Aug 2009, 03:32
View user's profile Send private message Visit poster's website Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 01 Sep 2009, 13:38
That would work, but the process could not be automated entirely by doing that.
Post 01 Sep 2009, 13:38
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 05 Sep 2009, 16:58
All you have to do for the negative divide(AND modulus!) is change 3 instructions.
In "InstructionHandlers.asm" in the "Proc Instruction_Division" Change:
Code:
xor edx,edx
div ecx    
to:
Code:
cdq
idiv ecx
    


In "Proc Instruction_Modulus" all you need to do is change "xor edx,edx" to "cdq"






Pinecone_ wrote:

When loading a script, it should allocate 25 lines of space even when there is less than 25 lines in the file, but it only allocates the minimum number of lines required to load the file, therefore a # on the bottom line of the file when executed when the execution direction is downwards, it will skip the next command on the top line of the file when it's not supposed to.


It seems to work right if you change "Mov [ScriptLines], ecx" to "Mov [ScriptLines], 25" in "Proc ParseFile uses Esi Edi", But it probably breaks something somewhere. Also, where is "ScriptSize" used? I don't see it referenced anywhere, yet you set it up.

_________________
----> * <---- My star, won HERE
Post 05 Sep 2009, 16:58
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 05 Sep 2009, 18:18
Thanks a heap windwakr. It's now updated.

Yay I learned a new instruction Razz cdq.
Post 05 Sep 2009, 18:18
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 06 Sep 2009, 01:42
Fixed a bug where negative values did not work right with the greater than command (`).

Previously the following happened:
Code:
<v5p009
         <v" is greater than 2"0
 >1-::.2`|>:#,_$00g1-:00p!#@_91+,
         <^" is lower than or equal to 2"0    
produced
Code:
4 is greater than 2
3 is greater than 2
2 is lower than or equal to 2
1 is lower than or equal to 2
0 is lower than or equal to 2
-1 is greater than 2
-2 is greater than 2
-3 is greater than 2
-4 is greater than 2    


First post has been updated with new version, which has been fixed. Thanks to windwakr for making me check this.

If you have the source and can't be bothered re-downloading, in InstructionHandlers.asm, look for "Proc Instruction_GreaterThan" and change
Code:
Jbe @F    
to
Code:
Jle @F    
Post 06 Sep 2009, 01:42
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 06 Sep 2009, 07:45
I've done a few more updates:

  • Stack overflows show an error instead of causing it to crash.
  • Division by zero now asks the user for what they want (strange, but thats what it's supposed to do according to the language specifications...)
  • A help message if the executable is run with no paramaters.


All avaliable for download in the first post.
Post 06 Sep 2009, 07:45
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 07 Sep 2009, 07:14
Another update.

Previously the entire fungespace was stored as 8 bit cells. It didn't occur to me until today that the p and g instructions should be able to store/get much greater values from the fungespace because the stack is 32 bit. The fungespace is now stored in 32 bit cells, so that wont be a problem anymore (even though we never noticed it before).

Chess still doesn't work...
Post 07 Sep 2009, 07:14
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 07 Sep 2009, 16:56
Yet another update!! Razz Sorry everyone.

revolution wrote:
I assume you want to use jle and jge for proper bounds checking?
Code:
 Proc GetInstruction, X, Y
           
            Cmp [X], -1
         Jl .OOB
             Cmp [Y], -1
         Jl .OOB
             
            Cmp [X], SCRIPT_COLUMNS
             Jg .OOB
             Mov Eax, [ScriptLines]
              Cmp [Y], Eax
                Jg .OOB

...

      Proc SetInstruction, X, Y, NewInstruction
           
            Cmp [X], -1
         Jl .Return
          Cmp [Y], -1
         Jl .Return
          
            Cmp [X], SCRIPT_COLUMNS
             Jg .Return
          Mov Eax, [ScriptLines]
              Cmp [Y], Eax
                Jg .Return    


Thanks revolution. Updated now and available for download in the first post..
Post 07 Sep 2009, 16:56
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 07 Sep 2009, 18:14
Are you sure you uploaded the right version? If you did indeed fix it at 2:56A.M. Sept. 8(in your time) like you say, then why does the edit date on the files read Sept. 7 and Sept. 6? And I can't find the fix that revolution suggested in the files.

_________________
----> * <---- My star, won HERE
Post 07 Sep 2009, 18:14
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 07 Sep 2009, 21:37
Thank you windwakr. The right one has been uploaded this now. (I don't know how that happened.....)
Post 07 Sep 2009, 21:37
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 07 Sep 2009, 23:22
There's an error in your interpreter....

The . command will pop a value off the stack and output it as a decimal integer, followed by a space





Original post below, ignore it. I didn't do any research before posting it, but it did lead me to discover this bug:

The chess code obviously relies on some sort of exploit in the official interpreter.
The second line prints "8 |R|....." in the official one(Note the space.), but in yours it prints "8|R|". If you trace through it yourself you see no reason why there is a space printed in the official one after the number.

Look at the part of code responsible for the "8 |". It enters from the top right going left.

v9.
>"|",

It shows the number, and puts 9 and "|" on the stack, then prints the "|". NO SPACE IN THERE! So why does the official one insert a space?

Befunge-93 Interpreter/Debugger v2.21
+-+-+-+-+-+-+-+-+
8 |R|N|B|Q|K|B|N|R|
7 |P|P|P|P|P|P|P|P|
6 |.|.|.|.|.|.|.|.|
5 |.|.|.|.|.|.|.|.|
4 |.|.|.|.|.|.|.|.|
3 |.|.|.|.|.|.|.|.|
2 |p|p|p|p|p|p|p|p|
1 |r|n|b|q|k|b|n|r|
+-+-+-+-+-+-+-+-+
A B C D E F G H

Who knows what other bugs the chess game uses.



When I make a mistake, I don't kohlrak my posts....nope. I just make it small and acknowledge that I was wrong.

_________________
----> * <---- My star, won HERE
Post 07 Sep 2009, 23:22
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 08 Sep 2009, 10:08
Thanks windwakr. Updated version in first post now prints a space after "." command.
Post 08 Sep 2009, 10:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 08 Sep 2009, 10:26
Pinecone_ wrote:
Thanks windwakr. Updated version in first post now prints a space after "." command.
This will affect the temperature converter codes. They all relied upon the previous "bug" of having no space. Wink
windwakr wrote:
When I make a mistake, I don't kohlrak my posts....nope. I just make it small and acknowledge that I was wrong.
Yeah, very right, pobody's nerfect.
Post 08 Sep 2009, 10:26
View user's profile Send private message Visit poster's website Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 10 Sep 2009, 02:09
windwakr wrote:
When I make a mistake, I don't kohlrak my posts....nope. I just make it small and acknowledge that I was wrong.
What do you mean by "kohlrak" there?
Post 10 Sep 2009, 02:09
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 10 Sep 2009, 02:11
kohlrak is a member here who got mad and deleted all his posts. So when I say I didn't "kohlrak" my post, I'm using his name as a verb meaning deleting the post.

He seems to have grown up(A LITTLE) and has started posting again.

I could have also use "mikegonta" or "asmcoder" in place of kohlrak. Mikegonta and asmcoder both also deleted their posts.

_________________
----> * <---- My star, won HERE
Post 10 Sep 2009, 02:11
View user's profile Send private message Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 10 Sep 2009, 15:14
I noticed asmcoder did that. It seems so stupid and makes it really annoying when one of his posts turn up in a search and people haven't quoted what he said that they're referring to, so you have no idea what they're talking about...

What could possibly have happened to make them take so much time to delete everything?
Post 10 Sep 2009, 15:14
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 10 Sep 2009, 15:17
Well, asmcoder was just an asshole, and I didn't help the situation much. Mikegonta, who knows why he did it. And I think kohlrak did it because he got pissed off.
Post 10 Sep 2009, 15:17
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 10 Sep 2009, 23:37
To be fair, I'll blindly guess that kohlrak and asmcoder are just young and immature (no offense intended by that) and can't handle criticism very well. We all remember how awkward it is to be that young. Sure, maybe? it could've been handled better, but that's life, mistakes are made, and nobody's (!) perfect.

Some people just easily annoy other people, and I'm one of 'em. Cool Fortunately, it's only (mostly?) offline people (e.g. my bro), heh. Razz
Post 10 Sep 2009, 23:37
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:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.