flat assembler
Message board for the users of flat assembler.

Index > Windows > bitewise operations

Author
Thread Post new topic Reply to topic
azdps



Joined: 10 Jul 2009
Posts: 3
Location: Tucson, Arizona
azdps 14 Jul 2009, 17:34
i've been reading up on bitwise operations xor, or , and, not and i'm not understanding them like i should. i don't see how i would use them in an application. can someone describe the most common uses for these. specifically or, and and not.
Post 14 Jul 2009, 17:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20290
Location: In your JS exploiting you and your system
revolution 14 Jul 2009, 17:41
AND, OR and NOT are the fundamental operations that the logic gates can do. With these operations you can make adders (subtracters/comparitors) and in turn from those build multipliers and dividers etc.
Post 14 Jul 2009, 17:41
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Jul 2009, 18:08
azdps: In practice, you use OR to set (set to 1) particular bit, AND to clear (set to 0) particular bit, and XOR to switch particular bit to opposite.

How it works:
- any bit that is ORed by 0 remains as is. Any bit that is ORed by 1 is set to 1, regardless of its previous value. This allows you set bits you want to 1 and leave other bits unchanged.
- any bit that is ANDed by 1 remains as is. Any bit that is ANDed by 0 is set to 0, regardless of its previous value. This allows you set bits you want to 1 and leave other bits unchanged.
- any bit that is XORed by 0 remains as is. Any bit that is XORed by 1 is set to the other value (1 to 0, 0 to 1). This allows you to flip bits you want, and leave other bits unchanged.

revolution: can't you do all 16 operations just with NOP?
Post 14 Jul 2009, 18:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20290
Location: In your JS exploiting you and your system
revolution 14 Jul 2009, 18:12
16 operations? With NOP?
Post 14 Jul 2009, 18:12
View user's profile Send private message Visit poster's website Reply with quote
azdps



Joined: 10 Jul 2009
Posts: 3
Location: Tucson, Arizona
azdps 14 Jul 2009, 18:16
thanks for the replys. vid, i understand how they work. i really just don't understand any practical uses for them. i would like to see some examples of the most comon uses so that I look at them and realize that... hey.. i understand how i would be using them. right now i can code in assembly without them but i don't use them except for xor.


Last edited by azdps on 14 Jul 2009, 20:01; edited 1 time in total
Post 14 Jul 2009, 18:16
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder 14 Jul 2009, 18:26
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:48; edited 1 time in total
Post 14 Jul 2009, 18:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
Location: Kraków, Poland
Tomasz Grysztar 14 Jul 2009, 18:39
vid wrote:
revolution: can't you do all 16 operations just with NOP?

revolution wrote:
16 operations? With NOP?

Very Happy

Perhaps he did mean NOR - but this way we've got the joke of the day. Wink
Post 14 Jul 2009, 18:39
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 14 Jul 2009, 18:58

_________________
----> * <---- My star, won HERE
Post 14 Jul 2009, 18:58
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 14 Jul 2009, 19:58
@azdps
Code:
;;arg1 N rcx value between 0 and 63
;;return rax as 2^N
Math_2powN:
;;clamp N to make sure it's between 0 and 63
AND rcx,63 ;63 = 111111b AND'd N <= 63
;;clear RAX set = 0
XOR rax,rax ;anything xor'd with itself = 0
;;set RAX (which is 0) to 1
OR rax,1 ;0 OR'd 1 = 1
;;shift RAX to the correct power
SHL rax,cl ; CL is [0,63) and RAX = 1 and SHL shifts 1 by the power N
ret 0
    
Post 14 Jul 2009, 19:58
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Jul 2009, 20:17
Quote:
Perhaps he did mean NOR - but this way we've got the joke of the day.

heh-heh, yeah Smile

But I think there was a thread about various instruction lengths for NOP operation, up to max instruction length = 16. If that is true, I wasn't that far either.... Embarassed
Post 14 Jul 2009, 20:17
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20290
Location: In your JS exploiting you and your system
revolution 14 Jul 2009, 20:28
Have you ever seen a length=16 instruction? Hint: A trick question. Read the manuals about maximum possible instruction length.
Post 14 Jul 2009, 20:28
View user's profile Send private message Visit poster's website Reply with quote
Fanael



Joined: 03 Jul 2009
Posts: 168
Fanael 14 Jul 2009, 20:50
Maximum possible instruction length? 15 bytes, isn't it?
Post 14 Jul 2009, 20:50
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Jul 2009, 21:14
You forgot one very important thing - NOP is the only operation that can be encoded in 0 bytes, giving you 16 possibilities Laughing

But seriously, I think I have read that CPU at most fetches 16 byte instruction, if there is duplicated prefix or something? I never really looked into this anyway.
Post 14 Jul 2009, 21:14
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 14 Jul 2009, 21:26
I opened a thread about this. If I remember right the behavior was #UD exception for 16-byte instruction and #GP exception for instructions bigger than that (in both cases repeating prefixes of course).
Post 14 Jul 2009, 21:26
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 14 Jul 2009, 21:52

_________________
----> * <---- My star, won HERE
Post 14 Jul 2009, 21:52
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 14 Jul 2009, 22:02
windwakr, exactly. So, a swap between #UD and #GP in my post above is required.
Post 14 Jul 2009, 22:02
View user's profile Send private message Reply with quote
ramguru



Joined: 26 Feb 2005
Posts: 19
Location: who cares...
ramguru 14 Jul 2009, 22:21
AND can erase bits you don't want & leave those you do
For example
WM_MOUSEMOVE
(The low-order word specifies the x-coordinate of the cursor.)
(The high-order word specifies the y-coordinate of the cursor.)
so in order to get x, you erase high-order word:
Code:
mov    ecx, [lParam]
and    ecx, 0FFFFh
    


OR can append some bits while leaving other intact
For example, to form a file size variable in 64bit env.

Code:
invoke GetFileSize, [file], ADDR sizeDQ
mov    rcx, [sizeDQ]
shl    rcx, 32
or     rcx, rax
mov    QWORD[fsize], rcx
    


XOR is handy, because it works like "switch"
lets say you want to fill a long structure in a loop, like:
any_number, 0, any_number, 0, any_number, 0, ...
Code:
xor    ecx, ecx
xor    eax, eax
@fillstruct:
    xor    eax, 12345678h ; any_number
    mov    DWORD[long_struct + ecx*4], eax
    inc    ecx
    cmp    ecx, 100
    jnz    @fillstruct
    


NOT could help to make some branchless code
lets say you want to implement something like this
if (x > 0)
y = y * 1;
else
y = y * 0;

Code:
mov    eax, [y]
mov    ecx, [x]

not    ecx
shr    ecx, 31
xor    ecx, 1
mul    ecx

mov    DWORD[y], eax
    
Post 14 Jul 2009, 22:21
View user's profile Send private message Reply with quote
azdps



Joined: 10 Jul 2009
Posts: 3
Location: Tucson, Arizona
azdps 15 Jul 2009, 04:12
thanks ramguru! interesting that you registered in 2005 and this is your first post Shocked
Post 15 Jul 2009, 04:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.