flat assembler
Message board for the users of flat assembler.

Index > Main > Converting arithmetic expressions to Assembly

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
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.
Post 11 Feb 2018, 14:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20631
Location: In your JS exploiting you and your system
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.
Post 11 Feb 2018, 18:20
View user's profile Send private message Visit poster's website Reply with quote
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.
Post 11 Feb 2018, 19:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20631
Location: In your JS exploiting you and your system
revolution 12 Feb 2018, 04:21
Not having JS is a choice I made.
Post 12 Feb 2018, 04:21
View user's profile Send private message Visit poster's website Reply with quote
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?
Post 12 Feb 2018, 11:05
View user's profile Send private message Reply with quote
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.
Post 12 Feb 2018, 19:41
View user's profile Send private message Reply with quote
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"
Post 12 Feb 2018, 20:09
View user's profile Send private message Reply with quote
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.

I must admit I don't know what you are talking about.
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.
Post 13 Feb 2018, 08:08
View user's profile Send private message Reply with quote
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.
Post 15 Feb 2018, 20:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20631
Location: In your JS exploiting you and your system
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.
Post 16 Feb 2018, 02:45
View user's profile Send private message Visit poster's website Reply with quote
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.
Post 17 Feb 2018, 20:11
View user's profile Send private message Reply with quote
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.
Post 28 Jun 2018, 11:19
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 28 Jun 2018, 20:53
Ok. I will try to break it again. Laughing
Btw, when really parsing, the approach of "splicing parenthesis in" doesnt seem like the best way to go.
Post 28 Jun 2018, 20:53
View user's profile Send private message Reply with quote
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.
Post 11 Jul 2018, 16:19
View user's profile Send private message Reply with quote
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".
Post 09 Nov 2018, 18:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20631
Location: In your JS exploiting you and your system
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.
Post 09 Nov 2018, 19:10
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



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

Use FABS against your base

or

BTR dword [your base's MSB],31
Post 10 Nov 2018, 13:46
View user's profile Send private message Visit poster's website Reply with quote
Furs



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

Use FABS against your base

or

BTR dword [your base's MSB],31
xor [your base's MSB], 0x80 should be better Wink (btw in your case shouldn't it be the entire base, not the MSB? a byte doesn't have 32 bits)
Post 10 Nov 2018, 21:02
View user's profile Send private message Reply with quote
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.
Post 11 Nov 2018, 04:57
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2625
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    
Post 11 Nov 2018, 10:42
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.