Skip to content

Commit e715148

Browse files
35pct
1 parent aea4945 commit e715148

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

ast.asm

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ section .bss
66
root resd 1
77
currentNode resd 1
88
isOperator resd 1
9+
isNegativeAtoi resd 1
910

1011
section .text
1112

@@ -26,6 +27,15 @@ iocla_atoi: ;change string to number
2627
xor ecx, ecx ;stores the result
2728
xor ebx, ebx ;stores character
2829

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+
2939
atoi_loop:
3040
mov bl, [eax] ;get the character
3141
test bl, bl ;check if we reached the end
@@ -42,7 +52,16 @@ atoi_loop:
4252

4353
end_of_atoi:
4454

45-
mov eax, ecx ;return value
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+
4665
pop edx
4766
pop ecx
4867
pop ebx
@@ -84,8 +103,9 @@ check_if_operator: ;check is the symbol given parameter is an operator
84103
;(+,-,*,/), store the information in "isOperator"
85104
enter 0, 0
86105

87-
mov cl, [ebp + 8]
88-
;PRINTF32 `OPERATOR:%c:OPERATOR\n\x0`,ecx
106+
mov ecx, [ebp + 8] ;puttin into ecx the string
107+
mov cl, [ecx] ;putting into cl the first character of the string
108+
89109
cmp cl, '+'
90110
jz is_an_operator
91111
cmp cl, '-'
@@ -99,6 +119,7 @@ check_if_operator: ;check is the symbol given parameter is an operator
99119
jmp end
100120

101121
is_an_operator:
122+
;PRINTF32 `OPERATOR:%c:OPERATOR\n\x0`,ecx
102123
mov ebx, 0x1
103124
mov [isOperator], ebx
104125
@@ -164,7 +185,7 @@ traverse_token:
164185
push eax ;saving eax register
165186
push ebx ;saving ebx register
166187
push ecx ;saving ecx register (nu e neaparat momentan)
167-
push ebx ;pushing parameter
188+
push edx ;pushing parameter
168189
call check_if_operator
169190
add esp, 0x4 ;removing parameter
170191
pop ecx ;restoring ecx register
@@ -184,7 +205,7 @@ things_to_do_if_operator:
184205
mov ecx, [currentNode] ;move to ecx the node
185206
mov ecx, [ecx + 0x4] ;move to ecx the left node address
186207
cmp ecx, 0x0 ;check if it si null
187-
jz add_to_left
208+
jz add_to_left_operator
188209
jmp add_to_right_operator
189210
; DONE
190211

@@ -197,12 +218,14 @@ things_to_do_if_not_operator:
197218
cmp ecx, 0x0 ;check if it si null
198219
jz add_to_left_value
199220
jmp add_to_right_value
221+
; DONE
200222

201223
end_of_operator_actions:
202-
224+
;mov ecx, [ecx]
225+
;PRINTF32 `INLINE:%s:INLINE\n\x0`,ecx
203226
jmp traverse_token ;iterate for next character
204227

205-
add_to_left:
228+
add_to_left_operator:
206229
mov ecx, [currentNode] ;move to ecx the node
207230
mov [ecx + 0x4], eax ;left value is updated
208231
mov [currentNode], eax ;currentNode is updated to the inserted value
@@ -219,12 +242,26 @@ add_to_left_value:
219242
mov [ecx + 0x4], eax ;left value is updated
220243
jmp end_of_operator_actions ;TBDT
221244

222-
add_to_right_value: ;adding to the right for integer values
245+
add_to_right_value: ;adding to the right for integer values
223246
mov ecx, [currentNode] ;move to ecx the node
224247
mov [ecx + 0x8], eax ;right value is updated
248+
249+
;now currentNode becomes the value in the stack that has a free right spot
250+
;we are going to check for ecx, and when we find the value, we stop
251+
loop_for_currentNode:
225252
pop ecx ;pop the parrent node of value
226-
mov ecx, [esp + 0x4] ;mov to ecx the future currentNode without poping
253+
mov ecx, [esp] ;mov to ecx the future currentNode without poping
227254
mov [currentNode], ecx ;update currentNode to future currentNode
255+
;now we test to see if currentNode(ecx) has a free right
256+
mov ecx, [ecx + 0x8] ;move to ecx the right value
257+
cmp ecx , 0x0 ;check if right tree is null
258+
jz we_have_found_currNode
259+
jmp loop_for_currentNode
260+
261+
we_have_found_currNode:
262+
;mov ecx, [ecx]
263+
;PRINTF32 `INLINE:%s:INLINE\n\x0`,ecx
264+
228265
jmp end_of_operator_actions ;TBDT
229266

230267
reached_end_token:
@@ -241,6 +278,15 @@ loop_for_poping_remaining_nodes:
241278
very_end:
242279
mov eax, [root] ;put in eax the root
243280
281+
;mov ecx, [eax + 4] ; left of "+"
282+
;mov ecx, [ecx + 4] ; right of "*"
283+
;mov ecx, [ecx + 4] ; right of "/"
284+
;mov ecx, [ecx + 4]
285+
;mov ecx, [ecx] ;
286+
287+
288+
;PRINTF32 `SFARSIT:%s:SFARSIT\n\x0`,ecx
289+
244290
pop edx
245291
pop ecx
246292
pop ebx

0 commit comments

Comments
 (0)