-
Notifications
You must be signed in to change notification settings - Fork 0
/
mult.asm
366 lines (309 loc) · 10.7 KB
/
mult.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
section .text
global _start
_start:
sub rsp, 6 * 128 * 8
mov rcx, 256
lea rdi, [rsp + 4 * 128 * 8]
call set_zero
lea rdi, [rsp + 2 * 128 * 8]
call set_zero
lea rdi, [rsp + 128 * 8]
lea r8, [rsp + 2 * 128 * 8]
lea r9, [rsp + 4 * 128 * 8]
mov rcx, 128
call read_long
mov rdi, rsp
call read_long
lea rsi, [rsp + 128 * 8]
call mul_long_long
mov rdi, r9
call write_long
mov al, 0x0a
call write_char
jmp exit
mul_long_long:
push r8
push r9
push rdi
push rsi
push rcx
clc
mov r13, rcx
mov r11, r8
mov r15, rdi
.loop1:
mov r10, rcx
mov rbx, [rsi]
.loop:
mov r12, [rdi]
mov [r8], r12
lea r8, [r8 + 8]
lea rdi, [rdi + 8]
dec r10
jnz .loop
push rsi
mov r8, r11
mov rdi, r8
call mul_long_short
mov rdi, r9
mov rsi, r8
call add_long_long
mov rdi, r15
pop rsi
lea r9, [r9 + 8]
lea rsi, [rsi + 8]
dec r13
jnz .loop1
pop rcx
pop rsi
pop rdi
pop r9
pop r8
ret
; adds two long number
; rdi -- address of summand #1 (long number)
; rsi -- address of summand #2 (long number)
; rcx -- length of long numbers in qwords
; result:
; sum is written to rdi
add_long_long:
push rdi
push rsi
push rcx
clc
.loop:
mov rax, [rsi]
lea rsi, [rsi + 8]
adc [rdi], rax
lea rdi, [rdi + 8]
dec rcx
jnz .loop
pop rcx
pop rsi
pop rdi
ret
; adds 64-bit number to long number
; rdi -- address of summand #1 (long number)
; rax -- summand #2 (64-bit unsigned)
; rcx -- length of long number in qwords
; result:
; sum is written to rdi
add_long_short:
push rdi
push rcx
push rdx
xor rdx,rdx
.loop:
add [rdi], rax
adc rdx, 0
mov rax, rdx
xor rdx, rdx
add rdi, 8
dec rcx
jnz .loop
pop rdx
pop rcx
pop rdi
ret
; multiplies long number by a short
; rdi -- address of multiplier #1 (long number)
; rbx -- multiplier #2 (64-bit unsigned)
; rcx -- length of long number in qwords
; result:
; product is written to rdi
mul_long_short:
push rax
push rdi
push rcx
push rsi
xor rsi, rsi
.loop:
mov rax, [rdi]
mul rbx
add rax, rsi
adc rdx, 0
mov [rdi], rax
add rdi, 8
mov rsi, rdx
dec rcx
jnz .loop
pop rsi
pop rcx
pop rdi
pop rax
ret
; divides long number by a short
; rdi -- address of dividend (long number)
; rbx -- divisor (64-bit unsigned)
; rcx -- length of long number in qwords
; result:
; quotient is written to rdi
; rdx -- remainder
div_long_short:
push rdi
push rax
push rcx
lea rdi, [rdi + 8 * rcx - 8]
xor rdx, rdx
.loop:
mov rax, [rdi]
div rbx
mov [rdi], rax
sub rdi, 8
dec rcx
jnz .loop
pop rcx
pop rax
pop rdi
ret
; assigns a zero to long number
; rdi -- argument (long number)
; rcx -- length of long number in qwords
set_zero:
push rax
push rdi
push rcx
xor rax, rax
rep stosq
pop rcx
pop rdi
pop rax
ret
; checks if a long number is a zero
; rdi -- argument (long number)
; rcx -- length of long number in qwords
; result:
; ZF=1 if zero
is_zero:
push rax
push rdi
push rcx
xor rax, rax
rep scasq
pop rcx
pop rdi
pop rax
ret
; read long number from stdin
; rdi -- location for output (long number)
; rcx -- length of long number in qwords
read_long:
push rcx
push rdi
call set_zero
.loop:
call read_char
or rax, rax
js exit
cmp rax, 0x0a
je .done
cmp rax, '0'
jb .invalid_char
cmp rax, '9'
ja .invalid_char
sub rax, '0'
mov rbx, 10
call mul_long_short
call add_long_short
jmp .loop
.done:
pop rdi
pop rcx
ret
.invalid_char:
mov rsi, invalid_char_msg
mov rdx, invalid_char_msg_size
call print_string
call write_char
mov al, 0x0a
call write_char
.skip_loop:
call read_char
or rax, rax
js exit
cmp rax, 0x0a
je exit
jmp .skip_loop
; write long number to stdout
; rdi -- argument (long number)
; rcx -- length of long number in qwords
write_long:
push rax
push rcx
mov rax, 20
mul rcx
mov rbp, rsp
sub rsp, rax
mov rsi, rbp
.loop:
mov rbx, 10
call div_long_short
add rdx, '0'
dec rsi
mov [rsi], dl
call is_zero
jnz .loop
mov rdx, rbp
sub rdx, rsi
call print_string
mov rsp, rbp
pop rcx
pop rax
ret
; read one char from stdin
; result:
; rax == -1 if error occurs
; rax \in [0; 255] if OK
read_char:
push rcx
push rdi
sub rsp, 1
xor rax, rax
xor rdi, rdi
mov rsi, rsp
mov rdx, 1
syscall
cmp rax, 1
jne .error
xor rax, rax
mov al, [rsp]
add rsp, 1
pop rdi
pop rcx
ret
.error:
mov rax, -1
add rsp, 1
pop rdi
pop rcx
ret
; write one char to stdout, errors are ignored
; al -- char
write_char:
sub rsp, 1
mov [rsp], al
mov rax, 1
mov rdi, 1
mov rsi, rsp
mov rdx, 1
syscall
add rsp, 1
ret
exit:
mov rax, 60
xor rdi, rdi
syscall
; print string to stdout
; rsi -- string
; rdx -- size
print_string:
push rax
mov rax, 1
mov rdi, 1
syscall
pop rax
ret
section .rodata
invalid_char_msg:
db "Invalid character: "
invalid_char_msg_size: equ $ - invalid_char_msg