Best Python code snippet using avocado_python
object.py
Source:object.py
1import os2import re3import sys4regs=['t1','t2','t3','t4','t5','t6','t7','t8','t9','s0','s1','s2','s3','s4','s5','s6','s7']5table={}6reg_ok={}7variables=[]89def translate(line):10 if line[1]=='=':11 if line[3] == '_':12 if line[2][0] == '#': # ['15:', '=', '#1', '_', 'a'] liå è½½ç«å³æ°13 return '%s\n\tli %s,%s'%("Label"+line[0],Get_R(line[4]),line[2].replace('#',''))14 else: # ['15:', '=', 't1', '_', 'a']15 return '%s\n\tmove %s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]))16 if line[1] == '+': 17 if line[3][0] == '#': # ['2:', '+', 'a', '#1', 't2']18 return '%s\n\taddi %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),line[3].replace('#',''))19 elif line[2][0] == '#': # ['2:', '+', '#1', 'a', 't2'] 20 return '%s\n\taddi %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[3]),line[2].replace('#',''))21 else: # ['2:', '+', 'b', 'a', 't2']22 return '%s\n\tadd %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),Get_R(line[3]))23 if line[1] == '-': 24 if line[3][0] == '#': # ['2:', '-', 'a', '#1', 't2'] 25 return '%s\n\taddi %s,%s,-%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),line[3].replace('#',''))26 elif line[2][0] == '#': # ['2:', '-', '#1', 'a', 't2'] 27 return '%s\n\taddi %s,%s,-%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[3]),line[2].replace('#',''))28 else: # ['2:', '-', 'b', 'a', 't2']29 return '%s\n\tsub %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),Get_R(line[3]))30 if line[1] == '*': # ['3:', '*', 't2', 'a', 't3']31 return '%s\n\tmul %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),Get_R(line[3]))32 if line[1]=='/': # div:两个å®ç¹å¯åå¨çå
容ç¸é¤ã ['3:', '/', 't2', 'a', 't3'] åæ°åæ¾å¨ lo, ä½æ°åæ¾å¨ hi ;ä¸è½ç´æ¥è·å hi æ loä¸çå¼; éè¦mfhi, mfloæä»¤ä¼ å¼ç»å¯åå¨33 return '%s\n\tdiv %s,%s\n\tmflo %s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),Get_R(line[4]))34 if line[1]=='%': # %:两个å®ç¹å¯åå¨çå
容æ±ä½ã ['3:', '%', 't2', 'a', 't3'] åæ°åæ¾å¨ lo, ä½æ°åæ¾å¨ hi ;ä¸è½ç´æ¥è·å hi æ loä¸çå¼; éè¦mfhi, mfloæä»¤ä¼ å¼ç»å¯åå¨35 return '%s\n\tdiv %s,%s\n\tmfhi %s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),Get_R(line[4]))36 if line[1]=='<': # slt $1,$2,$3 if($2<$3) $1=1 else $1=0 ['3:', '<', 'var0', 't4', 't5']37 return '%s\n\tslt %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[2]),Get_R(line[3]))38 if line[1]=='>':39 return '%s\n\tslt %s,%s,%s'%("Label"+line[0],Get_R(line[4]),Get_R(line[3]),Get_R(line[2]))40 if line[1] == 'CALL':41 if line[4] != '_': # ['1:','CALL','fun','_',t1]42 if line[2] == 'INPUT' or line[2] == 'PRINT': # ['1:','CALL','INPUT','_',t1]43 # $sp æ æéï¼æåæ 顶 -4 (æåtemp9); sw $ra,0($sp)-> memory[$sp+0]=$ra è¿åå°å; jal INPUT/PRINT; 44 # lw $ra,0($sp) -> $ra=memory[$sp+0]; move Get_R(temp9),$v0 INPUTå°±æ¯è¯»åçæ´æ°ï¼PRINTæ¯0 ; addi $sp,$sp,445 return '%s\n\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\tmove %s,$v0\n\taddi $sp,$sp,4'%("Label"+line[0],line[2],Get_R(line[4]))46 else: # ['1:','CALL','fun','_',t1]47 # addi $sp,$sp,-2448 # sw $t0,0($sp) -> memory[$sp+0]=$t049 # sw $ra,4($sp) -> memory[$sp+4]=$ra è¿åå°å50 # sw $t1,8($sp) -> memory[$sp+8]=$t151 # sw $t2,12($sp) -> memory[$sp+12]=$t252 # sw $t3,16($sp) -> memory[$sp+16]=$t353 # sw $t4,20($sp) -> memory[$sp+20]=$t454 # jal fact -> goto line[-1]55 # lw $a0,0($sp) -> $a0=memory[$sp+0]56 # lw $ra,4($sp) -> $ra=memory[$sp+4]57 # lw $t1,8($sp) -> $t1=memory[$sp+8]58 # lw $t2,12($sp) -> $t2=memory[$sp+12]59 # lw $t3,16($sp) -> $t3=memory[$sp+16]60 # lw $t4,20($sp) -> $t4=memory[$sp+20]61 # addi $sp,$sp,2462 return '%s\n\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0'%("Label"+line[0],line[2],Get_R(line[4]))63 else: # ['1:','CALL','fun','_','_']64 if line[2] == 'INPUT' or line[2] == 'PRINT': # ['1:','CALL','INPUT','_','_']65 return '%s\n\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\taddi $sp,$sp,4'%("Label"+line[0],line[2])66 else: # ['1:','CALL','fun','_','_']67 return '%s\n\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0'%("Label"+line[0],line[2],Get_R(line[1]))68 if line[1]=='GOTO': # ['1:','GOTO','_','_','Label'+2]69 return '%s\n\tj %s'%("Label"+line[0],'Label'+line[4])70 if line[1]=='RETURN':# ['1:','RETURN','t1','_','_'] ç¨v0åå¨è¿åå¼71 return '%s\n\tmove $v0,%s\n\tjr $ra'%("Label"+line[0],Get_R(line[2]))72 # ['1:','JEQ','t1','t2','Label'+2] -> == ; beq $1,$2,10 -> if($1==$2) goto PC+4+4073 if line[1] == 'JEQ':74 return '%s\n\tbeq %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1]) 75 # ['1:','JNZ','t1','t2','Label'+2] -> != ; bne $1,$2,10 -> if($1!=$2) goto PC+4+4076 if line[1] == 'JNE':77 return '%s\n\tbne %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1])78 # ['1:','J>','t1','t2','Label'+2] ; bgt $1,$2,10 -> if($1>$2) goto PC+4+4079 if line[1] == 'J>':80 return '%s\n\tbgt %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1])81 # ['1:','J<','t1','t2','Label'+2] ; blt $1,$2,10 -> if($1<$2) goto PC+4+4082 if line[1] == 'J<':83 return '%s\n\tblt %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1])84 # ['1:','JGE','t1','t2','Label'+2] -> >= ; bge $1,$2,10 -> if($1>=$2) goto PC+4+4085 if line[1] == 'JGE':86 return '%s\n\tbge %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1])87 # ['1:','JLE','t1','t2','Label'+2] -> <= ; ble $1,$2,10 -> if($1<=$2) goto PC+4+4088 if line[1] == 'JLE':89 return '%s\n\tble %s,%s,%s'%("Label"+line[0],Get_R(line[2]),Get_R(line[3]),"Label"+line[-1])90 # ['1:','FUNCTION','_','_','main']91 if line[1]=='FUNCTION':92 return '%s:\n%s'%(line[4],"Label"+line[0])93 # ['1:','ARG','var2','_','_'] 94 if line[1]=='ARG':95 return '%s\n\tmove $t0,$a0\n\tmove $a0,%s'%("Label"+line[0],Get_R(line[2]))96 if line[1]=='param': # ['1:','param','_','_','var2'] 97 table[line[4]]='a0'98 if line[1]=='^': # ['1:','^','a','b','t1'] mul a,b,a mul a,b,a mul a,b,a mul a,b,t199 return '%s\n\tli $a0,1\n\tmove %s,%s\n%s\n\tmul %s,%s,%s\n\taddi $a0,$a0,1\n\tblt $a0,%s,%s\n\tmove %s,%s'%("Label"+line[0],Get_R(line[0]),Get_R(line[2]),'Labelmi'+line[0],Get_R(line[2]),Get_R(line[2]),Get_R(line[0]),Get_R(line[3]),'Labelmi'+line[0][0:-1],Get_R(line[2]),Get_R(line[4]))100 return ''101102def write_to_txt(Obj):103 f = sys.stdout104 template='''105.data106_prompt: .asciiz "Enter an integer:"107_ret: .asciiz "\\n"108.globl main109.text110jal main111addi $v0 $v0 10112syscall113INPUT:114 li $v0,4 # The address of the string to be PRINTed is assigned to $a0115 la $a0,_prompt116 syscall117 li $v0,5 # Assigns the INPUT integer to $v0118 syscall119 jr $ra # jr -> goto # $ra the returned address120121PRINT:122 li $v0,1 # The integer to be PRINTed is assigned to $a0123 syscall124 li $v0,4 # The address of the string to be PRINTed is assigned to $a0125 la $a0,_ret # LA(Load Address) An instruction is used to store an address or label into a register126 syscall127 move $v0,$0128 jr $ra129'''130 f.write(template)131 for line in Obj:132 f.write(line+'\n')133 f.close()134def Load_Var(Inter):135 global variables136 temp_re = '(temphh\d+)'137 for line in Inter:138 temps=re.findall(temp_re,' '.join(line))139 variables+=temps140141def Load_Inter(filename):142 lines=[]143 for line in open(filename,'r',encoding='utf-8'):144 line=line.replace('\r','').replace('\n','')145 if line =='':146 continue147 lines.append(line.split('\t'))148 return lines149150def Get_R(string):151 try:152 variables.remove(string)153 except:154 pass155 if string in table:156 return '$'+table[string] #å¦æå·²ç»åå¨å¯åå¨åé
ï¼é£ä¹ç´æ¥è¿åå¯åå¨157 else:158 keys=[]159 for key in table: #å·²ç»åé
å¯åå¨çåékey160 keys.append(key)161 for key in keys: #å½éå°æªåé
å¯åå¨çåéæ¶ï¼æ¸
空ä¹åææåé
ç临æ¶åéçæ å°å
³ç³»ï¼ï¼ï¼162 if 'temphh' in key and key not in variables: #163 reg_ok[table[key]]=1164 del table[key]165 for reg in regs: #对äºææå¯åå¨166 if reg_ok[reg]==1: #å¦æå¯åå¨å¯ç¨167 table[string]=reg #å°å¯ç¨å¯åå¨åé
ç»è¯¥åéï¼æ å°å
³ç³»åå°tableä¸168 reg_ok[reg]=0 #å¯åå¨reg设置为已ç¨169 return '$'+reg170def parser():171 for reg in regs:172 reg_ok[reg]=1 #åå§åï¼ææå¯åå¨é½å¯ç¨173 Inter=Load_Inter(sys.argv[1]) #读åä¸é´ä»£ç 174 Load_Var(Inter) #第ä¸éæ«æï¼è®°å½ææåé175 Obj=[]176 for line in Inter:177 obj_line=translate(line) #ç¿»è¯ä¸é´ä»£ç æMIPSæ±ç¼178 if obj_line=='':179 continue180 Obj.append(obj_line)181 write_to_txt(Obj)182183if __name__ == "__main__":
...
changecodes.py
Source:changecodes.py
...20 if line == '':21 continue22 lines.append(line.split(' '))23 return lines24def get_r(string):25 print(variables)26 print(table)27 if string[0] == '*':28 string = string[1:]29 try:30 variables.remove(string)31 except Exception as e:32 print(string, "is not in variables!")33 if string in table:34 return '$' + table[string]35 else:36 keys = []37 for key in table:38 keys.append(key)39 for key in keys:40 if 'temp' in key and key not in variables:41 reg_ok[table[key]] = 142 del table[key]43 for reg in regs:44 if reg_ok[reg] == 1:45 table[string] = reg46 reg_ok[reg] = 047 return '$' + reg48def translate(line):49 if line[0] == 'LABEL':50 return line[1] + ':'51 if line[1] == ':=':52 if len(line) == 3:53 if line[-1][0] == '#':54 return '\tli %s,%s' % (get_r(line[0]), line[-1].replace(55 '#', ''))56 elif line[0][0] == '*':57 return '\tsw %s,0(%s)' % (get_r(line[2]), get_r(line[0][1:]))58 elif line[2][0] == '*':59 return '\tlw %s,0(%s)' % (get_r(line[0]), get_r(line[2][1:]))60 else:61 get_r(line[0])62 get_r(line[2])63 return '\tmove %s,%s' % (get_r(line[0]), get_r(line[2]))64 if len(line) == 5:65 if line[2][0] == '&' and line[2][1] == 'a':66 return '\tla %s,%s\n\taddu %s,%s,%s' % (get_r(67 line[2]), line[2][1:], get_r(line[0]), get_r(line[4]),68 get_r(line[2]))69 tmp_array = []70 if line[2][0] == '*':71 t1 = get_r(line[2][1:])72 variables.append(line[2][1:])73 t2 = get_r(line[2][1:])74 variables.append(line[2][1:])75 tmp_array.append('\tlw %s,0(%s)' % (t1, t2))76 if line[4][0] == '*':77 t1 = get_r(line[4][1:])78 variables.append(line[4][1:])79 t2 = get_r(line[4][1:])80 variables.append(line[4][1:])81 tmp_array.append('\tlw %s,0(%s)' % (t1, t2))82 if line[3] == '+':83 if line[-1][0] == '#':84 return '\taddi %s,%s,%s' % (get_r(line[0]), get_r(line[2]),85 line[-1].replace('#', ''))86 else:87 tmp_array.append('\tadd %s,%s,%s' % (get_r(88 line[0]), get_r(line[2]), get_r(line[-1])))89 return '\n'.join(tmp_array)90 elif line[3] == '-':91 if line[-1][0] == '#':92 return '\taddi %s,%s,-%s' % (get_r(line[0]), get_r(93 line[2]), line[-1].replace('#', ''))94 else:95 tmp_array.append('\tsub %s,%s,%s' % (get_r(96 line[0]), get_r(line[2]), get_r(line[-1])))97 return '\n'.join(tmp_array)98 elif line[3] == '*':99 tmp_array.append('\tmul %s,%s,%s' % (get_r(100 line[0]), get_r(line[2]), get_r(line[4])))101 return '\n'.join(tmp_array)102 elif line[3] == '/':103 tmp_array.append('\tdiv %s,%s\n\tmflo %s' % (get_r(104 line[2]), get_r(line[-1]), get_r(line[0])))105 return '\n'.join(tmp_array)106 elif line[3] == '<':107 tmp_array.append('\tslt %s,%s,%s' % (get_r(108 line[0]), get_r(line[2]), get_r(line[-1])))109 return '\n'.join(tmp_array)110 elif line[3] == '>':111 tmp_array.append('\tslt %s,%s,%s' % (get_r(112 line[0]), get_r(line[-1]), get_r(line[2])))113 return '\n'.join(tmp_array)114 if line[2] == 'CALL':115 if line[3] == 'read' or line[3] == 'print':116 return '\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\tmove %s,$v0\n\taddi $sp,$sp,4' % (117 line[-1], get_r(line[0]))118 else:119 return '\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0' % (120 line[-1], get_r(line[0]))121 if line[0] == 'DEC':122 data.append('%s: .space 1024' % (line[1]))123 if line[0] == 'GOTO':124 return '\tj %s' % line[1]125 if line[0] == 'RETURN':126 return '\tmove $v0,%s\n\tjr $ra' % get_r(line[1])127 if line[0] == 'IF':128 if line[2] == '==':129 return '\tbeq %s,%s,%s' % (get_r(line[1]), get_r(line[3]),130 line[-1])131 if line[2] == '!=':132 return '\tbne %s,%s,%s' % (get_r(line[1]), get_r(line[3]),133 line[-1])134 if line[2] == '>':135 return '\tbgt %s,%s,%s' % (get_r(line[1]), get_r(line[3]),136 line[-1])137 if line[2] == '<':138 return '\tblt %s,%s,%s' % (get_r(line[1]), get_r(line[3]),139 line[-1])140 if line[2] == '>=':141 return '\tbge %s,%s,%s' % (get_r(line[1]), get_r(line[3]),142 line[-1])143 if line[2] == '<=':144 return '\tble %s,%s,%s' % (get_r(line[1]), get_r(line[3]),145 line[-1])146 if line[0] == 'FUNCTION':147 return '%s:' % line[1]148 if line[0] == 'CALL':149 if line[-1] == 'read' or line[-1] == 'print':150 return '\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\taddi $sp,$sp,4' % (151 line[-1])152 else:153 return '\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0' % (154 line[-1], get_r(line[0]))155 if line[0] == 'ARG':156 if line[1][0] == '*':157 return '\tlw %s,0(%s)\n\tmove $t0,$a0\n\tmove $a0,%s' % (get_r(158 line[1][1:]), get_r(line[1][1:]), get_r(line[1][1:]))159 return '\tmove $t0,$a0\n\tmove $a0,%s' % get_r(line[-1])160 if line[0] == 'PARAM':161 table[line[-1]] = 'a0'162 return ''163def write_to_txt(obj):164 f = open('result.asm', 'w')165 template1 = '''166read:167 li $v0,4168 la $a0,_prompt169 syscall170 li $v0,5171 syscall172 jr $ra173print:...
objectcode.py
Source:objectcode.py
1import os2import re3regs=['t1','t2','t3','t4','t5','t6','t7','t8','t9','s0','s1','s2','s3','s4','s5','s6','s7']4table={}5reg_ok={}6variables=[]7def Load_Var(Inter):8 global variables9 temp_re='(temp\d+)'10 for line in Inter:11 temps=re.findall(temp_re,' '.join(line))12 variables+=temps13def Load_Inter(filename):14 lines=[]15 for line in open(filename,'r',encoding='utf-8'):16 line=line.replace('\r','').replace('\n','')17 if line =='':18 continue19 lines.append(line.split(' '))20 return lines21def Get_R(string):22 try:23 variables.remove(string)24 except:25 pass26 if string in table:27 return '$'+table[string] #å¦æå·²ç»åå¨å¯åå¨åé
ï¼é£ä¹ç´æ¥è¿åå¯åå¨28 else:29 keys=[]30 for key in table: #å·²ç»åé
å¯åå¨çåékey31 keys.append(key)32 for key in keys: #å½éå°æªåé
å¯åå¨çåéæ¶ï¼æ¸
空ä¹åææåé
ç临æ¶åéçæ å°å
³ç³»ï¼ï¼ï¼33 if 'temp' in key and key not in variables: #34 reg_ok[table[key]]=135 del table[key]36 for reg in regs: #对äºææå¯åå¨37 if reg_ok[reg]==1: #å¦æå¯åå¨å¯ç¨38 table[string]=reg #å°å¯ç¨å¯åå¨åé
ç»è¯¥åéï¼æ å°å
³ç³»åå°tableä¸39 reg_ok[reg]=0 #å¯åå¨reg设置为已ç¨40 return '$'+reg41def translate(line):42 if line[0]=='LABEL':43 return line[1]+':'44 if line[1]==':=':45 if len(line)==3:46 if line[-1][0]=='#':47 return '\tli %s,%s'%(Get_R(line[0]),line[-1].replace('#',''))48 else:49 return '\tmove %s,%s'%(Get_R(line[0]),Get_R(line[2]))50 if len(line)==5:51 if line[3]=='+':52 if line[-1][0]=='#':53 return '\taddi %s,%s,%s'%(Get_R(line[0]),Get_R(line[2]),line[-1].replace('#',''))54 else:55 return '\tadd %s,%s,%s'%(Get_R(line[0]),Get_R(line[2]),Get_R(line[-1]))56 elif line[3]=='-':57 if line[-1][0]=='#':58 return '\taddi %s,%s,-%s'%(Get_R(line[0]),Get_R(line[2]),line[-1].replace('#',''))59 else:60 return '\tsub %s,%s,%s'%(Get_R(line[0]),Get_R(line[2]),Get_R(line[-1]))61 elif line[3]=='*':62 return '\tmul %s,%s,%s'%(Get_R(line[0]),Get_R(line[2]),Get_R(line[-1]))63 elif line[3]=='/':64 return '\tdiv %s,%s\n\tmflo %s'%(Get_R(line[2]),Get_R(line[-1]),Get_R(line[0]))65 elif line[3]=='<':66 return '\tslt %s,%s,%s'%(Get_R(line[0]),Get_R(line[2]),Get_R(line[-1]))67 elif line[3]=='>':68 return '\tslt %s,%s,%s'%(Get_R(line[0]),Get_R(line[-1]),Get_R(line[2])) 69 if line[2]=='CALL':70 if line[3]=='read' or line[3]=='print':71 return '\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\tmove %s,$v0\n\taddi $sp,$sp,4'%(line[-1],Get_R(line[0]))72 else:73 return '\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0'%(line[-1],Get_R(line[0]))74 if line[0]=='GOTO':75 return '\tj %s'%line[1]76 if line[0]=='RETURN':77 return '\tmove $v0,%s\n\tjr $ra'%Get_R(line[1])78 if line[0]=='IF':79 if line[2]=='==':80 return '\tbeq %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])81 if line[2]=='!=':82 return '\tbne %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])83 if line[2]=='>':84 return '\tbgt %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])85 if line[2]=='<':86 return '\tblt %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])87 if line[2]=='>=':88 return '\tbge %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])89 if line[2]=='<=':90 return '\tble %s,%s,%s'%(Get_R(line[1]),Get_R(line[3]),line[-1])91 if line[0]=='FUNCTION':92 return '%s:'%line[1]93 if line[0]=='CALL':94 if line[-1]=='read' or line[-1]=='print':95 return '\taddi $sp,$sp,-4\n\tsw $ra,0($sp)\n\tjal %s\n\tlw $ra,0($sp)\n\taddi $sp,$sp,4'%(line[-1])96 else:97 return '\taddi $sp,$sp,-24\n\tsw $t0,0($sp)\n\tsw $ra,4($sp)\n\tsw $t1,8($sp)\n\tsw $t2,12($sp)\n\tsw $t3,16($sp)\n\tsw $t4,20($sp)\n\tjal %s\n\tlw $a0,0($sp)\n\tlw $ra,4($sp)\n\tlw $t1,8($sp)\n\tlw $t2,12($sp)\n\tlw $t3,16($sp)\n\tlw $t4,20($sp)\n\taddi $sp,$sp,24\n\tmove %s $v0'%(line[-1],Get_R(line[0]))98 if line[0]=='ARG':99 return '\tmove $t0,$a0\n\tmove $a0,%s'%Get_R(line[-1])100 if line[0]=='PARAM':101 table[line[-1]]='a0'102 return ''103def write_to_txt(Obj):104 f=open('result.asm','w')105 template='''106.data107_prompt: .asciiz "Enter an integer:"108_ret: .asciiz "\\n"109.globl main110.text111read:112 li $v0,4113 la $a0,_prompt114 syscall115 li $v0,5116 syscall117 jr $ra118print:119 li $v0,1120 syscall121 li $v0,4122 la $a0,_ret123 syscall124 move $v0,$0125 jr $ra126'''127 f.write(template)128 for line in Obj:129 f.write(line+'\n')130 f.close()131def parser():132 for reg in regs:133 reg_ok[reg]=1 #åå§åï¼ææå¯åå¨é½å¯ç¨134 Inter=Load_Inter('innerCode.txt') #读åä¸é´ä»£ç 135 Load_Var(Inter) #第ä¸éæ«æï¼è®°å½ææåé136 Obj=[]137 for line in Inter:138 obj_line=translate(line) #ç¿»è¯ä¸é´ä»£ç æMIPSæ±ç¼139 if obj_line=='':140 continue141 Obj.append(obj_line)142 write_to_txt(Obj)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!