-
Notifications
You must be signed in to change notification settings - Fork 20
/
0x36.asm
68 lines (58 loc) · 1.37 KB
/
0x36.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
68
;
; $Id: 0x36.asm,v 1.1.1.1 2016/03/27 08:40:13 raptor Exp $
;
; 0x36 explanation - from xchg rax,rax by [email protected]
; Copyright (c) 2016 Marco Ivaldi <[email protected]>
;
; For positive integer values of rax, this snippet
; calculates the next power of two, using a pretty cool
; technique.
;
; This analysis was facilitated by the assembly REPL rappel
; by [email protected]:
;
; https://github.com/yrp604/rappel/
;
; Example:
; $ gdb 0x36
; [...]
; (gdb) b*0x0000000000400636
; (gdb) r <- rax = 1027
; Breakpoint 1, 0x0000000000400636 in main ()
; (gdb) i r rax
; rax 0x800 2048
; (gdb) load
; (gdb) r <- rax = 2049
; Breakpoint 1, 0x0000000000400636 in main ()
; (gdb) i r rax
; rax 0x1000 4096
; (gdb) load
; (gdb) r <- rax = 0x11111111
; Breakpoint 1, 0x0000000000400636 in main ()
; (gdb) i r rax
; rax 0x20000000 536870912
;
BITS 64
SECTION .text
global main
main:
dec rax ; rax = rax - 1
mov rdx,rax ;
shr rdx,0x1 ;
or rax,rdx ; rax = (rax) | ((rax)>>1)
mov rdx,rax ;
shr rdx,0x2 ;
or rax,rdx ; rax = (rax) | ((rax)>>2)
mov rdx,rax ;
shr rdx,0x4 ;
or rax,rdx ; rax = (rax) | ((rax)>>4)
mov rdx,rax ;
shr rdx,0x8 ;
or rax,rdx ; rax = (rax) | ((rax)>>8)
mov rdx,rax ;
shr rdx,0x10 ;
or rax,rdx ; rax = (rax) | ((rax)>>16)
mov rdx,rax ;
shr rdx,0x20 ;
or rax,rdx ; rax = (rax) | ((rax)>>32)
inc rax ;