News:

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

Main Menu

Bitwise compound operators: &=, |= and ^=

Started by HSE, July 17, 2020, 04:13:17 AM

Previous topic - Next topic

HSE

Hi all!!

To translate from C++, I maked a little macro. This can save some lines even if you are not translating from  C++:fSlvB macro Expression:=< >, AuxReg

    tk_dest TEXTEQU <>
    tk_exp  TEXTEQU <>
    tk_op TEXTEQU <>
    tk_op1  = 0
    tk_op2 = 0

    FORC char,<&Expression>
        IF tk_op2 EQ 0
            tk_switch INSTR 1,<&|^{}>,<&char>
            IF tk_switch EQ 0
                IF tk_op1 EQ 0 
                    tk_dest CATSTR tk_dest,<&char>
                ELSE
                    tk_switch2 INSTR 1,<=>,<&char>
                    IF tk_switch2 EQ 1 
                        IF tk_op1 EQ 1
                            tk_op1 = 2
                            tk_op CATSTR tk_op,<&char>
                            tk_op2 = 1
                        ELSE
                            .err A2052  invalid compound operator
                        ENDIF
                    ELSE
                        IF tk_op1 EQ 0 
                           tk_dest CATSTR tk_dest,tk_op
                           tk_op TEXTEQU <>
                           tk_dest CATSTR tk_dest,<&char>
                           tk_op1 = 0
                        ELSE
                            .err A2052  invalid bitwise operator
                        ENDIF
                    ENDIF       
                ENDIF
            ELSE
                IF tk_op1 EQ 0 
                    tk_op1 = 1
                    tk_op CATSTR tk_op,<&char>
                    tk_switch3 = tk_switch
                ELSE
                    tk_switch2 INSTR 1,<{}>,<&char>
                    IF tk_switch2 GT 0
                        tk_is INSTR 1, tk_op, <&char>
                        IF tk_is EQ 1
                            tk_op2 = 1
                        ELSE
                            .err A2052 invalid compound operator
                        ENDIF
                    ELSE
                        .err A2052  invalid compound operator
                    ENDIF
                ENDIF
           ENDIF
        ELSE
            tk_exp CATSTR tk_exp,<&char>
        ENDIF
    ENDM

    IF tk_switch3 EQ 1
        tk_op3 TEXTEQU <and>
    ELSEIF tk_switch3 EQ 2
        tk_op3 TEXTEQU <or>
    ELSEIF tk_switch3 EQ 3
        tk_op3 TEXTEQU <xor>
    ELSEIF tk_switch3 EQ 4
        tk_op3 TEXTEQU <shl>
    ELSEIF tk_switch3 EQ 5
        tk_op3 TEXTEQU <shr>
    ENDIF

             ifnb <&AuxReg>
                  _eax textequ <&AuxReg>
              else
                  if @WordSize eq 8
                      _eax textequ <r10>
                  else
                      _eax textequ <eax>
                  endif
              endif

    IF (OPATTR(tk_dest)) and 00010000y ;; register
        tk_op3 tk_dest , tk_exp
    ELSEIF (OPATTR(tk_dest)) and 00000100y ;; inmediate
        .err A2052 invalid bit operation on inmediate
    ELSE
        mov _eax, tk_dest
        IF tk_switch3 LT 4
            tk_op3 _eax , tk_exp
        ELSE
            IF (OPATTR(tk_exp)) and 00000100y ;; inmediate
                tk_op3 _eax , tk_exp
            ELSE
                mov ecx, tk_exp
                tk_op3 _eax , cl
            ENDIF
        ENDIF
        mov tk_dest, _eax
    ENDIF
endm


Where:      item dd 0
      res1 dd 0
      res2 dw 0
      BIT equ 10


Results:
fSlvB eax ^= BIT

xor eax  ,  BIT
------------------------------
fSlvB res2 &= BIT, dx

mov dx, res1
and dx ,  BIT
mov res1 , dx
------------------------------
fSlvB res1 |= item

mov eax, res1
or eax , item
mov res1 , eax
------------------------------
fSlvB BIT &= 5

... : error A2008: syntax error : invalid bit operation on inmediate


The idea is to add also >> and <<  :biggrin:

Regards. HSE
Equations in Assembly: SmplMath

Biterider


HSE

 :biggrin:  Is in progress! (some unnecessary things still)

It's a little problematic "<<", but work "{{".

Update in first post.


fSlvB eax {{ BIT

shl eax  ,  BIT
------------------------------
fSlvB res1 {{ BIT

mov eax, res1
shl eax ,  BIT
mov res1 , eax
  ------------------------------
fSlvB res1 }} item

mov eax, res1
mov ecx,  item
shr eax , cl
mov res1 , eax

Equations in Assembly: SmplMath

daydreamer

doing great work Hector :thumbsup:
but honest,I dont like most of those compound operators in C
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

hutch--

Hector,

> It's a little problematic "<<", but work "{{".

JJ will start sobbing if you use this notation.  :tongue:

jj2007


HSE

Quote from: hutch-- on July 28, 2020, 12:35:26 PM
JJ will start sobbing if you use this notation.  :tongue:
:biggrin: I know.  :thumbsup:

Quote from: jj2007 on July 28, 2020, 05:38:17 PM
Quote from: hutch-- on July 28, 2020, 12:35:26 PMJJ will start sobbing if you use this notation.  :tongue:

Confirmed!

The alternative is to build a compiler!  :biggrin:
Equations in Assembly: SmplMath

hutch--

There is another choice, when I built a wrapper around Vasily's ".if" notation, instead of using symbols I used the old pre-processor notation eq ne gt lt etc ....

Its really easy to read and completely removes any ugly notation.

HSE

Quote from: hutch-- on August 01, 2020, 02:10:01 PM
... when I built a wrapper around Vasily's ".if" notation ....

Just yesterday I was reading those macros. Perhaps they could benefit from some ideas I 'm developing in 32 bits (without hurry, of course  :biggrin:).


Quote from: hutch-- on August 01, 2020, 02:10:01 PM
Its really easy to read and completely removes any ugly notation.

:biggrin: No problem with symbol ugliness for now. Thanks anyway  :thumbsup:
Equations in Assembly: SmplMath

HSE

Hi all!

Now by default auxiliar GPR is DWORD size in 32 bits or QWORD size in 64bits: EAX/R10. Other way you can define an auxiliar register (and then size).

Updated code in first post.

Regards, HSE.
Equations in Assembly: SmplMath