@@ -11,13 +11,15 @@ section .text
11
11
12
12
extern evaluate_ast
13
13
extern create_node
14
+ extern malloc
15
+ extern strdup
14
16
global create_tree
15
17
global iocla_atoi
16
18
17
19
18
20
iocla_atoi: ;change string to number
19
21
enter 0 , 0
20
- push ebx
22
+ push ebx ;saving callee registers
21
23
push ecx
22
24
push edx
23
25
@@ -57,7 +59,7 @@ end_of_atoi:
57
59
sub eax , ecx
58
60
is_positive_number:
59
61
60
- pop edx
62
+ pop edx ;restoring callee registers
61
63
pop ecx
62
64
pop ebx
63
65
leave
@@ -68,7 +70,6 @@ replace_next_white_char: ;replaces next space character with '\0'
68
70
;returns a pointer to the word after the space, or
69
71
;a 0 if this word was the last one (in ebx)
70
72
enter 0 , 0
71
-
72
73
mov ebx , [ ebp + 8 ] ; the parameter is a string
73
74
74
75
starting_loop:
@@ -90,13 +91,14 @@ last_word_case:
90
91
mov ebx , 0 ;return 0 in ebx
91
92
92
93
end_of_replacement_function:
93
-
94
+
94
95
leave
95
96
ret
96
97
97
98
check_if_operator: ;check is the symbol given parameter is an operator
98
99
;(+,-,*,/), store the information in "isOperator"
99
100
enter 0 , 0
101
+ push ebx
100
102
101
103
mov eax , [ ebp + 8 ] ;puttin into ecx the string
102
104
mov cl , [ eax ] ;putting into cl the first character of the string
@@ -127,12 +129,13 @@ is_an_operator:
127
129
128
130
end:
129
131
132
+ pop ebx
130
133
leave
131
134
ret
132
135
133
136
create_tree:
134
137
enter 0 , 0
135
- push ebx
138
+ push ebx ;saving registers
136
139
push ecx
137
140
push edx
138
141
@@ -149,9 +152,30 @@ create_tree:
149
152
pop eax ;restoring eax register
150
153
151
154
;initializing root Node
152
- push edx ;pushing parameter
153
- call create_node
154
- pop edx ;restore edx and empty parameter pushed to stack
155
+ push ebx ;saving ebx register
156
+ push ecx ;saving ecx register
157
+ push edx ;saving edx register
158
+
159
+ push edx ;save edx register for malloc
160
+ push 0xc
161
+ call malloc ;allocate space for structure
162
+ add esp , 0x4
163
+ pop edx ;restore edx register from malloc
164
+ push eax ;save eax
165
+
166
+ push edx
167
+ call strdup ;duplicate node value
168
+ add esp , 0x4
169
+ mov edx , eax
170
+
171
+ pop eax ;restore eax with node address
172
+ mov [ eax ], edx ;set value field
173
+ mov [ eax + 0x4 ], DWORD 0 ;set left to null
174
+ mov [ eax + 0x8 ], DWORD 0 ;set right to null
175
+
176
+ pop edx ;restoring edx register
177
+ pop ecx ;restoring ecx register
178
+ pop ebx ;restoring ebx register
155
179
156
180
mov [ root ], eax ; ;initialize the root
157
181
mov [ currentNode ], eax ;initialize the currentNode for the first time
@@ -171,19 +195,35 @@ traverse_token:
171
195
pop ecx ;restoring ecx register
172
196
pop eax ;restoring eax register
173
197
198
+ ;create node
174
199
push ebx ;saving ebx register
175
200
push ecx ;saving ecx register
176
201
push edx ;saving edx register
177
- push edx ;pushing parameter
178
- call create_node
179
- add esp , 0x4 ;removing parameter
202
+
203
+ push edx ;save edx register for malloc
204
+ push 0xc
205
+ call malloc ;allocate space for structure
206
+ add esp , 0x4
207
+ pop edx ;restore edx register from malloc
208
+ push eax ;save eax
209
+
210
+ push edx
211
+ call strdup ;duplicate node value
212
+ add esp , 0x4
213
+ mov edx , eax
214
+
215
+ pop eax ;restore eax with node address
216
+ mov [ eax ], edx ;set value field
217
+ mov [ eax + 0x4 ], DWORD 0 ;set left to null
218
+ mov [ eax + 0x8 ], DWORD 0 ;set right to null
219
+
180
220
pop edx ;restoring edx register
181
221
pop ecx ;restoring ecx register
182
222
pop ebx ;restoring ebx register
183
223
184
224
push eax ;saving eax register
185
225
push ebx ;saving ebx register
186
- push ecx ;saving ecx register (nu e neaparat momentan)
226
+ push ecx ;saving ecx register
187
227
push edx ;pushing parameter
188
228
call check_if_operator
189
229
add esp , 0x4 ;removing parameter
@@ -274,7 +314,7 @@ loop_for_poping_remaining_nodes:
274
314
very_end:
275
315
mov eax , [ root ] ;put in eax the root
276
316
277
- pop edx
317
+ pop edx ;restoring registers
278
318
pop ecx
279
319
pop ebx
280
320
leave
0 commit comments