-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoroushpoor-asha-610397170-reverseBit.asm
97 lines (78 loc) · 1.6 KB
/
Soroushpoor-asha-610397170-reverseBit.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%include "in_out.asm"
section .data
section .bss
section .text
global _start
printDigitBinary:; functionize for further use
;assume the number is in rax
push rbx
push rdx
mov rdx, 0x8000000000000000
printDigitBinaryloop2:
cmp rdx, 0
JE printDigitBinaryloop2continue
mov rbx, rdx;mov our bit counter to rbx
and rbx, rax
cmp rbx, 0
JE printDigitBinaryloop2printzero ;it is zero
push rax
mov rax, 0x31;print 1
call putc
pop rax
shr rdx, 1
JMP printDigitBinaryloop2
printDigitBinaryloop2printzero:
push rax
mov rax, 0x30
call putc
pop rax
shr rdx, 1
JMP printDigitBinaryloop2
printDigitBinaryloop2continue:
pop rdx
pop rbx
ret
printBinary:
ret
reverseBit:
;psudocode :
;count = bitcount(num)
;num >>= 1
;while (num) {
; reverse_num <<= 1
; reverse_num |= num & 1
; num >>= 1
; count--
; }
;reverse_num <<= count
push rcx
push rdx
push r9
mov rcx, 63
mov rdx, rax; it works same as rdi = rax & 1 in this case but because we shift all bits to the left this is faster
shr rax, 1
reverseBitloop1:
shl rdx, 1;reverse_num <<= 1
mov r9, rax;reverse_num |= num & 1
and r9, 1
or rdx, r9
shr rax, 1;num >>= 1
dec rcx;count--
cmp rax, 0
JNE reverseBitloop1
reverseBitloop2:
shl rdx, 1;reverse_num <<= count
loop reverseBitloop2
mov rax, rdx
pop r9
pop rdx
pop rcx
ret
_start:
call readNum
call reverseBit
call printDigitBinary
Exit:
mov rax, 1
mov rbx, 0
int 0x80