From 6403fa7eb8221e593e28789b63f0982588c81874 Mon Sep 17 00:00:00 2001 From: Rebecca Date: Thu, 25 Apr 2019 19:17:59 +0530 Subject: [PATCH] Assemblers --- Assembler/fig2_1.asm | 56 ++++++ Assembler/symbol_table.py | 24 +++ ICG/infix.py | 42 +++++ ICG/postfix.py | 23 +++ Macros/.idea/Macros.iml | 12 ++ Macros/.idea/encodings.xml | 4 + .../inspectionProfiles/Project_Default.xml | 13 ++ Macros/.idea/misc.xml | 7 + Macros/.idea/modules.xml | 8 + Macros/.idea/vcs.xml | 6 + Macros/.idea/workspace.xml | 177 ++++++++++++++++++ Macros/assembly.c | 26 +++ Macros/macros.py | 39 ++++ 13 files changed, 437 insertions(+) create mode 100644 Assembler/fig2_1.asm create mode 100644 Assembler/symbol_table.py create mode 100644 ICG/infix.py create mode 100644 ICG/postfix.py create mode 100644 Macros/.idea/Macros.iml create mode 100644 Macros/.idea/encodings.xml create mode 100644 Macros/.idea/inspectionProfiles/Project_Default.xml create mode 100644 Macros/.idea/misc.xml create mode 100644 Macros/.idea/modules.xml create mode 100644 Macros/.idea/vcs.xml create mode 100644 Macros/.idea/workspace.xml create mode 100644 Macros/assembly.c create mode 100644 Macros/macros.py diff --git a/Assembler/fig2_1.asm b/Assembler/fig2_1.asm new file mode 100644 index 0000000..b08e3ba --- /dev/null +++ b/Assembler/fig2_1.asm @@ -0,0 +1,56 @@ +. +. Modified from figure 2.1 in the textbook +. input device : DEV04 +. output device: DEV05 +. +COPY START 1000 +FIRST STL RETADR +CLOOP JSUB RDREC + LDA LENGTH + COMP ZERO + JEQ ENDFIL + JSUB WRREC + J CLOOP +ENDFIL LDA EOF + STA BUFFER + LDA THREE + STA LENGTH + JSUB WRREC + LDL RETADR + RSUB +EOF BYTE C'EOF' +THREE WORD 3 +ZERO WORD 0 +RETADR RESW 1 +LENGTH RESW 1 +BUFFER RESB 4096 +. +. SUBROUTINE TO READ RECORD INTO BUFFER +. +RDREC LDX ZERO + LDA ZERO +RLOOP TD INPUT + JEQ RLOOP + RD INPUT + COMP ZERO + JEQ EXIT + STCH BUFFER + TIX MAXLEN + JLT RLOOP +EXIT STX LENGTH + RSUB +INPUT BYTE X'04' +MAXLEN WORD 4096 +. +. SUBROUTINE TO WRITE RECORD FROM BUFFER +. +WRREC LDX ZERO +WLOOP TD OUTPUT + JEQ WLOOP + LDCH BUFFER + WD OUTPUT + TIX LENGTH + JLT WLOOP + RSUB +OUTPUT BYTE X'05' + END FIRST diff --git a/Assembler/symbol_table.py b/Assembler/symbol_table.py new file mode 100644 index 0000000..3bad954 --- /dev/null +++ b/Assembler/symbol_table.py @@ -0,0 +1,24 @@ +file = open('fig2_1.asm') +instructions, symbols, symbol_table = [],{},{} +for line in file.readlines(): + if line[0]!='.': + line = line.replace('\n','').split(' ') + instructions.append(list(filter(lambda x : x!='', line))) + if len(instructions[-1]) == 3: symbols[instructions[-1][0]] = False +variable = {'WORD':3,'BYTE':1} +buffer = {'RESB':1,'RESW':3} +for instruction in instructions: + if len(instruction) == 1:continue + elif len(instruction) == 2: + if symbols[instruction[1]]:continue + else:symbol_table[instruction[1]] = [instruction[1],None,None] + else: + if symbols[instruction[0]]:continue + else: + if instruction[1] in variable.keys(): symbol_table[instruction[0]] = [instruction[0],variable[instruction[1]],instruction[2]] + elif instruction[1] in buffer.keys(): symbol_table[instruction[0]] = [instruction[0],buffer[instruction[1]]*int(instruction[2]),'--'] + else: symbol_table[instruction[0]] = [instruction[0],3,'--'] + symbols[instruction[0]] = True + if instruction[2] in symbols.keys() and symbols[instruction[2]]==False: symbol_table[instruction[2]] = [instruction[2],None,None] +for symbol in symbol_table.values(): + print("{0}".format(symbol[0]).ljust(10),"{0}".format(symbol[1]).ljust(5),"{0}".format(symbol[2]).ljust(5)) diff --git a/ICG/infix.py b/ICG/infix.py new file mode 100644 index 0000000..06796c9 --- /dev/null +++ b/ICG/infix.py @@ -0,0 +1,42 @@ +infix = input("Enter infix expression : ").replace(' ','') +result = input("Enter resultant variable : ") +temp,string,operators = {},[],['+','-','*','/'] +count,cp = 1,0 + +for i in range(len(infix)): + if infix[i] in operators: + string.append(infix[cp:i]) + string.append(infix[i]) + cp = i+1 + +string.append(infix[cp:]) + +for i in range(len(string)): + if '[' in string[i]: + + temp['t'+str(count)] = ('*',string[i][string[i].index('[')+1],'8') + count += 1 + temp['t'+str(count)] = ('[]',string[i][:string[i].index('[')],'t'+str(count-1)) + string[i] = 't'+str(count) + count += 1 + + +while len(string)!=1: + + temp['t'+str(count)] = (string[1],string[0],string[2]) + string = string[3:] + string = ['t'+str(count)] + string + count += 1 + +print('\n','THREE ADDRESS CODES'.center(15,' ')) +for k,v in temp.items(): + if v[0]!='[]': + print(k.center(3,' '),'='.center(3,' '),v[1].center(3,' '),v[0].center(3,' '),v[2]) + else : + print(k.center(3,' '),'='.center(3,' '),v[1].center(3,' '),'['+v[2]+']') +print(result.center(3,' '), '='.center(3,' '), 't{0}'.format(count-1).center(3,' ')) + +temp[result] = ('=','t'+str(count-1),'---') +print("\n","QUADRUPLE TABLE".center(20,' ')) +print('op'.center(3,' '),'arg1'.center(5,' '),'arg2'.center(5,' '),'result'.center(7,' ')) +for k,v in temp.items(): print(v[0].center(3,' '),v[1].center(5,' '),v[2].center(5,' '),k.center(7,' ')) diff --git a/ICG/postfix.py b/ICG/postfix.py new file mode 100644 index 0000000..a1243e7 --- /dev/null +++ b/ICG/postfix.py @@ -0,0 +1,23 @@ +postfix = input("Enter postfix expression : ").replace(' ','') +result = input("Enter resultant variable : ") +temp,string,stack,operators = {},[],[_ for _ in postfix],['+','-','*','/'] +count = 1 +while len(stack)!=0: + if stack[0] in operators: + temp['t'+str(count)] = (stack[0],string[0],string[1]) + stack[0] = 't'+str(count) + string = [] + count += 1 + else: + string.append(stack[0]) + stack = stack[1:] + +print('\n','THREE ADDRESS CODES'.center(15,' ')) +for k,v in temp.items(): print(k.center(3,' '),'='.center(3,' '),v[1].center(3,' '),v[0].center(3,' '),v[2].center(3,' ')) +print(result.center(3,' '), '='.center(3,' '), 't{0}'.format(count-1).center(3,' ')) + + +temp[result] = ('=','t'+str(count-1),'---') +print("\n","QUADRUPLE TABLE".center(20,' ')) +print('op'.center(3,' '),'arg1'.center(5,' '),'arg2'.center(5,' '),'result'.center(7,' ')) +for k,v in temp.items(): print(v[0].center(3,' '),v[1].center(5,' '),v[2].center(5,' '),k.center(7,' ')) diff --git a/Macros/.idea/Macros.iml b/Macros/.idea/Macros.iml new file mode 100644 index 0000000..7c9d48f --- /dev/null +++ b/Macros/.idea/Macros.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Macros/.idea/encodings.xml b/Macros/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/Macros/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Macros/.idea/inspectionProfiles/Project_Default.xml b/Macros/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..c405810 --- /dev/null +++ b/Macros/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/Macros/.idea/misc.xml b/Macros/.idea/misc.xml new file mode 100644 index 0000000..3999087 --- /dev/null +++ b/Macros/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Macros/.idea/modules.xml b/Macros/.idea/modules.xml new file mode 100644 index 0000000..21bf2e1 --- /dev/null +++ b/Macros/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Macros/.idea/vcs.xml b/Macros/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Macros/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Macros/.idea/workspace.xml b/Macros/.idea/workspace.xml new file mode 100644 index 0000000..4c19eeb --- /dev/null +++ b/Macros/.idea/workspace.xml @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +