Professional Documents
Culture Documents
Compilers File
Compilers File
Write a two pass assembler for 8085 Making the list of opcodes
opCodeFile = file("opcodeslisting.txt") tempString = opCodeFile.read() tempString = tempString.upper() opCodesList = tempString.split("\n") opCodesMap = {} for i in range(0,len(opCodesList)): if len(opCodesList[i]): tempList = opCodesList[i].split("|") if len(tempList) >= 3: opCodesMap[tempList[1].rstrip()] = tempList[2].rstrip() #for k in opCodesMap: # print k + "=>" + opCodesMap[k]
def createMachineCode(symbolTable,code,opcodesMap): loc = symbolTable['codestartshere'] + 1 lines = code.split("\n") objectCode = {} for line in lines: if line[0:3] == "ORG": continue #if line[0] == ";": # continue #print line line = line.rstrip().lstrip() if ":" in line: line = line.split(":")[1] if line in opcodemap.opCodesMap.keys(): objectCode[loc] = opcodemap.opCodesMap[line] loc += 1 elif line[-3:-1].isdigit() or line[-5:-1].isdigit(): codeLine = line.split(",") print codeLine[-1] if int(codeLine[-1][:-1]) >= 0xFF: objectCode[loc] = opcodemap.opCodesMap[codeLine[0]+",NN"] loc += 1 objectCode[loc] = codeLine[1][-3:-1] loc += 1 objectCode[loc] = codeLine[1][-5:-3] loc += 1 else: objectCode[loc] = opcodemap.opCodesMap[codeLine[0]+",N"] loc += 1 objectCode[loc] = codeLine[1][-3:-1] loc += 1 else: line = line.split(' ') objectCode[loc] = opcodemap.opCodesMap[line[0] +" A"] loc += 1 if len(str(symbolTable[line[1]])) > 2: objectCode[loc] = str(symbolTable[line[1]])[-3:-1] loc += 1 objectCode[loc] = str(symbolTable[line[1]])[0:2] else: objectCode[loc] = symbolTable[line[1]] return objectCode print "Assembly code" print code machineCode = createMachineCode(buildSymbolTable(code),code,opcodemap.opCodesMap) print "Machine code" for k in machineCode.keys(): print k, print " ", print machineCode[k] #print opcodemap.opCodesMap
DELIM [ \t\n] WHITESPACE {DELIM}+ DIGIT [0-9] LETTER [a-zA-Z] ID {LETTER}({LETTER}|{DIGIT})* NUMBER {DIGIT}+(\.{DIGIT}+)?(e[+\-]?{DIGIT}+)? %% {DIGIT}+
{ printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); } {DIGIT}+"."{DIGIT}* { printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); } if|else|return|main|include|int|float|char { printf( "A keyword: %s\n", yytext ); } {ID} printf( "An identifier: %s\n", yytext ); "+"|"-"|";"|"="|"("|")"|"{"|"}"|"<"|">"|"*"|"/" printf( "An operator: %s\n", yytext ); "{"[^}\n]*"}" /* eat up one-line comments */ [ \t\n]+ /* eat up whitespace */ . printf( "Unrecognized character: %s\n", yytext ); %% main( argc, argv ) int argc; char **argv; { ++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); else yyin = stdin; yylex(); }
File.c
Output
[S@localhost ~]$ [S@localhost ~]$ [S@localhost ~]$ [S@localhost ~]$ A keyword: main An operator: ( An operator: ) An operator: {
flex lex.l gcc -c lex.yy.c gcc -o final lex.yy.o -lfl ./final file.c
A keyword: int An identifier: a1 Unrecognized character: , An identifier: a2 An operator: ; A keyword: float An identifier: f1 An operator: = A float: 9.09 (9.09) An operator: ; An identifier: a1 An operator: = An integer: 10 (10) An operator: ; A keyword: if An operator: ( An identifier: a1 An operator: > An integer: 10 (10) An operator: ) An identifier: a2 An operator: = An identifier: a1 An operator: ; An operator: } [S@localhost ~]$
Lex Code
%{ #include "y.tab.h" %} %% [0-9]+ [ \t\n] [-+*/();] . %% int yywrap (void) {return 1;} {yylval.a_number = atoi(yytext); return number;} ; {return yytext[0];} {ECHO; yyerror ("unexpected character");}
YACC code
%{ #include <stdio.h> %}
%union {int a_number;} %start line %token <a_number> number %type <a_number> exp term factor
%%
line
: exp ';' ;
exp
term
Output
labwork@nikhil-laptop:~/Desktop/compilers/calc$ bison -d calcy.y
labwork@nikhil-laptop:~/Desktop/compilers/calc$ mv calcy.tab.h y.tab.h labwork@nikhil-laptop:~/Desktop/compilers/calc$ gcc -c calcy.tab.c calcy.y:29: warning: conflicting types for yyerror calcy.tab.c:1420: note: previous implicit declaration of yyerror was here labwork@nikhil-laptop:~/Desktop/compilers/calc$ flex calcl.l labwork@nikhil-laptop:~/Desktop/compilers/calc$ gcc -c lex.yy.c labwork@nikhil-laptop:~/Desktop/compilers/calc$ gcc lex.yy.o calcy.tab.o -o calc labwork@nikhil-laptop:~/Desktop/compilers/calc$ ./calc 1 + (2*3); result is 7 1 - 5; result is -4