Skip to content

Commit 12af5c1

Browse files
45pct
1 parent aea4945 commit 12af5c1

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

ast.asm

Lines changed: 67 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,12 +103,13 @@ 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 eax, [ebp + 8] ;puttin into ecx the string
107+
mov cl, [eax] ;putting into cl the first character of the string
108+
89109
cmp cl, '+'
90110
jz is_an_operator
91111
cmp cl, '-'
92-
jz is_an_operator
112+
jz is_a_minus ;it could either a operator or a negative number
93113
cmp cl, '*'
94114
jz is_an_operator
95115
cmp cl, '/'
@@ -98,11 +118,21 @@ check_if_operator: ;check is the symbol given parameter is an operator
98118
mov [isOperator], ebx
99119
jmp end
100120

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+
101129
is_an_operator:
130+
;PRINTF32 `OPERATOR:%c:OPERATOR\n\x0`,ecx
102131
mov ebx, 0x1
103132
mov [isOperator], ebx
104133
105134
end:
135+
106136
leave
107137
ret
108138

@@ -151,6 +181,12 @@ traverse_token:
151181

152182
;PRINTF32 `DA:%s:DA\n\x0`,edx
153183

184+
;push ecx
185+
;mov ecx, [currentNode]
186+
;mov ecx, [ecx]
187+
;PRINTF32 `INLINE:%s:INLINE\n\x0`,ecx
188+
;pop ecx
189+
154190
push ebx ;saving ebx register
155191
push ecx ;saving ecx register
156192
push edx ;saving edx register
@@ -164,7 +200,7 @@ traverse_token:
164200
push eax ;saving eax register
165201
push ebx ;saving ebx register
166202
push ecx ;saving ecx register (nu e neaparat momentan)
167-
push ebx ;pushing parameter
203+
push edx ;pushing parameter
168204
call check_if_operator
169205
add esp, 0x4 ;removing parameter
170206
pop ecx ;restoring ecx register
@@ -184,7 +220,7 @@ things_to_do_if_operator:
184220
mov ecx, [currentNode] ;move to ecx the node
185221
mov ecx, [ecx + 0x4] ;move to ecx the left node address
186222
cmp ecx, 0x0 ;check if it si null
187-
jz add_to_left
223+
jz add_to_left_operator
188224
jmp add_to_right_operator
189225
; DONE
190226

@@ -197,12 +233,13 @@ things_to_do_if_not_operator:
197233
cmp ecx, 0x0 ;check if it si null
198234
jz add_to_left_value
199235
jmp add_to_right_value
236+
; DONE
200237

201238
end_of_operator_actions:
202239

203240
jmp traverse_token ;iterate for next character
204241

205-
add_to_left:
242+
add_to_left_operator:
206243
mov ecx, [currentNode] ;move to ecx the node
207244
mov [ecx + 0x4], eax ;left value is updated
208245
mov [currentNode], eax ;currentNode is updated to the inserted value
@@ -219,12 +256,24 @@ add_to_left_value:
219256
mov [ecx + 0x4], eax ;left value is updated
220257
jmp end_of_operator_actions ;TBDT
221258

222-
add_to_right_value: ;adding to the right for integer values
259+
add_to_right_value: ;adding to the right for integer values
223260
mov ecx, [currentNode] ;move to ecx the node
224261
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:
225266
pop ecx ;pop the parrent node of value
226-
mov ecx, [esp + 0x4] ;mov to ecx the future currentNode without poping
267+
mov ecx, [esp] ;mov to ecx the future currentNode without poping
227268
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+
228277
jmp end_of_operator_actions ;TBDT
229278

230279
reached_end_token:
@@ -241,6 +290,15 @@ loop_for_poping_remaining_nodes:
241290
very_end:
242291
mov eax, [root] ;put in eax the root
243292
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+
244302
pop edx
245303
pop ecx
246304
pop ebx

0 commit comments

Comments
 (0)