This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionEvaluation.asm
105 lines (80 loc) · 2.73 KB
/
ExpressionEvaluation.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
%include "io.inc"
%define MAX_INPUT_SIZE 4096
section .bss
expr: resb MAX_INPUT_SIZE
section .text
global CMAIN
number_construction: ; se parseaza stringul citit cat timp contine cifre
push ebp
lea ebp, [esp] ; se construieste un nou numar
xor edx, edx ; dl va contine fiecare caracter
xor eax, eax ; eax contine numarul din string, care va fi intors
parse_input:
mov dl, [ecx]
cmp dl, ' ' ; daca s-a gasit un spatiu sau un \0
jbe return
imul eax, 10
lea eax, [eax + edx - '0'] ; eax = eax * 10 + [ecx] - '0'
inc ecx
jmp parse_input
return:
leave
ret
CMAIN:
push ebp
lea ebp, [esp]
GET_STRING expr, MAX_INPUT_SIZE
lea ecx, [expr] ; se va lucra direct pe octetii stringului, pana la '\0'
evaluate_expression:
mov ebx, 1 ; ebx tine initial semnul unui numar gasit
cmp byte [ecx], 0 ; s-a ajuns la finalul stringului
je write
cmp byte [ecx], '*'
je multiplication
cmp byte [ecx], '/'
je division
cmp byte [ecx], '+'
je addition
cmp byte [ecx], '-' ; '-' poate simboliza o scadere sau un numar negativ
je decrease
cmp byte [ecx], ' '
je next_character
jmp get_number ; daca nu s-au gasit alte semne, s-a gasit o cifra
decrease:
cmp byte [ecx + 1], ' ' ; daca urmeaza un spatiu sau '\0'
jbe subtraction
mov ebx, -1 ; se schimba semnul numarului
inc ecx
get_number:
push eax ; se salveaza pe stiva fostul numar obtinut sau creat
call number_construction ; se returneaza in eax noul numar gasit, fara semn
imul ebx ; se aplica semnul
jmp next_character
multiplication:
imul dword [esp] ; se retine in eax rezultatul inmultirii
add esp, 4 ; se scoate inmultitorul de pe stiva
jmp next_character
division: ; se impart primele 2 numere gasite in ordine inversa, respectand semnul
pop ebx ; deimpartitul este pe stiva si se preia de catre ebx
xchg eax, ebx ; se pun operanzii in ordinea corecta
cdq
idiv ebx
jmp next_character
addition:
add eax, dword [esp] ; se aduna primele 2 numere disponibile
add esp, 4 ; se scoate al doilea termen de pe stiva
jmp next_character
subtraction: ; se scad primele 2 numere disponibile, in ordine inversa
pop ebx ; descazutul se gaseste pe stiva si se extrage
sub ebx, eax
lea eax, [ebx]
jmp next_character
next_character:
inc ecx
jmp evaluate_expression
write:
PRINT_DEC 4, eax ; rezultatul va fi in eax la sfarsit
NEWLINE
xor eax, eax
leave
ret