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 Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 598
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
Post 08 Jul 2013, 08:09
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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
Post 08 Jul 2013, 08:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
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.
Post 08 Jul 2013, 10:24
View user's profile Send private message Visit poster's website Reply with quote
Roman



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


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

Post 08 Jul 2013, 13:02
View user's profile Send private message Reply with quote
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'    
Post 08 Jul 2013, 18:25
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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
Post 09 Jul 2013, 07:00
View user's profile Send private message Reply with quote
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 {})
Post 09 Jul 2013, 15:42
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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
\}                        
}
    
Post 10 Jul 2013, 17:28
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
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
Post 10 Jul 2013, 22:19
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
Roman
bitRAKE thanks !
Mama Mia !
Incredible and amazing example !
Sad fact that in this way macros are not considered in the documentation on FASM Sad
Why ?
Post 11 Jul 2013, 06:43
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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?
Post 11 Jul 2013, 07:38
View user's profile Send private message Reply with quote
Roman



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

_dv dd 2.0
_t dd 4.0
Post 11 Jul 2013, 08:00
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
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 
    \\}    
Post 12 Jul 2013, 00:46
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
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
Post 12 Jul 2013, 01:56
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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)
Post 12 Jul 2013, 08:49
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
I'm sorry it was not clear. Use both and it works for any number of "/" operations. Wink
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
Post 12 Jul 2013, 10:18
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
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
Post 12 Jul 2013, 11:31
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
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.
Post 12 Jul 2013, 12:27
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 598
Roman
bitRAKE
What about this
XMMath [_dv]/2.0/[_dv]/[_t]+11.11
Post 12 Jul 2013, 12:35
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
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
Post 12 Jul 2013, 20:25
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.