-
Notifications
You must be signed in to change notification settings - Fork 20
/
0x09.asm
41 lines (39 loc) · 1.06 KB
/
0x09.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
;
; $Id: 0x09.asm,v 1.1.1.1 2016/03/27 08:40:12 raptor Exp $
;
; 0x09 explanation - from xchg rax,rax by [email protected]
; Copyright (c) 2016 Marco Ivaldi <[email protected]>
;
; For values of rax greater than or equal to zero, this
; snippet performs an Euclidean (integer) division by 8
; of the content of rax and then adds the content of the
; carry flag (cf) to the result. Such flag is set with
; the value of the last bit that was shifted out: in this
; case, the third least-significant bit (0b100). This
; means that 1 is added for odd values of (int)rax / 4.
;
; In other words, (int)(rax + 4) / 8 is computed and the
; result is rounded to the nearest integer.
;
; The following table illustrates sample inputs and
; outputs, along with the status of the cf flag:
;
; input output cf
; 0-3 0 0
; 4-7 1 1
; 8-11 1 0
; 12-15 2 1
; 16-19 2 0
; 20-23 3 1
; 24-27 3 0
; 28-31 4 1
; 32-35 4 0
; 36-39 5 1
; 40-43 5 0
;
BITS 64
SECTION .text
global main
main:
shr rax,3 ; rax = (int)rax / 8; cf = 1 if 3rd bit is 1
adc rax,0 ; rax = rax + cf