flat assembler
Message board for the users of flat assembler.
Index
> Windows > bitewise operations |
Author |
|
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.
|
|||
14 Jul 2009, 17:41 |
|
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? |
|||
14 Jul 2009, 18:08 |
|
revolution 14 Jul 2009, 18:12
16 operations? With NOP?
|
|||
14 Jul 2009, 18:12 |
|
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 |
|||
14 Jul 2009, 18:16 |
|
asmcoder 14 Jul 2009, 18:26
[content deleted]
Last edited by asmcoder on 14 Aug 2009, 14:48; edited 1 time in total |
|||
14 Jul 2009, 18:26 |
|
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? Perhaps he did mean NOR - but this way we've got the joke of the day. |
|||
14 Jul 2009, 18:39 |
|
windwakr 14 Jul 2009, 18:58
|
|||
14 Jul 2009, 18:58 |
|
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 |
|||
14 Jul 2009, 19:58 |
|
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 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.... |
|||
14 Jul 2009, 20:17 |
|
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.
|
|||
14 Jul 2009, 20:28 |
|
Fanael 14 Jul 2009, 20:50
Maximum possible instruction length? 15 bytes, isn't it?
|
|||
14 Jul 2009, 20:50 |
|
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
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. |
|||
14 Jul 2009, 21:14 |
|
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).
|
|||
14 Jul 2009, 21:26 |
|
windwakr 14 Jul 2009, 21:52
Is this the thread? http://board.flatassembler.net/topic.php?t=10099
|
|||
14 Jul 2009, 21:52 |
|
LocoDelAssembly 14 Jul 2009, 22:02
windwakr, exactly. So, a swap between #UD and #GP in my post above is required.
|
|||
14 Jul 2009, 22:02 |
|
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 |
|||
14 Jul 2009, 22:21 |
|
azdps 15 Jul 2009, 04:12
thanks ramguru! interesting that you registered in 2005 and this is your first post
|
|||
15 Jul 2009, 04:12 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.