flat assembler
Message board for the users of flat assembler.

 Index > Main > Converting arithmetic expressions to Assembly Goto page 1, 2  Next
Author
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 11 Feb 2018, 14:05
Hey, guys!
I am trying to impress my informatics teacher by making a web-app that converts arithmetic expressions to the i486-compatible assembly. You can check my work here:
Arithmetic expressions to assembly converter
So, does it produce correct results? I've tried to test it, and to me it looks like it does, but I'll ask some experts on those things.
11 Feb 2018, 14:05
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20203
revolution 11 Feb 2018, 18:20
I wanted to try this, but without JS it does nothing for me. However when examining the source the code looks good, indented and commented.
11 Feb 2018, 18:20
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 11 Feb 2018, 19:27
Well, yes, I've done everything in JavaScript (the webapp doesn't communicate with the server at all). Which browser are you using? I've tested it in Internet Explorer 6 and it works.
11 Feb 2018, 19:27
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20203
revolution 12 Feb 2018, 04:21
Not having JS is a choice I made.
12 Feb 2018, 04:21
tthsqe

Joined: 20 May 2009
Posts: 767
tthsqe 12 Feb 2018, 11:05
Overflows the stack:
Code:
`a+(b+(c+(d+(e+(f+(g+(h+(i+(j)))))))))    `

Reads past depth of stack (TBH, the AST is not right):
Code:
```a+-b
a*-b    ```

In general, I would say the parser has a problem with unary minus, and "a^b" can't be used in place of "pow(a,b)"
If you can solve the problem of unary minus, would you like to contribute to my project?
12 Feb 2018, 11:05
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 12 Feb 2018, 19:41
Quote:
Overflows the stack

Thanks for pointing that out! I think I fixed it, there was an error in my implementation of the DFS algorithm (which is attempting to avoid the stack overflow by compiling the "deepest" parts of the expression first). Could you test it again, please?
Quote:
a+-b

What's that supposed to do? As far as I know, it should issue a syntax error (and I haven't tried to implement that yet). Do you think it should parse the same as "a+(-b)"? That would be quite tough to implement.
Quote:
If you can solve the problem of unary minus, would you like to contribute to my project?

Sorry, my knowledge of C and C++ is minimal.
12 Feb 2018, 19:41
tthsqe

Joined: 20 May 2009
Posts: 767
tthsqe 12 Feb 2018, 20:09
Good, now it does the inner first. However, it is impossible to avoid spill code. Some expressions simply need more than 8 registers. Consider a sum of 256 terms with the parenthesis placed so that the ast is a complete binary tree.

Any school girl the age of at least 7 knows that 2*-3 is -6 and not a "syntax error"
12 Feb 2018, 20:09
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 13 Feb 2018, 08:08
Quote:
Consider a sum of 256 terms with the parenthesis placed so that the ast is a complete binary tree.

Quote:
Any school girl the age of at least 7 knows that 2*-3 is -6 and not a "syntax error"

Anyway, I've modified the tokenizer to tokenize the expressions such as "2*-3" as "2*(-3)". It does so by "splicing" those parentheses into the array. I don't know if that's possible in C++ though.
13 Feb 2018, 08:08
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 15 Feb 2018, 20:49
revolution wrote:
Not having JS is a choice I made.

Well, the code in that app is just 600 lines of JavaScript, so you can see for yourself that it's not a spyware, if you don't want to run it because you think it might be. Better yet, use a browser that doesn't do JIT compilation of JavaScript (such as the Internet Explorer 6), so that it's impossible to do the Meltdown or the Spectre attack. The less JavaScript a browser supports, the harder it is to make a spyware using the JavaScript it provides.
15 Feb 2018, 20:49
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20203
revolution 16 Feb 2018, 02:45
UniverseIsASimulation wrote:
revolution wrote:
Not having JS is a choice I made.

Well, the code in that app is just 600 lines of JavaScript, so you can see for yourself that it's not a spyware, if you don't want to run it because you think it might be. Better yet, use a browser that doesn't do JIT compilation of JavaScript (such as the Internet Explorer 6), so that it's impossible to do the Meltdown or the Spectre attack. The less JavaScript a browser supports, the harder it is to make a spyware using the JavaScript it provides.
It is not you that I doubt at all. But there are so many other actors in the game, and we have to trust every one of them even when we don't know who they are.

Anyhow, this off-topic might be best left to that other thread.
16 Feb 2018, 02:45
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 17 Feb 2018, 20:11
Does Tomasz mind me naming my website after Flat Assembler? Sorry I didn't ask before. It was supposed to be a metaphore. Like, if you write in assembly, you mustn't be sufficed with "surface" explanations, and I am supposed to give people a "deeper" perspective on some things. The domain assembler.000webhostapp.com was already taken, so I've chosen the domain name flatassembler.000webhostapp.com.
17 Feb 2018, 20:11
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 28 Jun 2018, 11:19
Anyway, the web-app has been improved in many ways since I last posted here. I've implemented the error-handling (hopefully all syntax errors are covered and reacted to with an useful error message) and I've implemented some simple syntax-highlighting algorithm for assembly.
28 Jun 2018, 11:19
tthsqe

Joined: 20 May 2009
Posts: 767
tthsqe 28 Jun 2018, 20:53
Ok. I will try to break it again.
Btw, when really parsing, the approach of "splicing parenthesis in" doesnt seem like the best way to go.
28 Jun 2018, 20:53
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 11 Jul 2018, 16:19
Did you find some error? It's hard to believe there are none.
11 Jul 2018, 16:19
UniverseIsASimulation

Joined: 23 Sep 2016
Posts: 34
UniverseIsASimulation 09 Nov 2018, 18:37
Nevertheless, I've found some error. Create a program using that web-app that calculates "pow(x,2)". Assemble it and set the variable 'x' to be '-1'. It will output 'NaN', despite the fact that pow(-1,2)=1. The algorithm my web-app uses only works for x>0.
Is there a simple way to calculate "pow(x,y)" in Assembly if the 'x' is negative? It doesn't have to deal with complex numbers (such as the result of "pow(-1,1/2)").

BTW, if you don't trust me that my JavaScript code is not malicious, download it and work offline. Then it can't "phone home".
09 Nov 2018, 18:37
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20203
revolution 09 Nov 2018, 19:10
If x is negative, then just make it positive and do the normal power function. Then, if y is an even integer, you are done. If y is an odd integer then return the negative of the result. If y is not an integer the return an error.
09 Nov 2018, 19:10
fasmnewbie

Joined: 01 Mar 2011
Posts: 555
fasmnewbie 10 Nov 2018, 13:46
Universe

or

10 Nov 2018, 13:46
Furs

Joined: 04 Mar 2016
Posts: 2466
Furs 10 Nov 2018, 21:02
fasmnewbie wrote:
Universe

or

xor [your base's MSB], 0x80 should be better (btw in your case shouldn't it be the entire base, not the MSB? a byte doesn't have 32 bits)
10 Nov 2018, 21:02
fasmnewbie

Joined: 01 Mar 2011
Posts: 555
fasmnewbie 11 Nov 2018, 04:57
Furs, XOR is one of the way as well. But it is not descriptive of the problem being discussed here, that it resetting the sign bit.

When talking about MSB, I refer to the highest portion byte be it in Real8 or Real 4. See the keyword dword there? I wasn't being specific because it I don't know about the size of his float, but still even in Real8, you need to take out the DWORD portion. DWORD for float 4, DWORD+4 for QWORD. In whatever portion, the MSB holds the sign bit.
11 Nov 2018, 04:57
Furs

Joined: 04 Mar 2016
Posts: 2466
Furs 11 Nov 2018, 10:42
I mean MSB = most significant byte. In your case it should be "most significant DWORD".

If you access the most significant byte (which is the float + 3, or double + 7) as a DWORD you'll access 3 bytes beyond the float and switch the sign bit of 3 bytes beyond the float. Crude diagram:
Code:
```  Float  (bytes in memory, so little endian)
/     \
* * * *
^     ^
LSB   MSB (sign bit is here)

You access this:

Float  (bytes in memory, so little endian)
/     \
* * *[* ? ? ?]
^
You flip this byte's sign bit    ```
11 Nov 2018, 10:42
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum