-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoroushpoor-asha-610397170-calculator.asm
436 lines (355 loc) · 8.41 KB
/
Soroushpoor-asha-610397170-calculator.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
;! simple calculator with parser and everything :)
;! input & to exit
%include "in_out.asm"
section .data
Msg db 'Hello World', 0
ans dq 0
negone dq -1
section .bss
s resq 1;start of s
sE resq 1;s End
neflg resb 1 ;just a negation flag
section .text
global _start
;psuedocode
;eval(*s, *sEnd){
; if s = number or s = ans: number
; if s has (): eval(a)?evalb
; if s has + or -: evala +- evalb
; if s has * or /: evala */ evalb
atoi:;converts the string from rsi to rdi to a number and return it in rax
enter 0,0
push rsi
push rdi
push rdx
push rbx; we use this as negation flag
mov rsi, [rbp+16];*s
mov rdi, [rbp+24];*send
mov bl,0
xor rdx,rdx
xor rax,rax
mov rax, [rsi]
cmp al, '-'
JE atoineg; in case that we have -int we don't jump and go one cell further
cmp al, '+'
JE atoipos
JMP atoiloop1
atoineg:
sub rsi, 8
mov bl,1
JMP atoiloop1
atoipos:
sub rsi, 8
JMP atoiloop1
atoiloop1:
STD
lodsq
; call putc
sub rax,0x30
imul rdx, 10
add rdx, rax
xor rax, rax
cmp rsi, rdi
JGE atoiloop1
atoiendloop1:
mov rax,rdx
cmp bl,0
JE atoiexit
neg rax
atoiexit:
pop rbx
pop rdx
pop rdi
pop rsi
leave
ret 16
calculatesubfuncsubadd:;updates rbx based on flags and r9
;dh = 0 means + and dh = 1 means -
push rdx
push rax
cmp dh,0
JE calculatesubfuncsubadd1
cmp dh, 1
JE calculatesubfuncsubadd2
JMP calculatesubfuncsubaddend
calculatesubfuncsubadd1:
add rbx, r9
mov r9,1
JMP calculatesubfuncsubaddend
calculatesubfuncsubadd2:
sub rbx, r9
mov r9,1
JMP calculatesubfuncsubaddend
calculatesubfuncsubaddend:
pop rax
pop rdx
mov dl,0;reset mult to *
ret
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
calculatesubfuncmuldiv:;updates r9 based on flags and rax
;dl = 0 means * and dl = 1 means /
push rdx
push rax
cmp dl,0
JE calculatesubfuncmuldiv1
cmp dl,1
JE calculatesubfuncmuldiv2
JMP calculatesubfuncmuldivend
calculatesubfuncmuldiv1:;r9 = r9*rax
imul r9
mov r9,rax
JMP calculatesubfuncmuldivend
calculatesubfuncmuldiv2:;t9 = r9/rax
mov rdx, r9
mov r9, rax
mov rax, rdx
xor rdx,rdx
cmp rax,0
cmovl rdx,[negone]
idiv r9
mov r9, rax
JMP calculatesubfuncmuldivend
calculatesubfuncmuldivend:
pop rax
pop rdx
ret
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
calculate:
;first version called eval was buggy and hard to use so here we are writing new one
;there are 2 passing variables *a and *aend calculates value from *a to *aend and returns it as int in rax
;rbx contains our result we call it res
;here is algorithm := calculated operation1 nexttoken operation2 therest
;operation2 = * or / r9 is calculated
;next toke is ( we call calculate recursively
;otherwise we add or sub
;pseudocode
;while(i<j)
;x=i
;for s[i]!= op
;else tokenize(s[x>i])
;r9 * / token based on flag (part 1)
;if nextop = + or - or end of line:rbx +- r9, r9=1
enter 8,0
push rsi; we use this register as beginning pointer
push rdi; we use this register as end pointer
push rbx; temperory result holder
push rdx; we use dh and dl as flags
;dl = 0 means * and dl = 1 means /
;dh = 0 means + and dh = 1 means -
push r9;temperory holder
push r8;counter for paranthesis
xor rbx, rbx;default of result is 0
xor rdx, rdx;default is + and *
mov r9,1;default of multiplication is 1
mov rsi, [rbp+16];*s
mov rdi, [rbp+24];*send
mov [rbp-8], rsi; we use local variable to store position of last parsed location
mov rax, [rsi]
cmp al, '-'
je calculatefirstneg
cmp al, '+'
je calculatefirstplus
JMP calculatewhile
calculatefirstneg:
sub rsi, 8; we just skip first -
mov [rbp-8], rsi
mov r9,-1;and then we put -1 in r9
JMP calculatewhile
calculatefirstplus:
sub rsi, 8; we just skip first -
mov [rbp-8], rsi
JMP calculatewhile
calculatewhile:;eq while loop in pseudocode
cmp rsi, rdi
JB calculateend;exit if we reach the end
; mov [rbp-8], rsi; x=i
; push rsi
calculatefor:;eq for loop in pseudocode
STD
lodsq
cmp al, '('
JE calculateforendpara
cmp al, '+'
JE calculateforendadd
cmp al, '-'
JE calculateforendsub
cmp al, '*'
JE calculateforendmul
cmp al, '/'
JE calculateforenddiv
cmp al,'a';a is only for ans
JE calculateforendans
cmp rsi, rdi
JB calculateforend; we are at the end of line
JMP calculatefor
calculateforend:
add rsi, 8
push rsi
mov rsi,[rbp-8]
; push rdi
push rsi
call atoi;we have last result in rax
calculateforendend:
call calculatesubfuncmuldiv
call calculatesubfuncsubadd
JMP calculateend
calculateforendmul:
push rsi
add rsi, 16
push rsi;rsi is next rdi
mov rsi,[rbp-8]
push rsi
call atoi;we have last result in rax
call calculatesubfuncmuldiv
pop rsi
mov [rbp-8],rsi
mov dl,0
JMP calculatefor
calculateforenddiv:
push rsi
add rsi, 16
push rsi;rsi is next rdi
mov rsi,[rbp-8]
push rsi
call atoi;we have last result in rax
call calculatesubfuncmuldiv
pop rsi
mov [rbp-8],rsi
mov dl,1
JMP calculatefor
calculateforendadd:
push rsi
add rsi, 16
push rsi;rsi is next rdi
mov rsi,[rbp-8]
push rsi
call atoi;we have last result in rax
call calculatesubfuncmuldiv
call calculatesubfuncsubadd
pop rsi
mov [rbp-8],rsi
mov dh,0
JMP calculatefor
calculateforendsub:
push rsi
add rsi, 16
push rsi;rsi is next rdi
mov rsi,[rbp-8]
push rsi
call atoi;we have last result in rax
call calculatesubfuncmuldiv
call calculatesubfuncsubadd
pop rsi
mov [rbp-8],rsi
mov dh,1
JMP calculatefor
calculateforendans:
sub rsi,16
push rsi
mov rax,[ans]
JMP calculatewithoutatioi
calculateforendpara:;we move until matched paranthesis and call the function recusrsively
mov r8,1; r8 is our counter
; push rsi;we need to pop later
calculateforendparafor:
cmp r8,0
JE calculateforendparaforend
STD
lodsq
cmp al, '('
JE calculateforendparafor1
cmp al, ')'
JE calculateforendparafor2
JMP calculateforendparafor
calculateforendparafor1:
inc r8
JMP calculateforendparafor
calculateforendparafor2:
dec r8
JMP calculateforendparafor
calculateforendparaforend:
push rsi
add rsi, 16
push rsi;*aend for recurive
mov rsi, [rbp-8]; int para case local var is start of expression
;*a
sub rsi, 8; local var is at first par
push rsi
call calculate;we have result in paranthesis in rax
; call writeNum
; call newLine
calculatewithoutatioi:;i realized i can use the code from down here for ans
pop rsi
cmp rsi, rdi
JB calculateforendend; we are at the end of line
call calculatesubfuncmuldiv
; sub rsi,16
STD
lodsq
;changing flags based on next sign
mov dl,0
cmp al, '*'
; cmove dl,0
cmp al, '/'
; cmove dl,1
sete dl
cmp al, '+'
JE calculateforendparaforendadd
cmp al, '-'
JE calculateforendparaforendsub
JMP calculateforendparaforendend
calculateforendparaforendadd:
call calculatesubfuncsubadd
mov dh,0
JMP calculateforendparaforendend
calculateforendparaforendsub:
call calculatesubfuncsubadd
mov dh,1
JMP calculateforendparaforendend
calculateforendparaforendend:
mov [rbp-8], rsi
JMP calculatefor
calculatewhileend:
calculateend:
mov rax,rbx
pop r8
pop r9
pop rdx
pop rbx
pop rdi
pop rsi
leave
ret 16
_start:
; mov qword [ans],10
mov rax, rsp
sub rax, 8
mov [s], rax ;saving top of stack to [s] we store our string in stack
mainwhile:
mov rsp, [s]
add rsp,8
getString:; gets an string and put it in stack
call getc
cmp al, 0xA
JE getStringEnd
cmp al, '&'
JE Exit
push rax
JMP getString
getStringEnd:
mov rsi, [s];start of string
mov [sE], rsp
mov rdi, rsp;end of string
push rdi
push rsi
; call atoi
call calculate
mov [ans],rax
call writeNum
call newLine
JMP mainwhile
Exit:
mov rax, 1
mov rbx, 0
; push rbx
int 0x80