Skip to content

Commit c27c203

Browse files
Fixed caller and callee saved registers (matching cdecl)
1 parent f4a1f21 commit c27c203

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

ast.asm

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ section .data
33

44
section .bss
55
root resd 1
6-
currentNode resd 1
7-
isOperator resd 1
8-
isNegativeAtoi resd 1
6+
current_node resd 1
7+
is_operator resd 1
8+
is_negative_atoi resd 1
99

1010
section .text
1111

@@ -20,21 +20,19 @@ global iocla_atoi
2020
iocla_atoi: ;change string to number
2121
enter 0, 0
2222
push ebx ;saving callee registers
23-
push ecx
24-
push edx
2523

2624
mov eax, [ebp + 8]
2725
xor ecx, ecx ;stores the result
2826
xor ebx, ebx ;stores character
2927

3028
mov edx, 0
31-
mov [isNegativeAtoi], edx ;the default way is that the number is positive
29+
mov [is_negative_atoi], edx ;the default way is that the number is positive
3230
;we check to see if the number is negative or not
3331
mov bl, [eax]
3432
cmp bl, '-' ;compare to minus character
3533
jnz atoi_loop ;if it is not a minus sign just do the rest as usual
3634
mov edx, 1
37-
mov [isNegativeAtoi], edx ;change global variable
35+
mov [is_negative_atoi], edx ;change global variable
3836
inc eax ;get rid of the minus
3937

4038
atoi_loop:
@@ -51,17 +49,15 @@ end_of_atoi:
5149

5250
;checking to see if the number was negative or not
5351
mov eax, ecx
54-
mov edx, [isNegativeAtoi]
52+
mov edx, [is_negative_atoi]
5553
cmp edx, 0
5654
jz is_positive_number
5755
;we do the next only of the number is negative
5856
mov eax, 0
5957
sub eax, ecx
6058
is_positive_number:
6159

62-
pop edx ;restoring callee registers
63-
pop ecx
64-
pop ebx
60+
pop ebx ;restoring callee registers
6561
leave
6662
ret
6763

@@ -74,7 +70,7 @@ replace_next_white_char: ;replaces next space character with '\0'
7470

7571
starting_loop:
7672
mov cl, [ebx]
77-
cmp cl, ' '
73+
cmp cl, [delim]
7874
jz end_of_replacement ;we have found a space, going to replace it
7975
cmp cl, 0
8076
jz last_word_case ;we have foudn a '\0' char, this is the last word
@@ -96,7 +92,7 @@ end_of_replacement_function:
9692
ret
9793

9894
check_if_operator: ;check is the symbol given parameter is an operator
99-
;(+,-,*,/), store the information in "isOperator"
95+
;(+,-,*,/), store the information in "is_operator"
10096
enter 0, 0
10197
push ebx
10298

@@ -112,20 +108,20 @@ check_if_operator: ;check is the symbol given parameter is an operator
112108
cmp cl, '/'
113109
jz is_an_operator
114110
mov ebx, 0x0 ; it is not an operator
115-
mov [isOperator], ebx
111+
mov [is_operator], ebx
116112
jmp end
117113

118114
is_a_minus:
119115
mov cl, [eax + 0x1] ;get the second character
120116
cmp cl, 0 ;if the next char is '\0' is an operator
121117
jz is_an_operator
122118
mov ebx, 0x0 ; it is not an operator
123-
mov [isOperator], ebx
119+
mov [is_operator], ebx
124120
jmp end
125121

126122
is_an_operator:
127123
mov ebx, 0x1
128-
mov [isOperator], ebx
124+
mov [is_operator], ebx
129125
130126
end:
131127

@@ -136,8 +132,6 @@ end:
136132
create_tree:
137133
enter 0, 0
138134
push ebx ;saving registers
139-
push ecx
140-
push edx
141135

142136
mov edx, [ebp + 8]
143137

@@ -178,7 +172,7 @@ create_tree:
178172
pop ebx ;restoring ebx register
179173

180174
mov [root], eax; ;initialize the root
181-
mov [currentNode], eax ;initialize the currentNode for the first time
175+
mov [current_node], eax ;initialize the current_node for the first time
182176
push eax; ;push first node to stack
183177

184178
traverse_token:
@@ -231,17 +225,17 @@ traverse_token:
231225
pop ebx ;restoring ebx register
232226
pop eax ;restoring eax register
233227

234-
mov ecx, [isOperator];
228+
mov ecx, [is_operator];
235229
cmp ecx, 0x1
236230
je things_to_do_if_operator ;it is an operator
237231
jmp things_to_do_if_not_operator ;it is not an operator
238232

239233
things_to_do_if_operator:
240234
push eax
241-
;now we add the eax node either to the left or right of currentNode
235+
;now we add the eax node either to the left or right of current_node
242236
;if left is null, add to left; if left is not null, add to right and change
243-
;currentNode to parrent node
244-
mov ecx, [currentNode] ;move to ecx the node
237+
;current_node to parrent node
238+
mov ecx, [current_node] ;move to ecx the node
245239
mov ecx, [ecx + 0x4] ;move to ecx the left node address
246240
cmp ecx, 0x0 ;check if it si null
247241
jz add_to_left_operator
@@ -251,8 +245,8 @@ things_to_do_if_operator:
251245
things_to_do_if_not_operator:
252246
;NO need to push eax because integer values are always leafs
253247
;we add the eax node either to the left or right, if we add to the right
254-
;the currentNode is updated by poping the stack
255-
mov ecx, [currentNode] ;move to ecx the node
248+
;the current_node is updated by poping the stack
249+
mov ecx, [current_node] ;move to ecx the node
256250
mov ecx, [ecx + 0x4] ;move to ecx the left node address
257251
cmp ecx, 0x0 ;check if it si null
258252
jz add_to_left_value
@@ -264,58 +258,56 @@ end_of_operator_actions:
264258
jmp traverse_token ;iterate for next character
265259

266260
add_to_left_operator:
267-
mov ecx, [currentNode] ;move to ecx the node
261+
mov ecx, [current_node] ;move to ecx the node
268262
mov [ecx + 0x4], eax ;left value is updated
269-
mov [currentNode], eax ;currentNode is updated to the inserted value
263+
mov [current_node], eax ;current_node is updated to the inserted value
270264
jmp end_of_operator_actions ;TBDT
271265

272266
add_to_right_operator: ;adding to the right for operators
273-
mov ecx, [currentNode] ;move to ecx the node
267+
mov ecx, [current_node] ;move to ecx the node
274268
mov [ecx + 0x8], eax ;right value is updated
275-
mov [currentNode], eax ;currentNode is updated to the inserted value
269+
mov [current_node], eax ;current_node is updated to the inserted value
276270
jmp end_of_operator_actions ;TBDT
277271

278272
add_to_left_value:
279-
mov ecx, [currentNode] ;move to ecx the node
273+
mov ecx, [current_node] ;move to ecx the node
280274
mov [ecx + 0x4], eax ;left value is updated
281275
jmp end_of_operator_actions ;TBDT
282276

283277
add_to_right_value: ;adding to the right for integer values
284-
mov ecx, [currentNode] ;move to ecx the node
278+
mov ecx, [current_node] ;move to ecx the node
285279
mov [ecx + 0x8], eax ;right value is updated
286280

287-
;now currentNode becomes the value in the stack that has a free right spot
281+
;now current_node becomes the value in the stack that has a free right spot
288282
;we are going to check for ecx, and when we find the value, we stop
289-
loop_for_currentNode:
283+
loop_for_current_node:
290284
pop ecx ;pop the parrent node of value
291-
mov ecx, [esp] ;mov to ecx the future currentNode without poping
292-
mov [currentNode], ecx ;update currentNode to future currentNode
293-
;now we test to see if currentNode(ecx) has a free right
285+
mov ecx, [esp] ;mov to ecx the future current_node without poping
286+
mov [current_node], ecx ;update current_node to future current_node
287+
;now we test to see if current_node(ecx) has a free right
294288
mov ecx, [ecx + 0x8] ;move to ecx the right value
295289
cmp ecx , 0x0 ;check if right tree is null
296290
jz we_have_found_currNode
297-
jmp loop_for_currentNode
291+
jmp loop_for_current_node
298292

299293
we_have_found_currNode:
300294
301295
jmp end_of_operator_actions ;TBDT
302296

303297
reached_end_token:
304298
;now we should pop the remaining stack nodes that we have pushed
305-
;we pop everytime into the currentNode and do that until currentNode = ebp
299+
;we pop everytime into the current_node and do that until current_node = ebp
306300
307301
loop_for_poping_remaining_nodes:
308302
mov ecx, ebp
309303
sub ecx, esp
310-
sub ecx, 0xc
304+
sub ecx, 0x4
311305
jz very_end ;reached the bottom of the stack, the frame pointer
312306
add esp, 0x4 ;pop node into ecx (we do nothing with it)
313307

314308
very_end:
315309
mov eax, [root] ;put in eax the root
316310
317-
pop edx ;restoring registers
318-
pop ecx
319311
pop ebx
320312
leave
321313
ret

0 commit comments

Comments
 (0)