flat assembler
Message board for the users of flat assembler.
![]() Goto page 1, 2 Next |
Author |
|
MacroZ 20 Oct 2018, 18:49
It can be used to flip bits
Code: xor al,1 ; Flips the first bit Can we get another unique xor usage/trick by the next person? We want this thread to grow larger with unique answers. Forum users are encouraged to participate! _________________ The king auto-generates two things to degrade dangerous artists and intellectuals. The King reserves the right to be king and has made the culprits in advance. Half of the time or more, they are auto-generated. |
|||
![]() |
|
HaHaAnonymous 20 Dec 2018, 12:03
MacroZ wrote: It can be used to flip bits... This is true. Nice find! Code: mov eax,1011b ; eax now is 1011b xor eax,1000b ; eax now is 0011b xor eax,1000b ; eax now is 1011b xor eax,0001b ; eax now is 1010b ret Thank you! |
|||
![]() |
|
a 10 Apr 2025, 17:24
MacroZ wrote: Can we get another unique xor usage/trick by the next person? We want this thread to grow larger xor can be used to find a difference between 2 registers: mov eax, 10000001b mov ebx, 10000010b xor eax,ebx result: 00000011b, it shows us that the bits 1 and 2 are different from eachother. You can use it to compare flags and you can also get a position to WHERE it is different you can use bsf ecx,eax instruction to get the first bit position into ecx and if you want to get the next position then remove that bit with btr eax,ecx (ecx already contains a position to remove) after that you can detect which next bit is different by using 2 above instructions in a loop |
|||
![]() |
|
Picnic 17 Apr 2025, 23:04
MacroZ wrote: It can be used to flip bits Some vintage XOR wizardry found in the legendary FASM forum over the years. 😎 Code: ; Swap two registers xor eax, ebx xor ebx, eax xor eax, ebx Code: ; Equality comparison mov eax, [value1] mov ebx, [value2] xor eax, ebx jz equal_label Code: ; Encrypt / Decrypt (XOR cipher) mov al, 'A' ; character to be encrypted mov bl, 0x5A ; encryption key xor al, bl ; encryption step (AL now holds encrypted value) ; ... ; ... xor al, bl ; decryption step (AL returns to original 'A') Code: ; ASCII character case manipulation and al, 0DFh ; convert to uppercase or al, 20h ; convert to lowercase xor al, 20h ; toggle case Code: ; XOR Bitwise NOT trick not eax ; same as xor eax, -1 |
|||
![]() |
|
Mat-Quasar 18 Apr 2025, 08:56
Picnic wrote:
Nice info. https://www.felixcloutier.com/x86/xor wrote: Performs a bitwise exclusive OR (XOR) operation on the destination (first) and source (second) operands and stores the result in the destination operand location. |
|||
![]() |
|
AsmGuru62 18 Apr 2025, 13:36
These cases are wonderful from the artistic point of view.
But, in real code --- would you just not use XCHG or CMP? |
|||
![]() |
|
Furs 18 Apr 2025, 14:53
You can store 2 values into 1 with xor, and then use one of them to retrieve the other (but it can be either, unlike if you stored directly).
Code: ; we have 2 values a and b, simple example mov eax, [a] xor eax, [b] mov [var], eax Now later if you have either a or b (not both but one of them), you can obtain the other from the var with xor: Code: mov eax, [var] xor eax, [a] ; eax = b mov eax, [var] xor eax, [b] ; eax = a But it will be a superior single linked list, since you can go either forward or backward while using the same amount of memory per node (only 1 pointer). |
|||
![]() |
|
macomics 18 Apr 2025, 23:39
Furs wrote: But it will be a superior single linked list, since you can go either forward or backward while using the same amount of memory per node (only 1 pointer). Is that all? You further evolve the idea. For a 64-bit system, the pointer size is 8 bytes. Then, even if we switch to a double linked list with a single pointer, we will still 8 bytes to store these values. But the XOR operation does something else useful for such a list. Let's say it is known that the pointer values for the elements of a double linked list are selected from a range not exceeding 64 kb. What then will be stored in the 8 bytes of the pointer? If XOR allows you to find all the different bits, then 0 will always be written in the top 6 bytes of the pointer because the address range at this location will always match. Then why use 8 bytes in such a list to store pointers, where the highest 6 bytes will always be 0? This way, you can create a double linked list and not store the full pointer, but only the low-order bits. And let the 64 kb example be just a special case, but in general, the heap size in Windows is limited to 2 GB. Then 31-bits will always be enough to store pointers in such a list. Even in a 64-bit program. |
|||
![]() |
|
revolution 19 Apr 2025, 02:21
And if the list elements are aligned and of even (or other power-of-two) length then the lower bits can also be elided since they are always zero.
|
|||
![]() |
|
revolution 19 Apr 2025, 04:18
XOR is used in PRNGs based upon LFSRs.
XOR is used in CRC/BCH/ECC algorithms for data integrity checking. |
|||
![]() |
|
macomics 19 Apr 2025, 10:16
revolution wrote: And if the list elements are aligned and of even (or other power-of-two) length then the lower bits can also be elided since they are always zero. But unlike the reduction of the higher digits, this operation will require more instructions. Whereas when the high-order digits are reduced, the XOR instruction remains the same. Code: ; rax - previous element pointer ; rbx - current element pointer xor ax, [rbx] ; compute next element pointer xchg rax, rbx ; walk ;----------------------------------------------------- ; rax - previous element pointer ; rbx - current element pointer movzx rdx, word [rbx] ; get pointer imul rdx, rdx, alignment ; ... xor rax, rdx ; compute next element pointer xchg rax, rbx ; walk |
|||
![]() |
|
Picnic 19 Apr 2025, 13:00
AsmGuru62 wrote: These cases are wonderful from the artistic point of view. Those tricks aren't always the most readable or practical choice for everyday development, but they're definitely fun to study and play around with. Just found another one buried in my notes. Code: ; if a <> 0 then a = b else a = c cmp eax, 1 sbb eax, eax xor ecx, ebx and eax, ecx xor eax, ebx |
|||
![]() |
|
macomics 19 Apr 2025, 13:22
Picnic wrote: Just found another one buried in my notes. 1) Ruins the value of C 2) a <> 1 3) Code: cmp eax, 1 cmovnz eax, ebx cmovz eax, ecx |
|||
![]() |
|
Picnic 19 Apr 2025, 13:25
macomics wrote:
Yes, only ebx remains unchanged. macomics wrote: cmp eax, 1 Nice, but not XOR here. |
|||
![]() |
|
macomics 19 Apr 2025, 13:32
Code: ; if a <> 0 then a = b else a = c cmp eax, 1 sbb eax, eax xor ecx, ebx and eax, ecx xor eax, ebx xor ecx, ebx ; just not the full code |
|||
![]() |
|
revolution 19 Apr 2025, 13:33
cmovcc is not available with XMM stuff. So those xor "tricks" are still valid there.
Also, not every CPU has xchg, x86 does, but ARM doesn't. |
|||
![]() |
|
Picnic 19 Apr 2025, 13:39
macomics wrote:
Sweet! That extra line really makes a difference! |
|||
![]() |
|
AsmGuru62 19 Apr 2025, 14:57
Hmmm... if only I cared about ARM...
|
|||
![]() |
|
a 19 Apr 2025, 19:13
2. xor can be used to heal cancer 3. xor can be used to time travel |
|||
![]() |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.