Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %include "includes/io.inc"
- extern getAST
- extern freeAST
- section .data
- string1 db "-3216969", 0
- string2 db "694269830" , 0
- CONST_10 dd 10
- numar db "0x%x", 0
- number db "2222", 0
- mesaj_bun db "da boss, e numar", 0
- section .bss
- ; La aceasta adresa, scheletul stocheaza radacina arborelui
- root: resd 1
- ; var atoi
- ;inmultire: resd 1
- suma: resd 1
- negativ: resb 1
- ; alte var
- string: resb 10
- pointer: resd 1
- adresa: resd 1
- charp: resd 1
- section .text
- global main
- main:
- mov ebp, esp; for correct debugging
- ; NU MODIFICATI - ok
- push ebp
- mov ebp, esp
- ; Se citeste arborele si se scrie la adresa indicata mai sus
- call getAST
- mov [root], eax
- ; Implementati rezolvarea aici:
- ; push string2
- ; call atoi
- ; add esp, 4
- ; mov [pointer], eax
- ; PRINT_DEC 4, pointer
- ; NEWLINE
- ; NEWLINE
- ; PRINT_STRING "PLM"
- ; xor eax, eax
- ; mov ebx, [root]
- ; mov eax, [ebx]
- ; PRINT_STRING [eax]
- ; mov ecx, [ebx + 4]
- ; mov eax, [ecx]
- ; PRINT_STRING [eax]
- ; mov eax, [ebx + 8]
- ; mov eax, [eax]
- ; PRINT_STRING [eax]
- ; NEWLINE
- ;mov eax, [root]
- ;add eax, 4
- ;mov eax, [eax]
- ;mov eax, [eax]
- ; PRINT_STRING [eax]
- mov eax, [root]
- ;mov ebx, [eax + 4] ; I dereferentiere
- ;mov eax, [ebx] ;II dereferentiere
- push eax
- call recursiv
- add esp, 4
- ;NEWLINE
- PRINT_DEC 4, eax
- ;NEWLINE
- ;PRINT_STRING "PLM"
- ; push number
- ; call parse_data
- ; add esp, 4
- ; PRINT_UDEC 4, eax
- ; NU MODIFICATI - ok
- ; Se elibereaza memoria alocata pentru arbore
- push dword [root]
- call freeAST
- xor eax, eax
- leave
- ret
- recursiv:
- push ebp
- mov ebp, esp
- ; preferabil sa returnez numarul intr-un registru: de exemplu eax
- mov eax, [ebp + 8] ; eax are adresa celulei
- ; mov ebx,
- mov ebx, [eax] ; ebx are valoare lui data, adica pointerul la string
- push ebx ; o salvez pe siva
- ;PRINT_STRING [ebx]
- push ebx
- call parse_data
- add esp, 4
- push eax ; salvez daca e numar sau daca nue numar ce operatie e
- cmp eax, 0
- jne is_operation
- ;mov ebx, [ebp + 8] ; accesez din nou valoare lui data, adica pointerul la string
- ;mov ebx, [ebx]
- pop eax
- pop ebx
- push eax
- push ebx
- call atoi
- add esp, 8 ; double pop si pentru push eax de mai de sus
- jmp eof
- is_operation:
- mov ebx, [ebp + 8]
- add ebx, 4 ; copilul de pe stanga
- mov ebx, [ebx] ; abia aici e copilul stanga
- push ebx
- call recursiv
- add esp, 4
- push eax ; aici se salveaza pe stiva operandul 1 venit de pe stanga
- mov ebx, [ebp + 8]
- add ebx, 8 ; copilul de pe dreapta
- mov ebx, [ebx] ; abia aici e copilul dreapta
- push ebx
- call recursiv
- add esp, 4
- push eax ; aici se salveaza pe stiva operandul 2 venit de pe dreapta
- pop ecx ; op2
- pop ebx ; op1
- pop eax ; op {+, -, *, /}
- cmp eax, 1
- je adunare
- cmp eax, 2
- je scadere
- cmp eax, 3
- je inmultire
- cmp eax, 4
- je impartire
- adunare:
- add ebx, ecx
- mov eax, ebx
- jmp eof
- scadere:
- sub ebx, ecx
- mov eax, ebx
- jmp eof
- inmultire:
- mov eax, ebx
- imul ecx
- jmp eof
- impartire:
- test ebx, 0x80000000
- jnz label1
- mov edx, 0
- jmp skip_imp
- label1:
- mov edx, 0xFFFFFFFF
- skip_imp:
- mov eax, ebx
- idiv ecx
- eof:
- leave
- ret
- ;~~~~~~~end of recursiv~~~~~~~~~
- parse_data:
- ; preimeste adresa stringului de vazut daca e sau nu numar
- ; returneaza in eax 0 daca e numar, 1 adunare, 2 scadere, 3 inmultire, 4 impartire
- push ebp
- mov ebp, esp
- mov eax, [ebp + 8]
- xor ebx, ebx
- mov bl, byte [eax]
- cmp bl, 48 ; daca e mai amre sau egal ca 0 atunci claros e numar
- jge is_numar
- cmp bl, 43 ; daca e +
- je is_adunare
- cmp bl, 42 ; daca e *
- je is_inmultire
- cmp bl, 47 ; daca e /
- je is_impartire
- ; daca nu e numar care incepe cu cifra, nu e +, nu e *, nu e / atunci e -
- ; daca dupa minus e null terminator atunci e scadere, altfel e numar
- mov bl, byte [eax + 1]
- cmp bl, 0 ; compara cu null terminator
- jne is_numar
- mov eax, 2
- jmp end_parse_data
- is_impartire:
- mov eax, 4
- jmp end_parse_data
- is_inmultire:
- mov eax, 3
- jmp end_parse_data
- is_adunare:
- mov eax, 1
- jmp end_parse_data
- is_numar:
- mov eax, 0
- end_parse_data:
- leave
- ret
- ;~~~~~~~~~ end of parse data!~~~~~~~~~
- atoi:
- ; primeste pointer la string
- ; returneaza rezultatul in eax
- push ebp
- mov ebp, esp
- mov edx, [ebp + 8]
- mov ecx, 1
- mov ebx, 0
- mov eax, 0
- mov byte [negativ], 0x00 ; nagitv = 0
- ; mov byte [suma], 0
- mov bl, byte [edx]
- cmp bl, 45 ; verifica daca primul char este '-'
- jne skip_no_negative
- mov byte [negativ], 1 ; negativ = 1
- jmp while
- skip_no_negative:
- mov al, bl
- movsx eax, al
- sub al, 0x30
- while:
- mov bl, byte [edx + ecx] ; muta char cu char in bl
- cmp bl, 0 ; cmp cu '\0'
- je endWhile ; sfarsitul stringulu
- sub bl, 48 ; conversie din char in numar
- movsx ebx, bl
- ;mov eax, [suma]
- push edx
- mul dword[CONST_10]
- pop edx
- add eax, ebx
- ; mov [suma], eax
- inc ecx
- jmp while
- endWhile:
- ;mov eax, [suma]
- cmp byte [negativ], 0 ; cmp negative cu 0
- je endFunction ; daca negative == 0 iese din functie
- neg eax ; else eax *= -1;
- ; mov [suma], eax
- endFunction:
- leave
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement