-
Notifications
You must be signed in to change notification settings - Fork 20
/
0x08.asm
53 lines (51 loc) · 1.62 KB
/
0x08.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
;
; $Id: 0x08.asm,v 1.1.1.1 2016/03/27 08:40:12 raptor Exp $
;
; 0x08 explanation - from xchg rax,rax by [email protected]
; Copyright (c) 2016 Marco Ivaldi <[email protected]>
;
; For positive values of rax and rdx, this snippet performs
; an Euclidean (integer) division by two of the sum of the
; rax and rdx registers. In C:
;
; rax = rax + rdx;
; rax = (int)rax / 2;
;
; In other words, it computes the average of two positive
; numbers.
;
; This also works when both rax and rdx have a negative
; value, because of the clever use of the rcr instruction
; (right rotate with carry) that uses the carry flag (cf):
; it shifts cf into the most-significant bit and shifts
; the least-significant bit into cf. When one of the input
; values is positive and the other is negative and their
; sum is also negative, however, we get another result:
;
; $ ./rappel
; > mov rax,1
; > mov rdx,-2
; rax: 0x0000000000000001 rbx: 0x0000000000000000
; rcx: 0x0000000000000000 rdx: 0xfffffffffffffffe
; > add rax,rdx
; rax: 0xffffffffffffffff rbx: 0x0000000000000000
; rcx: 0x0000000000000000 rdx: 0xfffffffffffffffe
; flags: 0x0000000000000286 [cf:0, zf:0, of:0, sf:1, pf:0, af:0]
; > rcr rax,1
; rax: 0x7fffffffffffffff rbx: 0x0000000000000000
; rcx: 0x0000000000000000 rdx: 0xfffffffffffffffe
; flags: 0x0000000000000a87 [cf:1, zf:0, of:0, sf:1, pf:0, af:0]
;
; For this reason, this explanation might be incomplete.
;
; This analysis was facilitated by the assembly REPL rappel
; by [email protected]:
;
; https://github.com/yrp604/rappel/
;
BITS 64
SECTION .text
global main
main:
add rax,rdx ; rax = rax + rdx
rcr rax,1 ; right rotate with carry