-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path0x3e_TODO.asm
67 lines (63 loc) · 1.34 KB
/
0x3e_TODO.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;
; $Id: 0x3e_TODO.asm,v 1.1.1.1 2016/03/27 08:40:13 raptor Exp $
;
; 0x3e explanation - from xchg rax,rax by xorpd@xorpd.net
; Copyright (c) 2016 Marco Ivaldi <raptor@0xdeadbeef.info>
;
; TODO: THIS EXPLANATION IS INCOMPLETE
;
; This snippet is roughly equivalent to the following C
; code:
;
; #include <stdio.h>
; int number_of_ones(unsigned int x)
; {
; int total_ones = 0;
; while (x != 0) {
; x = x & (x-1);
; total_ones++;
; }
; return total_ones;
; }
; main()
; {
; int in, out;
; for (in = 0; in < 30; in++) {
; out = number_of_ones(in ^ (in>>1)) & 3;
; printf("in:\t%d\t\tout:\t%d\n", in, out);
; }
; }
;
; Example run:
; $ ./0x3e_helper
; in: 0 out: 0
; in: 1 out: 1
; in: 2 out: 2
; in: 3 out: 1
; in: 4 out: 2
; in: 5 out: 3
; in: 6 out: 2
; in: 7 out: 1
; in: 8 out: 2
; in: 9 out: 3
; [...]
; in: 25 out: 3
; in: 26 out: 0
; in: 27 out: 3
; in: 28 out: 2
; in: 29 out: 3
;
; Once again, its ultimate purpose is not clear to me.
;
; NOTE. @AlexAltea points out that this snippet computes the
; direction of the lines in the (Heighway) Dragon Curve (!).
;
BITS 64
SECTION .text
global main
main:
mov rdx,rax ;
shr rdx,1 ;
xor rax,rdx ;
popcnt rax,rax ;
and rax,0x3 ; rax = number_of_ones(rax ^ (rax>>1)) & 3