Author Topic: Bitwise compound operators: &=, |= and ^=  (Read 3630 times)

HSE

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Bitwise compound operators: &=, |= and ^=
« on: July 17, 2020, 04:13:17 AM »
Hi all!!

To translate from C++, I maked a little macro. This can save some lines even if you are not translating from  C++:
Code: [Select]
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:
Code: [Select]
      item dd 0
      res1 dd 0
      res2 dw 0
      BIT equ 10

Results:
Code: [Select]
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
« Last Edit: December 23, 2021, 12:37:34 AM by HSE »
Equations in Assembly: SmplMath

Biterider

  • Member
  • ****
  • Posts: 984
  • ObjAsm Developer
    • ObjAsm
Re: Bitwise compound operators: &=, |= and ^=
« Reply #1 on: July 17, 2020, 05:51:51 AM »
Good idea  :thumbsup:

HSE

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Re: Bitwise compound operators: &=, |= and ^=
« Reply #2 on: July 17, 2020, 06:27:24 AM »
 :biggrin:  Is in progress! (some unnecessary things still)

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

Update in first post.

Code: [Select]
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

  • Member
  • *****
  • Posts: 2094
  • beer glass
Re: Bitwise compound operators: &=, |= and ^=
« Reply #3 on: July 19, 2020, 04:20:46 AM »
doing great work Hector :thumbsup:
but honest,I dont like most of those compound operators in C
SIMD fan and macro fan
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9794
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Bitwise compound operators: &=, |= and ^=
« Reply #4 on: July 28, 2020, 12:35:26 PM »
Hector,

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

JJ will start sobbing if you use this notation.  :tongue:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 12999
  • Assembler is fun ;-)
    • MasmBasic
Re: Bitwise compound operators: &=, |= and ^=
« Reply #5 on: July 28, 2020, 05:38:17 PM »
JJ will start sobbing if you use this notation.  :tongue:

Confirmed!

HSE

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Re: Bitwise compound operators: &=, |= and ^=
« Reply #6 on: July 28, 2020, 11:56:19 PM »
JJ will start sobbing if you use this notation.  :tongue:
:biggrin: I know.  :thumbsup:

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

Confirmed!

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9794
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Bitwise compound operators: &=, |= and ^=
« Reply #7 on: August 01, 2020, 02:10:01 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

HSE

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Re: Bitwise compound operators: &=, |= and ^=
« Reply #8 on: August 02, 2020, 06:20:40 AM »
... 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:).


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

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Re: Bitwise compound operators: &=, |= and ^=
« Reply #9 on: December 23, 2021, 12:40:25 AM »
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