The MASM Forum

Miscellaneous => Miscellaneous Projects => Topic started by: HSE on July 17, 2020, 04:13:17 AM

Title: Bitwise compound operators: &=, |= and ^=
Post by: HSE 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
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: Biterider on July 17, 2020, 05:51:51 AM
Good idea  :thumbsup:
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: HSE 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

Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: daydreamer 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
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: hutch-- 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:
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: jj2007 on July 28, 2020, 05:38:17 PM
JJ will start sobbing if you use this notation.  :tongue:

Confirmed!
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: HSE 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:
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: hutch-- 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.
Title: Re: Bitwise compound operators: &=, |= and ^=
Post by: HSE 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: