The MASM Forum

Miscellaneous => Miscellaneous Projects => Topic started by: UniverseIsASimulation on February 24, 2018, 06:50:31 AM

Title: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on February 24, 2018, 06:50:31 AM
Hey, guys!
New here. I've just made a simple web-app that attempts to convert arithmetic expressions to i486-compatible assembly:
Arithmetic expression to assembly converter (http://flatassembler.000webhostapp.com/compiler.html)
I've tried to test it (using Flat Assembler), and to me it seems like it produces correct results. However, I am not a professional programmer, so I would like to hear from some experts.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on February 24, 2018, 07:23:33 AM
Very cute :t What's behind, shunting yard?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: Mikl__ on February 24, 2018, 09:51:46 AM
This is a useful thing! Please write something else for converting integer arithmetic expressions... (Comments in Slovenian?)
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on February 24, 2018, 04:53:48 PM
Quote
What's behind, shunting yard?
There is no codebehind. It's just a few recursive algorithms implemented in 600 lines of JavaScript, you can see all by looking at the source code. Shunting Yard would be faster, but it would also be harder to implement. The difference isn't really visible on arithmetic expressions, which are always relatively short.
Quote
This is a useful thing!
Yes. And C compilers are even more useful, for the same reason.
Quote
Please write something else for converting integer arithmetic expressions
That wouldn't be easy. The algorithms I've made assume the existence of a stack (like the FPU-stack). I could make a stack of the general-purpose registers (eax,ebx,ecx,edx,edi,esi...), but that would be extremely inefficient. I could make a stack in RAM, but then it would be much like using a C compiler (it would be very hard to reassemble a program compiled for MacOS to run in DOS).
Quote
Comments in Slovenian?
Close enough. They are in Croatian. Google Translate usually does a good job translating from Croatian to English.

Anyway, can you confirm that my web-app produces the correct code? Again, I've only tested it a little in Flat Assembler.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: hutch-- on February 24, 2018, 07:54:58 PM
Sounds like an interesting tool, I would not worry about what its written in as it produces code, not runs it. I note you mentioned .486 compatible instructions and if you are catering for very old OS versions and hardware it would make sense but if you are addressing anything later in either 32 or 64 bit software, there may be better choices in instructions.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: hutch-- on February 25, 2018, 06:13:30 PM
Sad to say the link is broken.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on February 25, 2018, 09:48:30 PM
It's not. My website sleeps from 22 to 23 PM UTC+1. If I want to make it accessible 24h/day, I'd have to pay for the hosting, and now it's free.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: hutch-- on February 26, 2018, 03:24:59 AM
How big is the download ? If it is under 512k, post it here.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on February 26, 2018, 04:40:25 AM
It's huge, see attachment 8)
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on February 26, 2018, 06:16:52 AM
It's a JavaScript file (around 600 lines) intended to be embedded on a web-page (like one I linked to). I am not sure how I could post it here.
I think the webpage is up right now and that it will be up for the next 2 hours. Then it will be down for an hour, and then it will be up for 23 hours.
Maybe you can download it and post it here, if you think that's a good idea.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on February 26, 2018, 08:16:59 AM
Download the zip file I attached above, and just double-click on the only file in the archive. Astonishingly, it produces fine assembler code. I have not tested it thoroughly, but this is obviously a fine project, compliments.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: LiaoMi on February 26, 2018, 08:41:17 AM
Hello everybody,

I decided to collect here some alternative solutions that we can use in our projects, some of them can generate high-performance code  :P

C++ Mathematical Expression Library - uses mpir and mpfr

(http://www.partow.net/images/exprtk_uml_diagram.png)

Code: [Select]
The ExprTk library has the following capabilities:
Mathematical operators (+, -, *, /, %, ^)
Functions (min, max, avg, sum, abs, ceil, floor, round, roundn, exp, log, log10, logn, pow, root, sqrt, clamp, inrange, swap)
Trigonometry (sin, cos, tan, acos, asin, atan, atan2, cosh, cot, csc, sec, sinh, tanh, d2r, r2d, d2g, g2d, hyp)
Equalities & Inequalities (=, ==, <>, !=, <, <=, >, >=)
Assignment (:=, +=, -=, *=, /=, %=)
Logical operators (and, nand, nor, not, or, xor, xnor, mand, mor)
Control structures (if-then-else, ternary conditional, switch case, return-statement)
Loop structures (while loop, for loop, repeat until loop, break, continue)
Optimization of expressions (constant folding, strength reduction, operator coupling, special functions and dead code elimination)
String operations (equalities, inequalities, logical operators, concatenation and sub-ranges)
Expression local variables, vectors and strings
User defined variables, vectors, strings, constants and function support
Multivariate function composition
Multiple sequence point and sub expression support
Numeric integration and differentiation
Vector Processing: BLAS-L1 (axpy, axpby, axpb), all/any-true/false, count, rotate-left/right, shift-left/right, sort, nth_element, iota, sum, kahan-sum, dot-product, copy
File-IO package (routines include: open, close, read, write, getline, support for binary and text modes)
Support for various numeric types (float, double, long double, MPFR/GMP)
Single header implementation, no building required. No external dependencies.
Completely portable (Compiled and executed upon: x86 x86-64, ARMv7/8, POWER6/7 and AVR32)

Example Expressions

Quote
sqrt(1 - (x^2))
clamp(-1,sin(2 * pi * x) + cos(y / 2 * pi),+1)
sin(2 * x)
if (((x + 2) == 3) and ((y + 5) <= 9),1 + w, 2 / z)
inrange(-2,m,+2) == (({-2 <= m} and [m <= +2]) ? 1 : 0)
({1 / 1} * [1 / 2] + (1 / 3)) - {1 / 4} ^ [1 / 5] + (1 / 6) -({1 / 7} + [1 / 8]*(1 / 9))
a * exp(2 * t) + c
z := x + sin(2 * pi / y)
2x + 3y + 4z + 5w == 2 * x + 3 * y + 4 * z + 5 * w
3(x + y) / 2 + 1 == 3 * (x + y) / 2 + 1
(x + y)3 + 1 / 4 == (x + y) * 3 + 1 / 4
(x + y)z + 1 / 2 == (x + y) * z + 1 / 2
(sin(x/pi)cos(2y) + 1)==(sin(x / pi) * cos(2 * y) + 1)
while(x <= 100) { x += 1; }
x <= 'abc123' and (y in ('AStr' + 'ing')) or ('1x2y3z' != z)
('REX' + x like '*123*') or ('a123b' ilike y)

http://www.partow.net/programming/exprtk/index.html (http://www.partow.net/programming/exprtk/index.html)
https://github.com/ArashPartow/exprtk (https://github.com/ArashPartow/exprtk)

Mathematical Expression Parser and JIT Compiler

Code: [Select]
When executed the output of the application would be something like:

[AST-INITIAL]
* [Binary]
  * [Binary]
    - [Unary]
      - [Unary]
        abs [Unary]
          - [Binary]
            * [Binary]
              x
              y
            floor [Unary]
              x
    z
  - [Binary]
    12.900000
    3.000000

[AST-FINAL]
* [Binary]
  * [Binary]
    abs [Unary]
      - [Binary]
        * [Binary]
          x
          y
        floor [Unary]
          x
    z
  9.900000

[ASSEMBLY]
L0:                                 ;                     |                           ..
lea rax, [L2]                       ; 488D05........      | lea pConst, [L2]          ..w
movsd xmm0, [rdx]                   ; F20F1002            | movsd v3, [pVariables]    .r.w
mulsd xmm0, [rdx+8]                 ; F20F594208          | mulsd v3, [pVariables+8]  .r.x
movsd xmm1, [rdx]                   ; F20F100A            | movsd v4, [pVariables]    .r..w
roundsd xmm1, xmm1, 9               ; 660F3A0BC909        | roundsd v4, v4, 9         ....x
subsd xmm0, xmm1                    ; F20F5CC1            | subsd v3, v4              ...xR
xorpd xmm1, xmm1                    ; 660F57C9            | xorpd v5, v5              .... w
subsd xmm1, xmm0                    ; F20F5CC8            | subsd v5, v3              ...r x
maxsd xmm1, xmm0                    ; F20F5FC8            | maxsd v5, v3              ...R x
mulsd xmm1, [rdx+16]                ; F20F594A10          | mulsd v5, [pVariables+16] .R.  x
mulsd xmm1, [rax]                   ; F20F5908            | mulsd v5, [pConst]        . R  x
movsd [rcx], xmm1                   ; F20F1109            | movsd [pResult], v5       R    R
L1:                                 ;                     |
ret                                 ; C3                  |
.align 8
L2:                                 ;                     |
.data CDCCCCCCCCCC2340

RESULT: -1885.514400

https://github.com/kobalicek/mathpresso (https://github.com/kobalicek/mathpresso)

muparserx - Math Parser Library
A C++ Library for Parsing Expressions with Strings, Complex Numbers, Vectors, Matrices and more.


Code: [Select]
Overview
Supported data types: double, integer, complex, boolean, string, array
Extensible with custom operators (binary, infix or postfix)
Extensible with custom functions with an arbitrary number of function arguments
Support for an unlimited number of variables and constants
No limit on expression complexity
Reads binary, hexadecimal, complex, integer and string values from expressions and can be extended to read user defined values as well.
Supports a large variety of predefined operators, functions and constants.
Written in standard compliant C++ code with no external dependencies.
Features
By default the parser supports the following mathematical constants:

The eulerian number with: e = 2.718281828459045235360287
Pi with pi = 3.141592653589793238462643
The imaginary unit with: i = sqrt(-1)
Binary and ternary operators
Standard operators: +, -, *, /, ^
Assignment operators: =, +=, -=, *=, /=
Logical operators: and, or, xor, ==, !=, >, <, <=, >=
Bit manipulation: &, |, <<, >>
String concatenation: //
if then else conditionals with lazy evaluation: ?:
Postfix Operators
Unit postfixes (nano, micro, milli, kilo, giga, mega): n, mu, m, k, G, M
Transpose operator: '
Infix Operators
Sign operator and type conversions: -, (float), (int)
Special Operators
Multidimensional index operator: a[1,2]
Ternary if-then-else operator: (a<b) ? c:d
In-place array creation: {1,2,3}
Predefined Functions
Standard functions abs, sin, cos, tan, sinh, cosh, tanh, ln, log, log10, exp, sqrt
Unlimited number of arguments: min, max, sum
String functions: str2dbl, strlen, toupper
Complex functions: real, imag, conj, arg, norm
Array functions: sizeof, eye, ones, zeros

http://beltoforion.de/article.php?a=muparserx&hl=en (http://beltoforion.de/article.php?a=muparserx&hl=en)
https://github.com/beltoforion/muparserx (https://github.com/beltoforion/muparserx)

Blaze is an open-source, high-performance C++ math library for dense and sparse arithmetic. With its state-of-the-art Smart Expression Template implementation Blaze combines the elegance and ease of use of a domain-specific language with HPC-grade performance, making it one of the most intuitive and fastest C++ math libraries available.

The Blaze library offers ...

... high performance through the integration of BLAS libraries and manually tuned HPC math kernels
... vectorization by SSE, SSE2, SSE3, SSSE3, SSE4, AVX, AVX2, AVX-512, FMA, and SVML
... parallel execution by OpenMP, HPX, C++11 threads and Boost threads
... the intuitive and easy to use API of a domain specific language
... unified arithmetic with dense and sparse vectors and matrices
... thoroughly tested matrix and vector arithmetic
... completely portable, high quality C++ source code

A Complex Example
The following example is much more sophisticated. It shows the implementation of the Conjugate Gradient (CG) algorithm (http://en.wikipedia.org/wiki/Conjugate_gradient) by means of the Blaze library:
(https://bitbucket.org/blaze-lib/blaze/wiki/images/cg.jpg)
In this example it is not important to understand the CG algorithm itself, but to see the advantage of the API of the Blaze library. In the Blaze implementation we will use a sparse matrix/dense vector multiplication for a 2D Poisson equation using NxN unknowns. It becomes apparent that the core of the algorithm is very close to the mathematical formulation and therefore has huge advantages in terms of readability and maintainability, while the performance of the code is close to the expected theoretical peak performance:
Code: [Select]
const size_t NN( N*N );

blaze::CompressedMatrix<double,rowMajor> A( NN, NN );
blaze::DynamicVector<double,columnVector> x( NN, 1.0 ), b( NN, 0.0 ), r( NN ), p( NN ), Ap( NN );
double alpha, beta, delta;

// ... Initializing the sparse matrix A

// Performing the CG algorithm
r = b - A * x;
p = r;
delta = (r,r);

for( size_t iteration=0UL; iteration<iterations; ++iteration )
{
   Ap = A * p;
   alpha = delta / (p,Ap);
   x += alpha * p;
   r -= alpha * Ap;
   beta = (r,r);
   if( std::sqrt( beta ) < 1E-8 ) break;
   p = r + ( beta / delta ) * p;
   delta = beta;
}
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on February 27, 2018, 06:45:59 AM
Wow, that's great! How did you learn C++? I've been trying to learn C++ for five years, and I am still not able to do anything in it. Good that I finally gave up and found out there are languages that are easier to learn, such as JavaScript. I've only been learning JavaScript for a few months, and I am able to do much more than I was able to after learning C++ for five years.

Anyway, my idea for a relatively large project is to make LLAP, a LISP-like Assembly Preprocessor. Namely, many, if not most of the, assembly-language preprocessors are Turing-complete. However, very few of them provide functions that are actually relevant to low-level programming. I think it would be a good idea to make a preprocessor in which it's easy (as it's in LISP) to do complicated string manipulation at the compile-time, and which also has built-in functions for converting arithmetic expressions to assembly (which can be tedious to do manually).
For instance:
Code: [Select]
(echo (compile (parseArth (tokenizeArth "sqrt(a*a+b*b)"))))would output something like:
Code: [Select]
finit
fld dword [a]
fld dword [a]
fmulp st1,st0
fld dword [b]
fld dword [b]
fmulp st1,st0
faddp st1,st0
fsqrt
fstp dword [result]
Do you think that's a good idea? Right now, I don't know enough informatics to do it myself, and I don't have much time to learn that because I am already doing bad at school (and we don't even have informatics at school). If you think that's a good idea, I might do it after a few years.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: Biterider on February 27, 2018, 07:33:58 AM
Hello
Check the <SmplMath> project by qword here in the forum or at SourceForge https://sourceforge.net/projects/smplmath/ (https://sourceforge.net/projects/smplmath/)


Biterider
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on February 27, 2018, 07:40:15 AM
If you want to use your code for pre-processing assembly code, you may need a standalone JS compiler (https://msdn.microsoft.com/en-us/library/7435xtz6%28v=vs.100%29.aspx). Ever thought of that?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: AW on March 01, 2018, 11:08:12 PM
It's not. My website sleeps from 22 to 23 PM UTC+1. If I want to make it accessible 24h/day, I'd have to pay for the hosting, and now it's free.
Extreme poverty, people that can't pay a couple of dollars per month to have something fully functional, deserve my sympathy an I pray for them every night.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: HSE on March 02, 2018, 01:54:06 AM
Internet even exist when I have 18. And in that case, no money to expend in it.   :biggrin:
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: AW on March 02, 2018, 04:22:12 AM
Internet even exist when I have 18. And in that case, no money to expend in it.   :biggrin:
No free lunches, though, except in Salvation Army.  :t
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: markallyn on March 02, 2018, 06:33:08 AM
Hello Universe----,

That is a very cool project, I have to tell you.  I am not familiar with these "S expressions?", so my question may be hopelessly na├»ve and even misguided.  Here it is:

How far can you extend this compiler?  For example, would it be feasible to write code (presumably S expressions) that would solve differential  equations, or even just plain vanilla linear algebra?

Regards,
Mark Allyn
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on March 06, 2018, 07:15:10 AM
Yes, it's possible to solve the equations using the S-expressions. Emacs calculator does that. I am not planning to extend that web-app very far. I am planning to learn a language that's more suitable for those things than JavaScript is and, perhaps a few years from now, rewrite it in that language. The web-app was just a way to test whether the algorithm I've made for converting the arithmetic expressions to assembly was right.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on March 07, 2018, 12:11:31 AM
Does anyone here happen to know the algorithms Emacs calculator uses?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: felipe on March 07, 2018, 02:17:22 AM
Does anyone here happen to know the algorithms Emacs calculator uses?

Aren't the sources of emacs calculator freely available?  :idea:
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: AW on March 07, 2018, 02:54:10 AM
As you may know the only way to convert a High-Level language, whatever it is, already invented or not to machine executable code is through a compiler. A compiler is a sophisticated piece of code, as you will be guessing.
Assembly language (ASM) is machine code in easy readable form. It is not as difficult to take machine code and obtain ASM by using a disassembler because there is almost 1-to-1 correspondence.
What you are trying to achieve is the associated job of a compiler AND a disassembler. Think about it, there are no shortcuts. You have in hands a major project, which can definitly not be made in javascript or in any easy-go-round language for lazy kids to show off.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on March 15, 2018, 06:44:21 AM
Does anyone here happen to know the algorithms Emacs calculator uses?

Aren't the sources of emacs calculator freely available?  :idea:
As I've said, I am not a programmer. Can you break it down for me? Preferably in Croatian and using JavaScript to explain the code-related details. If you can't, then explain it in English and using some language similar to JavaScript (I think I would even be able to understand C++) to explain the code-related details.

So, if I understood you correctly, the assembly code my web-app produces can be compiled using not only FASM, but also using MASM? That's quite surprising. I've checked it and it doesn't compile in the GAS intel-syntax mode.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on March 15, 2018, 01:28:21 PM
So, if I understood you correctly, the assembly code my web-app produces can be compiled using not only FASM, but also using MASM? That's quite surprising.

Not that surprising, actually. FPU code is pretty standard, it's mnemonics like fld somevar or fxch that all assemblers should treat the same way. The differences in headers and directives are much more important.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on March 15, 2018, 06:03:02 PM
Well, not all assemblers let you to use the syntax such as "st0" to access the stack (some require you to use the longer "ST(0)" format), and not all assemblers let you use a syntax such as "2f" to create 32-bit floats and use them as the arguments for "mov".
It compiles them the same way FASM does? A bit complicated example, if you assemble the web-app's output for "(x<0)*(-x)+(x>0)*x", does the variable "result" contain the absolute value of the variable "x"?

So, nobody here uses EMACS? Well, to be honest, neither do I. I usually use "xed" on Mac and Notepad++ on PC.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on March 15, 2018, 07:29:52 PM
Well, not all assemblers let you to use the syntax such as "st0" to access the stack (some require you to use the longer "ST(0)" format), and not all assemblers let you use a syntax such as "2f" to create 32-bit floats and use them as the arguments for "mov".

You are right, the differences are bigger than I thought :icon_redface:

finitfinit
fld dword [alpha]fld alpha
mov [result],57.2957795fm2m result, FP4(57.2957795)
fld dword [result]fld dword ptr result
fdivp st1, st0fdivp st(1), st
fsinfsin
mov [result],2fm2m result, FP4(2.)
fld dword [result]fld dword ptr result
fxchfxch
fld1fld1
fxchfxch
fyl2xfyl2x
fldl2efldl2e
fdivp st1, st0fdivp st(1), st
fmulp st1, st0fmulp st(1), st
fldl2efldl2e
fmulp st1, st0fmulp st(1), st
fld1fld1
fscalefscale
fxchfxch
fld1fld1
fxchfxch
fpremfprem
f2xm1f2xm1
faddp st1, st0faddp st(1), st
fmulp st1, st0fmulp st(1), st
fld dword [alpha]fld alpha
mov [result],57.2957795fm2m result, FP4(57.2957795)
fld dword [result]fld result
fdivp st1, st0fdivp st(1), st
fcosfcos
mov [result],2fm2m result, FP4(2.)
fld dword [result]fld result
fxchfxch
fld1fld1
fxchfxch
fyl2xfyl2x
fldl2efldl2e
fdivp st1, st0fdivp st(1), st
fmulp st1, st0fmulp st(1), st
fldl2efldl2e
fmulp st1, st0fmulp st(1), st
fld1fld1
fscalefscale
fxchfxch
fld1fld1
fxchfxch
fpremfprem
f2xm1f2xm1
faddp st1, st0faddp st(1), st
fmulp st1, st0fmulp st(1), st
faddp st1, st0faddp st(1), st
mov [result],0fmov result, 0
fld dword [result]fld result
fxchfxch
fsubp st1, st0fsubp st(1), st
mov [result],0fmov result, 0
fld dword [result]fld result
mov [result],1fm2m result, FP4(1.)
fld dword [result]fld result
fsubp st1, st0fsubp st(1), st
fsubp st1, st0fsubp st(1), st
fabsfabs
mov [result],1fm2m result, FP4(1.)
fld dword [result]fld result
mov [result],1000fm2m result, FP4(1000.)
fld dword [result]fld result
fdivp st1, st0fdivp st(1), st
fcomip st1fcomip st, st(1)
fstp dword [result]fstp result
jna l194549jna l194549
fld1fld1
jmp l876055jmp l876055
l194549:l194549:
fldzfldz
l876055:l876055:
mov [result],2fm2m result, FP4(2.)
fld dword [result]fld result
mov [result],2fm2m result, FP4(2.)
fld dword [result]fld result
faddp st1, st0faddp st(1), st
mov [result],5fm2m result, FP4(5.)
fld dword [result]fld result
fcomip st1fcomip st, st(1)
fstp dword [result]fstp result
jne l436047jne l436047
fld1fld1
jmp l71133jmp l71133
l436047:l436047:
fldzfldz
l71133:l71133:
mov [result],3fm2m result, FP4(3.)
fld dword [result]fld result
mov [result],1fm2m result, FP4(1.)
fld dword [result]fld result
faddp st1, st0faddp st(1), st
mov [result],3.5fm2m result, FP4(3.5)
fld dword [result]fld result
fcomip st1fcomip st, st(1)
fstp dword [result]fstp result
jnb l613334jnb l613334
fld1fld1
jmp l467593jmp l467593
l613334:l613334:
fldzfldz
l467593:l467593:
fistp dword [result]fistp result
mov eax,[result]mov eax, result
fistp dword [result]fistp result
or [result],eaxor result,eax
fild dword [result]fild result
fistp dword [result]fistp result
mov eax,[result]mov eax, result
fistp dword [result]fistp result
and [result],eaxand result,eax
fild dword [result]fild result
mov [result],45fm2m result, FP4(45.)
fld dword [result]fld result
fstp dword [result]fstp result
fld dword [result]fld result
fld1fld1
fld dword [result]fld result
fld dword [result]fld result
fmulp st1, st0fmulp st(1), st
fsubp st1, st0fsubp st(1), st
fsqrtfsqrt
fdivp st1, st0fdivp st(1), st
fld1fld1
fpatanfpatan
mov [result],57.2957795fm2m result, FP4(57.2957795)
fld dword [result]fld result
fxchfxch
fmulp st1, st0fmulp st(1), st
mov [result],0.5fm2m result, FP4(0.5)
fld dword [result]fld result
fsubp st1, st0fsubp st(1), st
fabsfabs
mov [result],1fm2m result, FP4(1.)
fld dword [result]fld result
mov [result],1000fm2m result, FP4(1000.)
fld dword [result]fld result
fdivp st1, st0fdivp st(1), st
fcomip st1fcomip st, st(1)
fstp dword [result]fstp result
jna l606217jna l606217
fld1fld1
jmp l520364jmp l520364
l606217:l606217:
fldzfldz
l520364:l520364:
fld1fld1
fxchfxch
fsubp st1, st0fsubp st(1), st
fistp dword [result]fistp result
mov eax,[result]mov eax, result
fistp dword [result]fistp result
and [result],eaxand result,eax
fild dword [result]fild result
fstp dword [result]fstp result

Quote
So, nobody here uses EMACS? Well, to be honest, neither do I. I usually use "xed" on Mac and Notepad++ on PC.

RichMasm (http://masm32.com/board/index.php?topic=5314.0), of course :P
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on March 16, 2018, 07:18:21 AM
So, indeed, it doesn't compile in MASM? Well, I guess it's still easier to manually convert the FASM code into the MASM code than to manually convert arithmetic expressions to assembly. Or is there a way to automatically convert the FASM code into the MASM code? Perhaps by compiling the code with FASM and then using a disassembler?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on March 16, 2018, 08:03:01 AM
Compiling the code with FASM and then using a disassembler should work, but that's a workaround. If it was my project, I would consider two versions, but it seems you are normally working with FASM...
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on April 07, 2018, 12:26:16 AM
Maybe you can modify mine (fork it) to produce MASM-compatible code?
Just download the latest source code, I've fixed some bugs and implemented some basic error-handling since the version you posted here.

BTW, I can't find out, what do "m2m" and "fp4" mean exactly in MASM?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: jj2007 on April 07, 2018, 05:07:04 AM
Maybe you can modify mine (fork it) to produce MASM-compatible code?
Sounds intriguing but right now I have absolutely no spare time...

Quote
BTW, I can't find out, what do "m2m" and "fp4" mean exactly in MASM?

m2m is mem to mem:

m2m someint32, someotherint32 basically translates to

push someotherint32
pop someint32

FP4(123.456) creates a REAL4 in the .data section. You can use it, for example, as
fld FP10(1.234567890123456789)
fsub FP4(123.456)
fmul FP8(0.1234567890123456)
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on May 01, 2018, 04:08:19 AM
What do you think about the syntax highlighting for assembly I've just added to my web-app?
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on November 13, 2018, 03:24:15 AM
I've put some effort into designing the way the web-app looks. Do you like it? The layout works in IE6, IE9 and IE11, and all the non-Internet-Explorer browsers I've tested it in except Safari 5, but it breaks in IE7, IE8 and IE10.
Title: Re: Converting Arithmetic Expressions to Assembly
Post by: UniverseIsASimulation on June 16, 2020, 01:52:52 AM
000webhost has banned me for hate speech, so I moved my website to GitHub Pages. So, the ArithmeticExpressionCompiler is now available here (https://flatassembler.github.io/compiler.html). Now it supports GNU Assembler as well as FlatAssembler. And now it can not only be run in a browser or Duktape, but also in NodeJS, the NodeJS executable is available here (https://github.com/FlatAssembler/ArithmeticExpressionCompiler/releases/download/1.1/aec.js). A relatively long example program it can compile is available here (https://raw.githubusercontent.com/FlatAssembler/ArithmeticExpressionCompiler/master/HybridSort/hsort.aec) (it's the implementation of the linearithmic-time sorting algorithm I made).