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

HSE

  • Member
  • *****
  • Posts: 1377
  • <AMD>< 7-32>
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:=< >

    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
                        ;;echo es doble
                        tk_is INSTR 1, tk_op, <&char>
                        IF tk_is EQ 1
                            ;;echo es igual
                            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

    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
      BIT equ 10

Results:
Code: [Select]
fSlvB eax ^= BIT

xor eax  ,  BIT
------------------------------
fSlvB res1 &= BIT

mov eax, res1
and eax ,  BIT
mov res1 , eax
------------------------------
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: July 17, 2020, 06:07:54 AM by HSE »

Biterider

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

HSE

  • Member
  • *****
  • Posts: 1377
  • <AMD>< 7-32>
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


daydreamer

  • Member
  • *****
  • Posts: 1357
  • building nextdoor
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
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7539
  • 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: 10543
  • 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: 1377
  • <AMD>< 7-32>
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:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7539
  • 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: 1377
  • <AMD>< 7-32>
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: