Browse Source

Add files via upload

Nicolas T. Cuerbas 4 months ago
parent
commit
57e33636e3
No account linked to committer's email address
5 changed files with 89 additions and 9 deletions
  1. 46
    2
      Geracaodecodigo.py
  2. 1
    1
      fatorial.txt
  3. BIN
      final
  4. 40
    0
      final.fasm
  5. 2
    6
      selection.py

+ 46
- 2
Geracaodecodigo.py View File

@@ -35,7 +35,6 @@ def intemerdiario(token,args, inicio, fim, loop,condicional, texto):
35 35
 
36 36
     with open('intermediario.s','w') as f:
37 37
         f.write('\n'.join(texto))
38
-    #print (texto)
39 38
 def simpleexpre(exp,var,texto):
40 39
     '''
41 40
     ADD dest,src1,src2	dest = src1 + src2
@@ -77,4 +76,49 @@ def variaveis(token,i):
77 76
         if not('virgula' in token[l][0]):
78 77
             var.append('INTEIRO '+token[l][1])
79 78
         l+=1
80
-    return var
79
+    return var
80
+def gerarcodigo(token,args):
81
+    texto=[]
82
+    '''
83
+    intermediario(token, args, posicao inicial que deseja converter para assembler, posição final, valor inicial das Labels dos loops, valor inicial das Labels dos condicionais)
84
+    '''
85
+    intemerdiario(token, args, 0, len(token), 0, 0, texto)
86
+    gerar()
87
+def gerar():
88
+    texto=[]
89
+    with open('intermediario.s','r') as f:
90
+        texto=f.readlines()
91
+    i=0
92
+    final=[]
93
+    db=[]
94
+    msg=-1
95
+    final.append('format ELF64 executable 3')
96
+    final.append('segment readable executable')
97
+    final.append('entry main')
98
+    final.append('main:')
99
+
100
+    while i < len(texto):
101
+        if 'ESCREVA' in texto[i]:
102
+            if '"' in texto[i]:
103
+                msg += 1
104
+                size = texto[i].strip('ESCREVA')
105
+                size = size.strip('\n').replace('"','')
106
+                final.append(f'lea rdi, [msg{msg}]')
107
+                final.append(f'mov rax, {len(size)}')
108
+                final.append(f'mov rdx, rax')
109
+                final.append(f'mov rsi, rdi')
110
+                final.append(f'mov rdi, 1')
111
+                final.append(f'mov rax, 1')
112
+                final.append(f'syscall')
113
+                db.extend([f'msg{msg} db \'{size.strip()}\', 10,0'])
114
+            else:
115
+                print ()
116
+        i+=1
117
+    final.append('xor rdi,rdi')
118
+    final.append('mov rax, 60')
119
+    final.append('syscall')
120
+    if(msg>-1):
121
+        final.append('segment readable writable')
122
+        final.extend(db)
123
+    with open('final.fasm','w') as f:
124
+        f.write('\n'.join(final))

+ 1
- 1
fatorial.txt View File

@@ -2,7 +2,7 @@ programa
2 2
 inteiro var1, var2;
3 3
 escreva("Escreva um numero: ");
4 4
 leia(var1);
5
-var2 = (- 1);
5
+var2 = 1;
6 6
 se(1<1){
7 7
     enquanto(var1 > 1){
8 8
         var2 = var1 / var2 * 1 + 2;

BIN
final View File


+ 40
- 0
final.fasm View File

@@ -0,0 +1,40 @@
1
+format ELF64 executable 3
2
+segment readable executable
3
+entry main
4
+main:
5
+lea rdi, [msg0]
6
+mov rax, 20
7
+mov rdx, rax
8
+mov rsi, rdi
9
+mov rdi, 1
10
+mov rax, 1
11
+syscall
12
+lea rdi, [msg1]
13
+mov rax, 2
14
+mov rdx, rax
15
+mov rsi, rdi
16
+mov rdi, 1
17
+mov rax, 1
18
+syscall
19
+lea rdi, [msg2]
20
+mov rax, 14
21
+mov rdx, rax
22
+mov rsi, rdi
23
+mov rdi, 1
24
+mov rax, 1
25
+syscall
26
+lea rdi, [msg3]
27
+mov rax, 3
28
+mov rdx, rax
29
+mov rsi, rdi
30
+mov rdi, 1
31
+mov rax, 1
32
+syscall
33
+xor rdi,rdi
34
+mov rax, 60
35
+syscall
36
+segment readable writable
37
+msg0 db 'Escreva um numero:', 10,0
38
+msg1 db 'a', 10,0
39
+msg2 db 'fatorial de:', 10,0
40
+msg3 db '\n', 10,0

+ 2
- 6
selection.py View File

@@ -40,9 +40,5 @@ def lse(token,args):
40 40
     from semantico import semantico
41 41
     semantico(token,args)
42 42
 def lgc(token,args):
43
-    from  Geracaodecodigo import intemerdiario
44
-    texto = []
45
-    intemerdiario(token,args,0,len(token),0,0, texto)
46
-    '''
47
-    intermediario(token, args, posicao inicial que deseja converter para assembler, posição final, valor inicial das Labels dos loops, valor inicial das Labels dos condicionais)
48
-    '''
43
+    from  Geracaodecodigo import gerarcodigo
44
+    gerarcodigo(token,args)

Loading…
Cancel
Save