Skip to content

Commit f4a1f21

Browse files
Change from create_node to inline expansion
1 parent 526afeb commit f4a1f21

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

ast.asm

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ section .text
1111

1212
extern evaluate_ast
1313
extern create_node
14+
extern malloc
15+
extern strdup
1416
global create_tree
1517
global iocla_atoi
1618

1719

1820
iocla_atoi: ;change string to number
1921
enter 0, 0
20-
push ebx
22+
push ebx ;saving callee registers
2123
push ecx
2224
push edx
2325

@@ -57,7 +59,7 @@ end_of_atoi:
5759
sub eax, ecx
5860
is_positive_number:
5961

60-
pop edx
62+
pop edx ;restoring callee registers
6163
pop ecx
6264
pop ebx
6365
leave
@@ -68,7 +70,6 @@ replace_next_white_char: ;replaces next space character with '\0'
6870
;returns a pointer to the word after the space, or
6971
;a 0 if this word was the last one (in ebx)
7072
enter 0, 0
71-
7273
mov ebx, [ebp + 8] ; the parameter is a string
7374

7475
starting_loop:
@@ -90,13 +91,14 @@ last_word_case:
9091
mov ebx, 0 ;return 0 in ebx
9192

9293
end_of_replacement_function:
93-
94+
9495
leave
9596
ret
9697

9798
check_if_operator: ;check is the symbol given parameter is an operator
9899
;(+,-,*,/), store the information in "isOperator"
99100
enter 0, 0
101+
push ebx
100102

101103
mov eax, [ebp + 8] ;puttin into ecx the string
102104
mov cl, [eax] ;putting into cl the first character of the string
@@ -127,12 +129,13 @@ is_an_operator:
127129
128130
end:
129131

132+
pop ebx
130133
leave
131134
ret
132135

133136
create_tree:
134137
enter 0, 0
135-
push ebx
138+
push ebx ;saving registers
136139
push ecx
137140
push edx
138141

@@ -149,9 +152,30 @@ create_tree:
149152
pop eax ;restoring eax register
150153

151154
;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
155179

156180
mov [root], eax; ;initialize the root
157181
mov [currentNode], eax ;initialize the currentNode for the first time
@@ -171,19 +195,35 @@ traverse_token:
171195
pop ecx ;restoring ecx register
172196
pop eax ;restoring eax register
173197

198+
;create node
174199
push ebx ;saving ebx register
175200
push ecx ;saving ecx register
176201
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+
180220
pop edx ;restoring edx register
181221
pop ecx ;restoring ecx register
182222
pop ebx ;restoring ebx register
183223
184224
push eax ;saving eax register
185225
push ebx ;saving ebx register
186-
push ecx ;saving ecx register (nu e neaparat momentan)
226+
push ecx ;saving ecx register
187227
push edx ;pushing parameter
188228
call check_if_operator
189229
add esp, 0x4 ;removing parameter
@@ -274,7 +314,7 @@ loop_for_poping_remaining_nodes:
274314
very_end:
275315
mov eax, [root] ;put in eax the root
276316
277-
pop edx
317+
pop edx ;restoring registers
278318
pop ecx
279319
pop ebx
280320
leave

0 commit comments

Comments
 (0)