1
+ %include "utils/printf32.asm"
2
+ section .data
3
+ delim db " " , 0
4
+
5
+ section .bss
6
+ root resd 1
7
+ currentNode resd 1
8
+ isOperator resd 1
9
+ isNegativeAtoi resd 1
10
+
11
+ section .text
12
+
13
+ extern evaluate_ast
14
+ extern create_node
15
+ extern printf
16
+ global create_tree
17
+ global iocla_atoi
18
+
19
+
20
+ iocla_atoi: ;change string to number
21
+ enter 0 , 0
22
+ push ebx
23
+ push ecx
24
+ push edx
25
+
26
+ mov eax , [ ebp + 8 ]
27
+ xor ecx , ecx ;stores the result
28
+ xor ebx , ebx ;stores character
29
+
30
+ mov edx , 0
31
+ mov [ isNegativeAtoi ], edx ;the default way is that the number is positive
32
+ ;we check to see if the number is negative or not
33
+ mov bl , [ eax ]
34
+ cmp bl , '-' ;compare to minus character
35
+ jnz atoi_loop ;if it is not a minus sign just do the rest as usual
36
+ mov edx , 1
37
+ mov [ isNegativeAtoi ], edx ;change global variable
38
+
39
+ atoi_loop:
40
+ mov bl , [ eax ] ;get the character
41
+ test bl , bl ;check if we reached the end
42
+ jz end_of_atoi
43
+ cmp bl , ' '
44
+ jz end_of_atoi
45
+ cmp bl , 10
46
+ jz end_of_atoi
47
+ imul ecx , 0xa ;multiply ecx by 10
48
+ sub bl , '0'
49
+ add ecx , ebx
50
+ inc eax
51
+ jmp atoi_loop
52
+
53
+ end_of_atoi:
54
+
55
+ ;checking to see if the number was negative or not
56
+ mov eax , ecx
57
+ mov edx , [ isNegativeAtoi ]
58
+ cmp edx , 0
59
+ jz is_positive_number
60
+ ;we do the next only of the number is negative
61
+ mov eax , 0
62
+ sub eax , ecx
63
+ is_positive_number:
64
+
65
+ pop edx
66
+ pop ecx
67
+ pop ebx
68
+ leave
69
+ ret
70
+
71
+
72
+ replace_next_white_char: ;replaces next space character with '\0'
73
+ ;returns a pointer to the word after the space, or
74
+ ;a 0 if this word was the last one (in ebx)
75
+ enter 0 , 0
76
+
77
+ mov ebx , [ ebp + 8 ] ; the parameter is a string
78
+
79
+ starting_loop:
80
+ mov cl , [ ebx ]
81
+ cmp cl , ' '
82
+ jz end_of_replacement ;we have found a space, going to replace it
83
+ cmp cl , 0
84
+ jz last_word_case ;we have foudn a '\0' char, this is the last word
85
+ inc ebx ;
86
+ jmp starting_loop
87
+
88
+ end_of_replacement:
89
+ mov cl , 0
90
+ mov [ ebx ], cl ;replace the space character with a '\0'
91
+ inc ebx ;return the value after the '\0'
92
+ jmp end_of_replacement_function
93
+
94
+ last_word_case:
95
+ mov ebx , 0 ;return 0 in ebx
96
+
97
+ end_of_replacement_function:
98
+
99
+ leave
100
+ ret
101
+
102
+ check_if_operator: ;check is the symbol given parameter is an operator
103
+ ;(+,-,*,/), store the information in "isOperator"
104
+ enter 0 , 0
105
+
106
+ mov eax , [ ebp + 8 ] ;puttin into ecx the string
107
+ mov cl , [ eax ] ;putting into cl the first character of the string
108
+
109
+ cmp cl , '+'
110
+ jz is_an_operator
111
+ cmp cl , '-'
112
+ jz is_a_minus ;it could either a operator or a negative number
113
+ cmp cl , '*'
114
+ jz is_an_operator
115
+ cmp cl , '/'
116
+ jz is_an_operator
117
+ mov ebx , 0x0 ; it is not an operator
118
+ mov [ isOperator ], ebx
119
+ jmp end
120
+
121
+ is_a_minus:
122
+ mov cl , [ eax + 0x1 ] ;get the second character
123
+ cmp cl , 0 ;if the next char is '\0' is an operator
124
+ jz is_an_operator
125
+ mov ebx , 0x0 ; it is not an operator
126
+ mov [ isOperator ], ebx
127
+ jmp end
128
+
129
+ is_an_operator:
130
+ ;PRINTF32 `OPERATOR:%c:OPERATOR\n\x0`,ecx
131
+ mov ebx , 0x1
132
+ mov [ isOperator ], ebx
133
+
134
+ end:
135
+
136
+ leave
137
+ ret
138
+
139
+ create_tree:
140
+ enter 0 , 0
141
+ push ebx
142
+ push ecx
143
+ push edx
144
+
145
+ mov edx , [ ebp + 8 ]
146
+
147
+ push eax ;saving eax register
148
+ push ecx ;saving ecx register
149
+ push edx ;pushing parameter
150
+
151
+ call replace_next_white_char
152
+
153
+ pop edx ;removing paramter
154
+ pop ecx ;restoring ecx register
155
+ pop eax ;restoring eax register
156
+
157
+ ;PRINTF32 `DA:%s:DA\n\x0`,edx
158
+
159
+ ;initializing root Node
160
+ push edx ;pushing parameter
161
+ call create_node
162
+ pop edx ;restore edx and empty parameter pushed to stack
163
+
164
+ mov [ root ], eax ; ;initialize the root
165
+ mov [ currentNode ], eax ;initialize the currentNode for the first time
166
+ push eax ; ;push first node to stack
167
+
168
+ traverse_token:
169
+ cmp ebx , 0 ;the previous word was the last one
170
+ jz reached_end_token ;
171
+
172
+ mov edx , ebx ;move edx to the next word and call the function that
173
+ ;puts a '\0' tot the end of it
174
+ push eax ;saving eax register
175
+ push ecx ;saving ecx register
176
+ push edx ;pushing parameter
177
+ call replace_next_white_char
178
+ add esp , 0x4 ;removing paramter
179
+ pop ecx ;restoring ecx register
180
+ pop eax ;restoring eax register
181
+
182
+ ;PRINTF32 `DA:%s:DA\n\x0`,edx
183
+
184
+ ;push ecx
185
+ ;mov ecx, [currentNode]
186
+ ;mov ecx, [ecx]
187
+ ;PRINTF32 `INLINE:%s:INLINE\n\x0`,ecx
188
+ ;pop ecx
189
+
190
+ push ebx ;saving ebx register
191
+ push ecx ;saving ecx register
192
+ push edx ;saving edx register
193
+ push edx ;pushing parameter
194
+ call create_node
195
+ add esp , 0x4 ;removing parameter
196
+ pop edx ;restoring edx register
197
+ pop ecx ;restoring ecx register
198
+ pop ebx ;restoring ebx register
199
+
200
+ push eax ;saving eax register
201
+ push ebx ;saving ebx register
202
+ push ecx ;saving ecx register (nu e neaparat momentan)
203
+ push edx ;pushing parameter
204
+ call check_if_operator
205
+ add esp , 0x4 ;removing parameter
206
+ pop ecx ;restoring ecx register
207
+ pop ebx ;restoring ebx register
208
+ pop eax ;restoring eax register
209
+
210
+ mov ecx , [ isOperator ] ;
211
+ cmp ecx , 0x1
212
+ je things_to_do_if_operator ;it is an operator
213
+ jmp things_to_do_if_not_operator ;it is not an operator
214
+
215
+ things_to_do_if_operator:
216
+ push eax
217
+ ;now we add the eax node either to the left or right of currentNode
218
+ ;if left is null, add to left; if left is not null, add to right and change
219
+ ;currentNode to parrent node
220
+ mov ecx , [ currentNode ] ;move to ecx the node
221
+ mov ecx , [ ecx + 0x4 ] ;move to ecx the left node address
222
+ cmp ecx , 0x0 ;check if it si null
223
+ jz add_to_left_operator
224
+ jmp add_to_right_operator
225
+ ; DONE
226
+
227
+ things_to_do_if_not_operator:
228
+ ;NO need to push eax because integer values are always leafs
229
+ ;we add the eax node either to the left or right, if we add to the right
230
+ ;the currentNode is updated by poping the stack
231
+ mov ecx , [ currentNode ] ;move to ecx the node
232
+ mov ecx , [ ecx + 0x4 ] ;move to ecx the left node address
233
+ cmp ecx , 0x0 ;check if it si null
234
+ jz add_to_left_value
235
+ jmp add_to_right_value
236
+ ; DONE
237
+
238
+ end_of_operator_actions:
239
+
240
+ jmp traverse_token ;iterate for next character
241
+
242
+ add_to_left_operator:
243
+ mov ecx , [ currentNode ] ;move to ecx the node
244
+ mov [ ecx + 0x4 ], eax ;left value is updated
245
+ mov [ currentNode ], eax ;currentNode is updated to the inserted value
246
+ jmp end_of_operator_actions ;TBDT
247
+
248
+ add_to_right_operator: ;adding to the right for operators
249
+ mov ecx , [ currentNode ] ;move to ecx the node
250
+ mov [ ecx + 0x8 ], eax ;right value is updated
251
+ mov [ currentNode ], eax ;currentNode is updated to the inserted value
252
+ jmp end_of_operator_actions ;TBDT
253
+
254
+ add_to_left_value:
255
+ mov ecx , [ currentNode ] ;move to ecx the node
256
+ mov [ ecx + 0x4 ], eax ;left value is updated
257
+ jmp end_of_operator_actions ;TBDT
258
+
259
+ add_to_right_value: ;adding to the right for integer values
260
+ mov ecx , [ currentNode ] ;move to ecx the node
261
+ mov [ ecx + 0x8 ], eax ;right value is updated
262
+
263
+ ;now currentNode becomes the value in the stack that has a free right spot
264
+ ;we are going to check for ecx, and when we find the value, we stop
265
+ loop_for_currentNode:
266
+ pop ecx ;pop the parrent node of value
267
+ mov ecx , [ esp ] ;mov to ecx the future currentNode without poping
268
+ mov [ currentNode ], ecx ;update currentNode to future currentNode
269
+ ;now we test to see if currentNode(ecx) has a free right
270
+ mov ecx , [ ecx + 0x8 ] ;move to ecx the right value
271
+ cmp ecx , 0x0 ;check if right tree is null
272
+ jz we_have_found_currNode
273
+ jmp loop_for_currentNode
274
+
275
+ we_have_found_currNode:
276
+
277
+ jmp end_of_operator_actions ;TBDT
278
+
279
+ reached_end_token:
280
+ ;now we should pop the remaining stack nodes that we have pushed
281
+ ;we pop everytime into the currentNode and do that until currentNode = ebp
282
+
283
+ loop_for_poping_remaining_nodes:
284
+ mov ecx , ebp
285
+ sub ecx , esp
286
+ sub ecx , 0xc
287
+ jz very_end ;reached the bottom of the stack, the frame pointer
288
+ add esp , 0x4 ;pop node into ecx (we do nothing with it)
289
+
290
+ very_end:
291
+ mov eax , [ root ] ;put in eax the root
292
+
293
+ ;mov ecx, [eax + 0x8] ; left of "+"
294
+ ;mov ecx, [ecx + 0x4] ; right of "*"
295
+ ;mov ecx, [ecx + 4] ; right of "/"
296
+ ;mov ecx, [ecx + 4]
297
+ ;mov ecx, [ecx] ;
298
+
299
+
300
+ ;PRINTF32 `SFARSIT:%s:SFARSIT\n\x0`,ecx
301
+
302
+ pop edx
303
+ pop ecx
304
+ pop ebx
305
+ leave
306
+ ret
0 commit comments