Hi guys, I hope you like the code I'm posting. This program is written in Python
Programming language and it converts the non parenthesized arithmetic expressions to
Assembly level code.
Try a = b + c / d to test the code.
And this should be the expected output:
The Genereated code is:
LD R0 ,c
LD R1 ,d
DIV R0 ,R0 ,R1
LD R1 ,b
LD R2 ,temp
ADD R1 ,R1 ,R2
ST temp , R1
Please Note: Do include a space (" ") between the operators and operands.
And keep it in your mind that this is a code generator. Hence please do try it with
valid expressions only.
Upgrading the code for implementing the 'if' and looping constructs like 'while' and
'for' and sharing the modified code with us and other coding enthusiasts is really
very well appreciated.
I shall make another post about the explanation of the code soon. Until then,
please do comment on our post and share our link to other coding lovers like yourself!
I hope you like the code guys! Any Correction or scope for improvement is very well
appreciated!
Thank you very much. :)
#-*-*-*-*-*-*-*-*-*-*-*- CODE -*-*-*-*-*-*-*-*-*-*-*-
c = raw_input("Enter the command string\n")
tot_cmd = ""
a = ""
b = ""
res_reg = ""
Free = 1
Held_up = 0
loop_pass = 0
reg_count = 0
R0 = "R0 "
R1 = "R1 "
R2 = "R2 "
R3 = "R3 "
R4 = "R4 "
R5 = "R5 "
R6 = "R6 "
R7 = "R7 "
add = "ADD "
sub = "SUB "
mul = "MUL "
div = "DIV "
LD = "LD "ST = "ST "
temp = "temp"
inst = " "
op_queue = []
#OPERAND QUEUE
or_queue = []
#OPERATOR QUEUE
op_top = -1
or_top = -1
R0f = 1
R1f = 1
R2f = 1
R3f = 1
R4f = 1
R5f = 1
R6f = 1
R7f = 1
cmd = c.split()
################ The functinon to check for operands #############################
def isAlNum(c):
if c >= 'a' and c <= 'z':
return 1
elif c >= 'A' and c <= 'Z':
return 1
elif c >= "0" and c <= "99999":
return 1
else:
return 0
################ The functinon to check for operators #############################
def isOper(c):
if c == '+' or c == '-' or c == '*' or c =='/' or c == '=':
return 1
return 0
################ The functinon to free the registers #############################
def setRegFree(R):
global R0f,R1f,R2f,R3f
global reg_count
if R == "R0 ":
R0f = Free
reg_count -= 1
elif R == "R1 ":
R1f = Free
reg_count -= 1
elif R == "R2 ":
R2f = Free
reg_count -= 1
elif R == "R3 ":
R3f = Free
reg_count -= 1
elif R == "R4 ":
R4f = Free
reg_count -= 1
elif R == "R5 ":
R5f = Free
reg_count -= 1
elif R == "R6 ":
R6f = Free
reg_count -= 1
elif R == "R7 ":
R7f = Free
reg_count -= 1
elif reg_count > 7:
setRegFree(R0)
setRegFree(R1)
setRegFree(R2)
setRegFree(R3)
setRegFree(R4)
setRegFree(R5)
setRegFree(R6)
setRegFree(R7)
################ The functinon to check to allocate registers ###############
#FLAG = 0 -> HELD UP
#FLAG = 1 -> FREE
def getFreeReg():
global R0f,R1f,R2f,R3f,R4f,R5f,R6f,R7f
global reg_count
if R0f == Free:
R0f = Held_up
reg_count += 1
return "R0 "
elif R1f == Free:
R1f = Held_up
reg_count += 1
return "R1 "
elif R2f == Free:
R2f = Held_up
reg_count += 1
return "R2 "
elif R3f == Free:
R3f = Held_up
reg_count += 1
return "R3 "
elif R4f == Free:
R4f = Held_up
reg_count += 1
return "R4 "
elif R5f == Free:
R5f = Held_up
reg_count += 1
return "R5 "
elif R6f == Free:
R6f = Held_up
reg_count += 1
return "R6 "
elif R7f == Free:
R7f = Held_up
reg_count += 1
return "R7 "
elif reg_count > 5:
setRegFree(R0)
setRegFree(R1)
setRegFree(R2)
setRegFree(R3)
setRegFree(R4)
setRegFree(R5)
setRegFree(R6)
setRegFree(R7)
return getFreeReg()
####################### START APPENDING THE COMMANDS INTO APPROPRIATE QUEUES ######
for i in cmd:
if isAlNum(i):
op_top += 1
op_queue.append(i)
elif isOper(i):
or_top += 1
or_queue.append(i)
########################### START THE CODE GENERATION #####################
i = 0
opr = str(or_queue.pop())
#print opror_top -= 1
while( 1 ):
if opr == False:
exit(0)
if opr == '+':
inst = add
elif opr == '-':
inst = sub
elif opr == '*':
inst = mul
elif opr == '/':
inst = div
if opr != '=' and isOper(opr) and op_top :
op2 = op_queue.pop()
op1 = op_queue.pop()
op_top -= 2
a = getFreeReg()
b = getFreeReg()
tot_cmd += LD + str(a) + " ," + str(op1) + "\n"
tot_cmd += LD + str(b) + " ," + str(op2) + "\n"
######### SET USED OPERATOR REGISTERS FREE ###########################
if (loop_pass % 2 == 0) or reg_count >= 3:
setRegFree(a)
setRegFree(b)
temp_op = op_queue.pop()
op_top -= 1
if temp_op != "=":
op_queue.append(temp_op)
op_queue.append(temp)
op_top += 2
res_reg = str(getFreeReg())
tot_cmd += inst+ res_reg + " ," + str(a) + " ," + str(b) + "\n"
######### SET USED RESULT REGISTERS FREE ###########################
if reg_count > 2 or loop_pass > 3:
setRegFree(res_reg)
opr = str(or_queue.pop())
or_top -= 1
if opr == "=":
op1 = op_queue.pop()
tot_cmd += ST + str(op1) + " , " + res_reg + "\n"
loop_pass += 1 if loop_pass > 5:
loop_pass = loop_pass - 5
if or_top < 0:
break
print "\n\nThe Genereated code is:\n\n\n"
print tot_cmd
#-*-*-*-*-*-*-*-*-*-*-*- CODE ENDS -*-*-*-*-*-*-*-*-*-*-*-
Thank You so much for visitng this post!
Keep visiting the page for more Posts.
Very nice.
ReplyDeleteHope it will benifit everyone.
Thank you very much. I hope the same too.
Delete