The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ToutEnMasm on October 11, 2007, 03:06:51 PM

Title: Algebraic expression
Post by: ToutEnMasm on October 11, 2007, 03:06:51 PM
Hello,
I am searching something that can translate Algebraic expression into source code.
Does this thing exist ?
Title: Re: Algebraic expression
Post by: realcr on October 12, 2007, 06:30:24 PM
Hey ToutEnMasm.

You are infact looking for information about compiler construction.
I recommend you to read this article :
http://compilers.iecc.com/crenshaw/
It might not be masm32 ,however I think it was one of the best article of this kind.

good luck,
realcr.
Title: Re: Algebraic expression
Post by: ToutEnMasm on October 19, 2007, 01:09:00 PM
Hello,
Thanks for answer.
I see that my question isn't very good.I will try a sample in pseudo code.
Quote
.data
X DWORD 5
Y REAL 4.0
z dword 0
Formula db "z=X² + Y²",0
That's not very difficult to do it with the FPU (just do it ...).What I am searching is something that can translate this in masm code.This thing seems possible,no?

Title: Re: Algebraic expression
Post by: Rockoon on October 19, 2007, 01:26:03 PM
Convert your infix expression to RPN .. after that it damn near codes itself.

Title: Re: Algebraic expression
Post by: ToutEnMasm on October 20, 2007, 07:07:40 AM
Hello,
Thanks for answer.
I see somethings easy to do using re-entrance ( a prog that call itself) and tempory data in stack.
Perhaps if I have sometimes i do it myself.
Title: Re: Algebraic expression
Post by: jack on October 20, 2007, 09:20:05 PM
I looked for the same thing, does anybody know of an algorithm to minimize stack usage in RPN ?
let's say you translate an algebraic expression to postfix (RPN), how would you rewrite it so as to use the minimum of stack registers ?
Title: Re: Algebraic expression
Post by: tenkey on October 21, 2007, 04:21:52 PM
For minimizing the parser stack usage, if you don't have function calls (for example, sin(x) or exp(x)), then you can use "operator precedence".
Title: Re: Algebraic expression
Post by: Rockoon on October 21, 2007, 05:15:41 PM
Quote from: tenkey on October 21, 2007, 04:21:52 PM
For minimizing the parser stack usage, if you don't have function calls (for example, sin(x) or exp(x)), then you can use "operator precedence".

RPN doesnt have "operator precedence" .. its one of the main points of using RPN

RPN expressions are ALWAYS evaluated left to right

The "worst case" for an RPN expression as far as stack usage is when all the terms precede all the operators ..

Examples:

a b c d e + + + +

which has a stack usage depth of 5

a b + c + d + e +

which has a stack usage depth of 2



Both expressions perform the exact same calculation .. as far as automatically performing such minimization .. not sure what a good technique would be

Something to consider however is that something like:

a b + c d + e + +

which while it has a stack usage depth of 3, has greater parallelism than the others so should lend itself well to generating code with better instruction level parallelism

edited to add:

It should be noted that "stack" is just an abstraction which can easily be implimented as register management (for instance ST0 = eax, ST1 = ebx, ST2 = ecx, ...)  hence the desire to minimize "stack" usage could mean minimizing register usage which means minimizing auxilary storage outside of registers (such as on the real processor stack)
Title: Re: Algebraic expression
Post by: tenkey on October 23, 2007, 07:28:52 AM
RPN doesn't use operator precedence, but a parser that converts from infix to RPN can. It wasn't clear at first read what was to be rewritten - the parser or the RPN evaluator.
Title: Re: Algebraic expression
Post by: Rockoon on October 24, 2007, 10:12:49 AM
For register/stack minimization, there is the Sethi-Ullman algorithm