News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Converting Arithmetic Expressions to Assembly

Started by UniverseIsASimulation, February 24, 2018, 06:50:31 AM

Previous topic - Next topic

aw27

Quote from: 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.
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.

HSE

Internet even exist when I have 18. And in that case, no money to expend in it.   :biggrin:
Equations in Assembly: SmplMath

aw27

Quote from: 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:
No free lunches, though, except in Salvation Army.  :t

markallyn

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

UniverseIsASimulation

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.
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html

UniverseIsASimulation

Does anyone here happen to know the algorithms Emacs calculator uses?
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html

felipe

Quote from: UniverseIsASimulation on March 07, 2018, 12:11:31 AM
Does anyone here happen to know the algorithms Emacs calculator uses?

Aren't the sources of emacs calculator freely available?  :idea:

aw27

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.

UniverseIsASimulation

Quote from: felipe on March 07, 2018, 02:17:22 AM
Quote from: UniverseIsASimulation on March 07, 2018, 12:11:31 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.
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html

jj2007

Quote from: UniverseIsASimulation on March 15, 2018, 06:44:21 AMSo, 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.

UniverseIsASimulation

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.
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html

jj2007

Quote from: 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".

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

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

RichMasm, of course :P

UniverseIsASimulation

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?
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html

jj2007

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...

UniverseIsASimulation

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?
Author of the AEC programming language:
https://flatassembler.github.io/compiler.html