-
Notifications
You must be signed in to change notification settings - Fork 2
/
bit_strings.asm
140 lines (126 loc) · 2.4 KB
/
bit_strings.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
; CSES Bit Strings Fastest Submission as of 12/28/2020
section .bss
buffer: resb 64
outbuff: resb 64
N: resb 8
a: resb 8
echar: resb 8
currpos: resb 8
outlen: resb 8
section .text
atoi:
xor rax, rax
xor rcx, rcx
.pre:
mov cl, [rdx]
inc rdx
cmp rcx, '0'
jb .pre
cmp rcx, '9'
ja .pre
dec rdx
.compute:
mov cl, [rdx]
inc rdx
cmp rcx, '0'
jb .done
cmp rcx, '9'
ja .done
sub rcx, '0'
imul rax, 10
add rax, rcx
jmp .compute
.done:
ret
itoa: ; rax = num, echar = end char
xor rbx, rbx
.pre:
xor rdx, rdx
mov rcx, 10
div rcx
push rdx
inc rbx
test rax, rax
jnz .pre
mov rcx, rbx
mov rdx, [currpos]
.compute:
pop rax
add rax, '0'
mov [rdx], rax
inc rdx
dec rcx
cmp rcx, 0
je .done
jmp .compute
.done:
mov rax, [echar]
mov [rdx], rax
mov rcx, [outlen]
add rcx, rbx
inc rcx
inc rdx
mov [outlen], rcx
mov [currpos], rdx
ret
print: ; print whatever is in buffer
mov rdx, [outlen]
mov rax, 1
mov rdi, 1
mov rsi, outbuff
syscall
ret
global _start
_start:
; read input (CSES reads entire input not just one line !!!)
mov rax, 0
mov rdi, 0
mov rsi, buffer
mov rdx, 64
syscall
; get N
mov rdx, buffer
call atoi
mov [N], rax
mov rbx, 2
mov [a], rbx
mov rax, 1
xor rcx, rcx
; get sum of numbers given
.solve:
cmp rcx, 20
je .done
mov rbx, 1
shl rbx, cl
mov rdx, [N]
and rdx, rbx
mov rbx, 1000000007
cmp rdx, 0
je .mulmod
push rcx
mov rcx, [a]
imul rcx
xor rdx, rdx
idiv rbx
mov rax, rdx
pop rcx
.mulmod:
push rax
mov rax, [a]
imul rax
xor rdx, rdx
idiv rbx
mov [a], rdx
pop rax
inc rcx
jmp .solve
.done:
mov rbx, outbuff
mov [currpos], rbx
mov rbx, 10
mov [echar], rbx
call itoa
call print
mov rax, 60
mov rdi, 0
syscall