-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_mul.asm
361 lines (302 loc) · 10.9 KB
/
my_mul.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
section .text
global _start
_start:
sub rsp, 4 * 128 * 8 ; allocate memory for 2 long numbers and answer
lea rdi, [rsp + 2 * 128 * 8]; set the pointer to the beginning of the answer
mov rcx, 2 * 128 ; size of answer can be up to 2 times greater than size of input numbers
call set_zero ; get rid of any trash in this part of memory
lea r9, [rsp + 2 * 128 * 8] ; put answer register to needed place
mov rcx, 128 ; max size of input numbers in bytes
lea rdi, [rsp + 128 * 8] ; prepare for reading
call read_long ; read input #1
mov rdi, rsp ; prepare for reading
call read_long ; read input #2
mov rsi, rdi ; move rsi to the beginning of the second input number
lea rdi, [rsp + 128 * 8] ; move rdi to the beginning of the first input number
call mul_long_long ; do the multiplication
mov rcx, 2 * 128 ; prepare for writing
call write_long ; write the answer
mov al, 0x0a
call write_char ; write the new line symbol
jmp exit
; multiplies two long numbers
; rdi -- address of multiplier #1 (long number)
; rsi -- address of multiplier #2 (long number)
; rcx -- length of long number in qwords
; result:
; product is written to r9
mul_long_long:
push rsi
push rcx
mov r14, rcx ; start value of counter
xor r13, r13 ; rdi offset
.outer_loop:
xor r10, r10 ; will be used to save carry
xor r11, r11 ; rsi offset
mov rbp, r14 ; restart the counter for inner loop
clc
.inner_loop:
mov rax, [rsi + r11] ; get a piece of rsi
mov rbx, [rdi + r13] ; get a piece of rdi
mul rbx ; mul rax and rbx, result in rax; overflow in rdx
add rax, r10 ; add carry from prev circle
adc rdx, 0 ; recalc new carry
lea r15, [r13 + r11] ; result offset
add [r9 + r15], rax ; count result
adc rdx, 0 ; recalc new carry again
mov r10, rdx ; save new carry
add r11, 8 ; recalc rsi offset
dec rbp ; recalc inner loop counter
jnz .inner_loop ; go to next circle
add r13, 8 ; recalc rdi offset
dec rcx ; recalc outer loop counter
jnz .outer_loop ; go to next circle
pop rcx
pop rsi
mov rdi, r9 ; move rdi to the beginning of the answer
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
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 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