flat assembler
Message board for the users of flat assembler.

 Index > Main > sorting the mathematical operations like this 2+10-2*4/3 Goto page 1, 2  Next
Author
 Thread
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
My macros:
Code:
```macro CoolMath  [exp]
{
common match \exp,exp
\{
_rest equ \exp
irps sym,\exp
\\{    Vul = 0
display \\`sym
display 13,10

if \\`sym = `+
display "Plus"
display 13,10
Vul = 1
end if
if \\`sym = `*
display "Mul"
display 13,10
Vul = 1
end if
if \\`sym = `-
display "Minus"
display 13,10
Vul = 1
end if
if \\`sym = `/
display "Div"
display 13,10
Vul = 1
end if
if Vul = 0
invoke MessageBox,0,"l",\\`sym,0
end if
\\}
restore _rest
\}
}
CoolMath 2+10-2*4/3    ```
edit by revolution: Added code tags
08 Jul 2013, 08:09
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
I want sort by mathematical operations and break into groups. And then we get like this first -2*4=x then x/3=z and then 2+10=y and at last we have y+z
I found a bug. If write CoolMath 2*99.99999 work, but if write CoolMath 2*199.99999 fasm swears for number 199.99999. Why ?

I use Fasm version 1.69.03
08 Jul 2013, 08:11
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18115
Location: In your JS exploiting you and your system
revolution
Roman wrote:
I found a bug. If write CoolMath 2*99.99999 work, but if write CoolMath 2*199.99999 fasm swears for number 199.99999. Why ?
What do you mean? Please give an example where you are using these numbers. With the code given it does not compile for me. Perhaps you can consider posting a fully coded example for us to test with.
Roman wrote:
I use Fasm version 1.69.03
Perhaps a later version would be a better choice. Not many here will have this version ready to run so that means we are less likely to be testing using the same version as you.
08 Jul 2013, 10:24
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
Revolution
compile this code. And you see.

 Description: compile this code Download Filename: Math.ASM Filesize: 1.43 KB Downloaded: 328 Time(s)

08 Jul 2013, 13:02
uart777

Joined: 17 Jan 2012
Posts: 369
uart777
`s and \s are misplaced and if should be replaced with match. Try something like this:
Code:
```macro MATH [p] {
forward
define ?s 0
match a+b, p \{
display 'add '
define ?s 1     ; success
\}
match =0 a-b, ?s p \{
display 'sub '
define ?s 1     ; success
\}
match =0 a*b, ?s p \{
display 'mul '
define ?s 1     ; success
\}
match =0 a/b, ?s p \{
display 'div '
define ?s 1     ; success
\}
if ?s eq 0        ; no successful match?
'Syntax error'
end if
}    ```
Example:
Code:
`MATH a+b, a*b ; displays 'add mul'    `
08 Jul 2013, 18:25
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
uart777
Thanks

But how get this:
MATH a+b - a*b or MATH a+b + a*b or MATH a+b * a*b

uart777
if we write MATH a+b, a*b then it is not clear how the operation will be between a+b and a*b
09 Jul 2013, 07:00
uart777

Joined: 17 Jan 2012
Posts: 369
uart777
What are you trying to do? Just learning and practicing? How to match this? a+b - a*b? Exactly as-is.
Code:
```match a+b - x*y, p {
; ...
}    ```
Only = and , are special symbols that need to be prefixed with =. Example:
Code:
```match (i==0=,i<n=,i++), p { ; match (i=0,i<n,i++)
; ...
}    ```
(Obviously, match needs \s when inside blocks {})
09 Jul 2013, 15:42
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
Now I am writing hands actions for different operations. But how is it easier with macros? The more operations, the more combinations will have to write your hands. It's like a sad write hands:(
Code:
```macro MMXFlt2 Mesto,[p]{
Dons    EQU No
match a+b - x*y, p \{
mov   eax,a
movd  xmm1,eax
mov   eax,b
movd  xmm2,eax
addss xmm1,xmm2
mov   eax,x
movd xmm3,eax
mov   eax,y
movd xmm4,eax
mulss   xmm3,xmm4
subss   xmm1,xmm3
movss   [Mesto],xmm1
Dons    EQU Yes
;cvtss2si eax,xmm1    ;convert to integer number
\}
match =No a+b + x*y, Dons p \{
mov   eax,a
movd  xmm1,eax
mov   eax,b
movd  xmm2,eax
addss xmm1,xmm2
mov   eax,x
movd xmm3,eax
mov   eax,y
movd xmm4,eax
mulss   xmm3,xmm4
addss   xmm1,xmm3
movss   [Mesto],xmm1
Dons    EQU Yes
\}
match =No a+b * x*y, Dons p \{
mov   eax,a
movd  xmm1,eax
mov   eax,b
movd  xmm2,eax
;addss xmm1,xmm2
mov   eax,x
movd xmm3,eax
mov   eax,y
movd xmm4,eax
mulss   xmm3,xmm4
mulss   xmm3,xmm2
addss   xmm1,xmm3
movss   [Mesto],xmm1
Dons    EQU Yes
\}
}
```
10 Jul 2013, 17:28
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
Here is another possibility:
Code:
```; stack of registers
rept 8 i:0 { XREG equ xmm#i }

macro def_XMMath {
macro XMMath [str*] \{ common
define OK =
def_XMMath ; allow recursion

; low precedence operations first

match R == A + B,XREG OK str \\{
restore OK
define OK ,
XMMath A ; recurse on A
restore XREG ; preserve value
XMMath B ; recurse on B
addss R,XREG
XREG equ R
\\}
match R == A * B,XREG OK str \\{
restore OK
define OK ,
XMMath A ; recurse on A
restore XREG
XMMath B ; recurse on B
mulss R,XREG
XREG equ R
\\}
; no match, assume memory value
match R ==,XREG OK \\{
movss R,str
\\}
purge XMMath
restore OK
\}
} def_XMMath

; Example of use:
_dv dd 9.80665
_dx dd 1.973
_t  dd 9.12345
_x0 dd -5.0

entry \$
; generate code to put result in XMM7 (top of stack register)
XMMath [_dv] * [_t] * [_t] + [_dx] * [_t] + [_x0]    ```
Not very efficient, but it seems to work.
Less manual work by programmer, though.

(Partly based on the discussion here.)

_________________
¯\(°_o)/¯ unlicense.org
10 Jul 2013, 22:19
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
bitRAKE thanks !
Mama Mia !
Incredible and amazing example !
Sad fact that in this way macros are not considered in the documentation on FASM
Why ?
11 Jul 2013, 06:43
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
bitRAKE
Thanks for help.
If you know, please tell me about this.

In macro (for example Val equal [_dx]*[_x0] )
if i write movss xmm1,Val Fasm will give an error, because we get movss xmm1,[_dx]*[_x0].
But how to make the Val equal [_x0] (and not using match)?
I mean depose Val to [_x0].
This can be done without match?
11 Jul 2013, 07:38
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
Sad
XMMath [_t]/[_t]/[_dv] get value 2.0 ! This is wrong , must be 0.5 !

_dv dd 2.0
_t dd 4.0
11 Jul 2013, 08:00
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
Topic was kind of split.
Answer should be here, too:
Code:
```    match R == A / B / C,XREG OK str \\{
restore OK
define OK
XMMath A
restore XREG
XMMath B
divss R,XREG
XREG equ R
XMMath R / C
\\}    ```
12 Jul 2013, 00:46
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
Roman wrote:
In macro (for example Val equal [_dx]*[_x0] )
if i write movss xmm1,Val Fasm will give an error, because we get movss xmm1,[_dx]*[_x0].
But how to make the Val equal [_x0] (and not using match)?
I mean depose Val to [_x0].
This can be done without match?
I'm confused by this. You appear to be mixing compile-time and run-time. Basically, the preprocessor works with strings - it doesn't run any code - just string manipulation. Please, describe your goal, or include a complete example.

p.s. You don't need to PM me for every question. I will respond to the thread if I can. My email is at gmail if you have something private, or want to pay me (paypal) for more prompt replies. Otherwise just be patient, please.

_________________
¯\(°_o)/¯ unlicense.org
12 Jul 2013, 01:56
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
bitRAKE
this work if we using only 3 operations
But how to make this code more versatile(i mean using in macro)?
Code:
```match R == A / B / C,XREG OK str \\{
restore OK
define OK
XMMath A
restore XREG
XMMath B
divss R,XREG
XREG equ R
XMMath R / C
\\}
```

How to write this example for the next combination?
XMMath [_t]/[_t]/[_dv]/../.../ (i mean we dont know how much operations div were. Maybe 4 operations or maybe 7 operations)
12 Jul 2013, 08:49
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
I'm sorry it was not clear. Use both and it works for any number of "/" operations.
Code:
```; stack of availible registers
rept 16 i:0 { XREG equ xmm#i } ; amd64 mode

macro def_XMMath {
macro XMMath [str*] \{ common
define OK =
def_XMMath ; allow recursion

; low precedence functions first

match R == A + B,XREG OK str \\{
restore OK
define OK
XMMath A ; recurse on A
restore XREG ; preserve value
XMMath B ; recurse on B
addss R,XREG
XREG equ R
\\}
match R == A - B,XREG OK str \\{
restore OK
define OK
XMMath A ; recurse on A
restore XREG ; preserve value
XMMath B ; recurse on B
subss R,XREG
XREG equ R
\\}

match R == A * B,XREG OK str \\{
restore OK
define OK
XMMath A
restore XREG
XMMath B
mulss R,XREG
XREG equ R
\\}
match R == A / B / C,XREG OK str \\{
restore OK
define OK
XMMath A
restore XREG
XMMath B
divss R,XREG
XREG equ R
XMMath R / C
\\}
match R == A / B,XREG OK str \\{
restore OK
define OK
XMMath A
restore XREG
XMMath B
divss R,XREG
XREG equ R
\\}

; no match, assume memory value
match R ==,XREG OK \\{
; skip top of stack register
match =R,str \\\{
restore OK
define OK
\\\}
match ==,OK \\\{
movss R,str
\\\}
\\}
purge XMMath
restore OK
\}
} def_XMMath    ```
Should be easy to add constants with some prefix. Still pondering a simple way to add support for parenthesis (to collapse common sub-expressions manually). Anyhow, it's a compact domain specific language - the instructions can be changed to support a variety of uses: which is what interests me.

For example, the register stack could be the 64-bit registers r8-r15, and the instructions could be changed to use basic integer arithmetic, or fixed point code, etc. A slight adaptation would even work for the FPU math.

_________________
¯\(°_o)/¯ unlicense.org
12 Jul 2013, 10:18
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
bitRAKE
Thanks again !
But there is one point, you macro XMMath generated a lot of extra commands.
I mean XMMath [_dv]/[_dv]/[_dv]/[_t]+[_t]
We get 11 SSE commands !
If write hands we write 5 SSE commands !
if we write a lot of similar (XMMath [_dv]/[_dv]/[_dv]/[_t]+[_t]) we get a lot of extra commands !

In any case, your macro zingy
12 Jul 2013, 11:31
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
Hmm...maybe you can figure out an optimization to improve the code generation. For example, if B is a register or memory location then it can be used directly in the instruction.

I wonder if the "no match" case could assign the string to another symbol which would work as the second operand to the instructions? Probably something like that.
12 Jul 2013, 12:27
Roman

Joined: 21 Apr 2012
Posts: 1065
Roman
bitRAKE
What about this
XMMath [_dv]/2.0/[_dv]/[_t]+11.11
12 Jul 2013, 12:35
bitRAKE

Joined: 21 Jul 2003
Posts: 3235
Location: vpcmipstrm
bitRAKE
Processing constants aren't that difficult.
Code:
```match R == _ A,XREG OK str \\{
< store constant somewhere and reference it >
\\}    ```
The prefix "_" is just a placeholder for whatever one wishes to use. How the constant is stored and referenced can vary greatly. Could be immediate data in the code stream; stored on the stack; or a complex macro creating an array of unique constants.

Different prefixes could be used for integers and float. Could detect common constants for special handling - like 0,+/-1, etc.

_________________
¯\(°_o)/¯ unlicense.org
12 Jul 2013, 20:25
 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

Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.