One pass assembler for 8086 assembly code

73

By prabhakar gouda

One pass assembler

iPods with lower price

Apple iPod touch 8GB (4th Generation) - Black - Current Version
Amazon Price: $184.99
List Price: $199.00

8086 Instruction  AAA, AAS, AAM, ASCII adjust for addition, subtraction, multiplication, division (ASCII
AAD codes 30-39)
Data Transfer Instructions
DAA, DAS Decimal adjust for addition, subtraction (binary coded decimal numbers)
Transfer Instructions
MOV Move byte or word to register or memory
IN, OUT Input byte or word from port, output word to port JMP Unconditional jump (short ?127/8, near ?32K, far between segments)
LEA Load effective address
LDS, LES Load pointer using data segment, extra segment Conditional jumps:
PUSH, POP Push word onto stack, pop word off stack
XCHG Exchange byte or word
JA (JNBE) Jump if above (not below or equal)? +127, -128 range only
XLAT Translate byte using look-up table
JAE (JNB) Jump if above or equal(not below)? +127, -128 range only
Logical Instructions JB (JNAE) Jump if below (not above or equal)? +127, -128 range only
JBE (JNA) Jump if below or equal (not above)? +127, -128 range only
NOT Logical NOT of byte or word (one's complement) JE (JZ) Jump if equal (zero)? +127, -128 range only
AND Logical AND of byte or word JG (JNLE) Jump if greater (not less or equal)? +127, -128 range only
OR Logical OR of byte or word JGE (JNL) Jump if greater or equal (not less)? +127, -128 range only
XOR Logical exclusive-OR of byte or word JL (JNGE) Jump if less (not greater nor equal)? +127, -128 range only
TEST Test byte or word (AND without storing) JLE (JNG) Jump if less or equal (not greater)? +127, -128 range only
JC, JNC Jump if carry set, carry not set? +127, -128 range only
Shift and Rotate Instructions
JO, JNO Jump if overflow, no overflow? +127, -128 range only
JS, JNS Jump if sign, no sign? +127, -128 range only
SHL, SHR Logical shift left, right byte or word? by 1 or CL JNP (JPO) Jump if no parity (parity odd)? +127, -128 range only
SAL, SAR Arithmetic shift left, right byte or word? by 1 or CL JP (JPE) Jump if parity (parity even)? +127, -128 range only
ROL, ROR Rotate left, right byte or word? by 1 or CL
RCL, RCR Rotate left, right through carry byte or word? by 1 or CL Loop control:
Arithmetic Instructions
LOOP Loop unconditional, count in CX, short jump to target address
ADD, SUB Add, subtract byte or word LOOPE (LOOPZ) Loop if equal (zero), count in CX, short jump to target address
ADC, SBB Add, subtract byte or word and carry (borrow) LOOPNE (LOOPNZ) Loop if not equal (not zero), count in CX, short jump to target address
INC, DEC Increment, decrement byte or word JCXZ Jump if CX equals zero (used to skip code in loop)
NEG Negate byte or word (two's complement)
Subroutine and Interrupt Instructions
CMP Compare byte or word (subtract without storing)
MUL, DIV Multiply, divide byte or word (unsigned)
CALL, RET Call, return from procedure (inside or outside current segment)
IMUL, IDIV Integer multiply, divide byte or word (signed)
INT, INTO Software interrupt, interrupt if overflow
CBW, CWD Convert byte to word, word to double word (useful before multiply/divide)
IRET Return from interrupt
Adjustments after arithmetic operations:
String Instructions
1 of 3 9/4/01 5:04 PM 2 of 3 9/4/01 5:04 PM
8086 Instruction Set Summary file:///D|/notes/8086inst.html
MOVS Move byte or word string
MOVSB, MOVSW Move byte, word string
CMPS Compare byte or word string
SCAS Scan byte or word string (comparing to A or AX)
LODS, STOS Load, store byte or word string to AL or AX
Repeat instructions (placed in front of other string operations):
REP Repeat
REPE, REPZ Repeat while equal, zero
REPNE, REPNZ Repeat while not equal (zero)
Processor Control Instructions
Flag manipulation:
STC, CLC, CMC Set, clear, complement carry flag
STD, CLD Set, clear direction flag
STI, CLI Set, clear interrupt enable flag
LAHF, SAHF Load AH from flags, store AH into flags
PUSHF, POPF Push flags onto stack, pop flags off stack
Coprocessor, multiprocessor interface:
ESC Escape to external processor interface
LOCK Lock bus during next instruction
Inactive states:
NOP No operation
WAIT Wait for TEST pin activity
HLT Halt processor

Code written in Lex

%{ 
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#define  disp  printf("1");	
long locctr;
char myOut[200][100],myOut1[200][100];
int myOutDec[200],myOutLoc[200];
int myOutRef=0,printFlag =1,re1,re2,seg,n,im,m1,m2,l=0,op1,op2,i=0,j=0,mnem,error,g=0,k=0;
char fname[20],s[50],c,temp1[50],temp2[50],temp3[50],obname[20],w,d[5],w1[5],w2[2],num[20],x[5],datal[4],datah[4],t[100],mo[3],reg[4],rim[4],dis[10],binbufferl[10],binbufferh[10];
struct fwd{
              char name[50];
              int myOutLoc;
		char myOutIns[10];
          }res[50];
FILE *ob,*sp,*tp;
%}
%%
"end"   {strcpy(t,"\0");}

"@data"  {
		if(op1==1&&op2==1)
		strcpy(t,"1000100000000000");
	  }
"ax"|"AX" {  if(op1==1&&op2==0)
		{
			strcpy(d,"1");
			strcpy(reg,"000");
			strcpy(w1,"1");
			
			re1=1;
		}
		if(op1==1&&op2==1)
		{
		 if(m1==1)
		 strcpy(reg,"000");
		 else
		 strcpy(rim,"000");
		 strcpy(w2,"1");re2=1;
		}
	    }
"al"|"AL" {  if(op1==1&&op2==0)
                {
                       strcpy(reg,"000");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"000");     
                 else
                 strcpy(rim,"000");
                 strcpy(w2,"0");re2=1;  
                }
            }
		
"cx"|"CX" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"001");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"001");     
                 else
                strcpy(rim,"001");
                 strcpy(w2,"1");re2=1;  
                }
            }
"cl"|"CL" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"001");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"001");     
                 else
                 strcpy(rim,"001");
                 strcpy(w2,"0");re2=1;  
                }
            }

"dx"|"DX" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"010");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"010");     
                 else
                 strcpy(rim,"010");
                 strcpy(w2,"1");re2=1;  
                }
            }

"dl"|"DL" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"010");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                strcpy(reg,"010");     
                 else
                 strcpy(rim,"010");
                 strcpy(w2,"0");re2=1;  
                }
            }
"bx"|"BX" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"011");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"011");     
                 else
                 strcpy(rim,"011");
                 strcpy(w2,"1");re2=1;  
                }
            }
"bl"|"BL" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"011");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"011");     
                 else
                 strcpy(rim,"011");
                 strcpy(w2,"0");re2=1;  
                }
            }
"ah"|"AH" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"100");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"100");
                 else
                 strcpy(rim,"100");
                 strcpy(w2,"0");re2=1;
                }
            }
"ch"|"CH" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"101");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"101");
                 else
                 strcpy(rim,"101");
                 strcpy(w2,"0");re2=1;
                }
            }

"dh"|"DH" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"110");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"110");
                 else
                 strcpy(rim,"110");
                 strcpy(w2,"0");re2=1;
                }
            }
"bh"|"BH" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"111");
                        strcpy(w1,"0");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"111");
                 else
                 strcpy(rim,"111");
                 strcpy(w2,"0");re2=1;
                }
            }
"sp"|"SP" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"100");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"100");
                 else
                 strcpy(rim,"100");
                 strcpy(w2,"1");re2=1;
                }
            }

"bp"|"BP" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"101");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"101");
                 else
                 strcpy(rim,"101");
                 strcpy(w2,"1");re2=1;
                }
            }
"si"|"SI" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"110");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                strcpy(reg,"110");
                 else
                 strcpy(rim,"110");
                 strcpy(w2,"1");re2=1;
                }
            }
"di"|"DI" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"111");
                        strcpy(w1,"1");strcpy(d,"1");re1=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"111");
                 else
                 strcpy(rim,"111");
                 strcpy(w2,"1");re2=1;
                }
            }

"es"|"ES" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"000");
                        strcpy(w1,"1");strcpy(d,"1");seg=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"000");
                 else
                 strcpy(rim,"000");
                 strcpy(w2,"1");seg=1;
                }
            }

"cs"|"CS" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"001");
                        strcpy(w1,"1");strcpy(d,"1");seg=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"001");
                 else
                 strcpy(rim,"001");
                 strcpy(w2,"1");seg=1;
                }
            }

"ss"|"SS" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"010");
                        strcpy(w1,"1");strcpy(d,"1");seg=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"010");
                 else
                 strcpy(rim,"010");
                 strcpy(w2,"1");seg=1;
                }
            }

"ds"|"DS" {  if(op1==1&&op2==0)
                {
                        strcpy(reg,"011");
                        strcpy(w1,"1");strcpy(d,"1");seg=1;
                }
                if(op1==1&&op2==1)
                {
                 if(m1==1)
                 strcpy(reg,"011");
                 else
                 strcpy(rim,"011");
                 strcpy(w2,"1");seg=1;
                }
            }
[0-9]*[a-f]*[A-F]*"[si]"|[0-9]*[a-f]*[A-F]*"[SI]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"100");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"100");
           								m2=1;
               								}
							}	
[0-9]*[a-f]*[A-F]*"[di]"|[0-9]*[a-f]*[A-F]*"[DI]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"10");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"101");
           								m2=1;
               								}
							}	
[0-9]*[a-f]*[A-F]*"[bp]"|[0-9]*[a-f]*[A-F]*"[BP]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"110");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"110");
           								m2=1;
               								}
							}
[0-9]*[a-f]*[A-F]*"[bx]"|[0-9]*[a-f]*[A-F]*"[BX]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"111");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"111");
           								m2=1;
               								}
							}
[0-9]*[a-f]*[A-F]*"[bx][si]"|[0-9]*[a-f]*[A-F]*"[BX][SI]"|[0-9]*[a-f]*[A-F]*"[si][bx]"|[0-9]*[a-f]*[A-F]*"[SI][BX]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							  if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"000");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"000");
           								m2=1;
               								}
							}
[0-9]*[a-f]*[A-F]*"[bx][di]"|[0-9]*[a-f]*[A-F]*"[BX][DI]"|[0-9]*[a-f]*[A-F]*"[di][bx]"|[0-9]*[a-f]*[A-F]*"[DI][BX]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"001");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"001");
           								m2=1;
               								}
							}				
[0-9]*[a-f]*[A-F]*"[bp][si]"|[0-9]*[a-f]*[A-F]*"[BP][SI]"|[0-9]*[a-f]*[A-F]*"[si][bp]"|[0-9]*[a-f]*[A-F]*"[SI][BP]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"010");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"010");
           								m2=1;
               								}
							}	
[0-9]*[a-f]*[A-F]*"[bp][di]"|[0-9]*[a-f]*[A-F]*"[BP][DI]"|[0-9]*[a-f]*[A-F]*"[di][bp]"|[0-9]*[a-f]*[A-F]*"[DI][BP]" {   strcpy(dis,yytext);
							k=0;
							if(isdigit(dis[k])||isalpha(dis[k]))
							{
							 while(dis[k]!='[')
							 { 
								num[l++]=dis[k++];
								}
							num[l]='\0';
							n=atoi(num);
							}
							 if(op1==1&&op2==0)
        						        {
                        					strcpy(rim,"011");
     						                   strcpy(d,"0");m1=1;
                							}
             						 if(op1==1&&op2==1)
           						     {
              					 	            strcpy(rim,"011");
           								m2=1;
               								}
							}		

[0-9]*[a-f]*[A-F]*                        {im=1;
                                             strcpy(x,yytext);
                                            if(strlen(x)>4)
                                            error=1;

                                            if(strlen(x)==3)
                                             datah[0]='0';
                                             strcat(datah,x);
                                             strcpy(x,datah);
                                             datal[0]=x[2];
                                             datal[1]=x[3];
                                             datah[0]=x[0];
                                             datah[1]=x[1];
                                             datah[2]=datal[2]='\0'; 

                                           }
                                                      
"push"|"PUSH"     {   if(re1==1)
                                 {
                                 	strcpy(t,"01010");
                                 	strcat(t,reg);
								 }				
					  if(m1==1)
					             {
					             	strcpy(t,"11111111");
					             	if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
						         }
                     if(seg==1)
                                 {
                     	         strcpy(t,"000");
                     	          strcat(t,reg);
                     	          strcat(t,"110");
					             }
                }
                
"pop"|"POP"      {   if(re1==1)
                                 {

                                              



             	strcpy(t,"01011");
                                 	strcat(t,reg);
								 }				
					  if(m1==1)
					             {
					             	strcpy(t,"10001111");
					             	if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
						         }
                     if(seg==1)
                                 {
                     	          strcpy(t,"000");
                     	          strcat(t,reg);
                     	          strcat(t,"111");
					             }
                }  
                
"lahf"|"LAHF"    {strcpy(t,"10011111");}
"sahf"|"SAHF"    {strcpy(t,"10011110");}
"pushf"|"PUSHF"  {strcpy(t,"10011100");}
"popf"|"POPF"    {strcpy(t,"10011101");}
"clc"|"CLC"      {strcpy(t,"11111000");}
"div"|"DIV"      {       if(re1==1)
                                  { 
                                  strcpy(t,"1111011");
                                  strcat(t,w1);
                                  strcat(t,"11110");
                                  strcat(t,reg);
								  }
                         if(m1==1)
                                  {
                        	       strcpy(t,"11110111");
                        	       
                        	       if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
			                         strcat(t,"110");
									 strcat(t,rim);					  
								  }
                 }

"mul"|"MUL"       {       if(re1==1)
                                  { 
                                 strcpy(t,"1111011");
                                  strcat(t,w1);
                                  strcat(t,"11100");
                                  strcat(t,reg);
								  }
                         if(m1==1)
                                  {
                        	       strcpy(t,"11110111");
                        	       if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
			                         strcat(t,"100");
									 strcat(t,rim);					  
								  }
                 }
"and"|"AND"        {if(re1==1&&re2==1)
                                    {
                                    	strcpy(t,"001000");
                                    	strcat(t,d);
                                    	strcat(t,w1);
                                    	strcat(t,"11");
                                    	strcat(t,reg);
                                    	strcat(t,rim);
									}
					if(re2==1&&m1==1)
					{
					               strcpy(t,"001000");
								   strcat(t,d);
								   strcat(t,w2);
								   if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
								   	
					}   
					if(re1==1&&m2==1)
					{
						strcpy(t,"001000");
						strcat(t,d);
						strcat(t,w1);
						if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
					}
	            }
	            
"or"|"OR"    {if(re1==1&&re2==1)
                                    {
                                    	strcpy(t,"000010");
                                    	strcat(t,d);
                                    	strcat(t,w1);
                                    	strcat(t,"11");
                                    	strcat(t,reg);
                                    	strcat(t,rim);
									}
					if(re2==1&&m1==1)
					{
					               strcpy(t,"000010");
								   strcat(t,d);
								   strcat(t,w2);
								   if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
								   	
					}   
					if(re1==1&&m2==1)
					{
						strcpy(t,"000010");
						strcat(t,d);
						strcat(t,w1);
						if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
					}
	            }
"xor"|"XOR"  {if(re1==1&&re2==1)
                                    {
                                    	strcpy(t,"001100");
                                    	strcat(t,d);
                                    	strcat(t,w1);
                                    	strcat(t,"11");
                                    	strcat(t,reg);
                                    	strcat(t,rim);
									}
					if(re2==1&&m1==1)
					{
					               strcpy(t,"001100");
								   strcat(t,d);
								   strcat(t,w2);
								   if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
								   	
					}   
					if(re1==1&&m2==1)
					{
						strcpy(t,"001100");
						strcat(t,d);
						strcat(t,w1);
						if(n)
					             	{
					             		if(n<=255)
					             		strcat(t,"01");
					             		else
					             		strcat(t,"10");
									 }
									 else 
									 strcat(t,"00");
									 strcat(t,reg);
									 strcat(t,rim);
					}
	            }
"movsb"|"MOVSB"     {strcpy(t,"10100100");}
"movsw"|"MOVSW"     {strcpy(t,"10100101");}
"jmp"|"JMP"         {strcpy(t,"11101001");
			strcat(t,"********");
			g=0;
			while(res[g].name==NULL)g++;
			strcpy(res[g].name,temp2);
			res[g].myOutLoc=myOutRef;
			strcpy(res[g].myOutIns,"11101001");
			 }
"call"|"CALL"      {strcpy(t,"11101000");
                        strcat(t,"********"); 
                        g=0; 
                        while(res[g].name==NULL)g++;
                        strcpy(res[g].name,temp2); 
                       res[g].myOutLoc=myOutRef;
			strcpy(res[g].myOutIns,"11101000");
                         }

"ret"|"RET"  {strcpy(t,"11000011");}
"je"|"jz"|"JE"|"JZ" {strcpy(t,"01110100");
                        strcat(t,"********"); 
                        g=0; 
                        while(res[g].name==NULL)g++;
                        strcpy(res[g].name,temp2); 
                        res[g].myOutLoc=myOutRef;
			strcpy(res[g].myOutIns,"01110100");
                         }
"cmp"|"CMP" {if(re1==1&&re2==1)
                                    {
                                        strcpy(t,"001110");
                                        strcat(t,d);
                                        strcat(t,w1);
                                        strcat(t,"11");
                                        strcat(t,reg);
                                        strcat(t,rim);
                                                                        }
                                        if(re2==1&&m1==1)
                                        {
                                                       strcpy(t,"001110");
                                                                   strcat(t,d);
                                                                   strcat(t,w2);
                                                                   if(n)
                                                        {
                                                                if(n<=255)
                                                                strcat(t,"01");
                                                                else
                                                                strcat(t,"10");
                                                                         }
                                                                         else
                                                                         strcat(t,"00");
                                                                         strcat(t,reg);
                                                                         strcat(t,rim);

                                        }
                                        if(re1==1&&m2==1)
                                        {
                                                    	
                          		      strcpy(t,"001110");
                                                strcat(t,d);
                                                strcat(t,w1);
                                                if(n)
                                                        {
                                                                if(n<=255)
                                                                strcat(t,"01");
                                                                else
                                                                strcat(t,"10");
                                                                         }
                                                                         else
                                                                         strcat(t,"00");
                                                                         strcat(t,reg);
                                                                         strcat(t,rim);
                                        }
                    }

                  

"mov"|"MOV"  {           if(seg==1)
                                {strcpy(t,"10001110");strcat(t,mo);strcat(t,"0");strcat(t,reg);strcat(t,rim);}   
                                    if(re1==1&&re2==1)
                                    {
                                        strcpy(t,"100010");
					if(((!strcmp(w1,"1")) && (!strcmp(w2,"1"))||((!strcmp(w2,"0"))))||((!strcmp(w1,"0"))&& (!strcmp(w2,"0"))))
                                        {strcat(t,d);
                                        strcat(t,w1);
                                        strcat(t,"11");
                                        strcat(t,reg);
                                        strcat(t,rim);}
					error=1;
                                                                        }
                                        if(re2==1&&m1==1)
                                        {
                                                       strcpy(t,"100010");
                                             strcat(t,d);
                                                                   strcat(t,w2);
                                                                   if(n)
                                                        {
                                                                if(n<=255)
                                                                strcat(t,"01");
                                                                else
                                                                strcat(t,"10");
                                                                         }
                                                                         else
                                                                         strcat(t,"00");
                                                                         strcat(t,reg);
                                                                         strcat(t,rim);

                                        }
                                        if(re1==1&&m2==1)
                                        {

                                       strcpy(t,"100010");
                                                strcat(t,d);
                                                strcat(t,w1);
                                                if(n)
                                                        {
                                                                if(n<=255)
                                                                strcat(t,"01");
                                                                else
                                                                  strcat(t,"10");
                                                                         }
                                                                         else
                                                                         strcat(t,"00");
                                                                         strcat(t,reg);
                                                                         strcat(t,rim);
                                        }
					if(re1==1&& im==1)
					{
						strcpy(t,"1011");
						strcat(t,w1);
						strcat(t,reg);
						strcat(t,binbufferl);
						strcat(t,binbufferh);

                                               }
                    

Content of Makefile

a.out: lex.yy.c final.l
	gcc -ggdb lex.yy.c -ll
lex.yy.c:final.l
	lex final.l	
PHONY:clean
clean:
	rm a.out lex.yy.c out.obj	

Contd . . .

	void parse() 	{
		FILE *tp1;
		tp1 = fopen("temper","r");
		yyin=tp1;
		yylex();
		fclose(tp1);
	}

	void dec2bin(long decimal, char *binary) 	{
		int k = 0, n = 0;
		int neg_flag = 0;
		int remain;
		int old_decimal; 
		char temp[80];
		if (decimal < 0)
		{
			decimal = -decimal;
			neg_flag = 1;
		}
		do
		{
			old_decimal = decimal; 
			remain = decimal % 2;
			decimal = decimal / 2;
			temp[k++] = remain + '0';
		} while (decimal > 0);
		if (neg_flag)
			temp[k++] = '-';
		while (k >= 0)
			binary[n++] = temp[--k];
			binary[n-1] = 0;
		}

	int bin2dec(char *bin)	{
		int b, k, m, n;
		int len, sum = 0;
		len = strlen(bin) - 1;
		for(k = 0; k <= len; k++)
		{
			n = (bin[k] - '0');
			if ((n > 1) || (n < 0))
			{
				puts("\n\n ERROR! BINARY has only 1 and 0!\n");
				return (0);
			}
			for(b = 1, m = len; m > k; m--)
			{
				// 1 2 4 8 16 32 64 ... place-values, reversed here
				b *= 2;
			}
			// sum it up
			sum = sum + n * b;
			}
return(sum);
}

	void resolve()	{
		int k=0;
		char v;
		char binary[30];
		char myName[30];
		strcpy(myName,temp1);
		strcat(myName,"\n");
		while(k<50)
		{	
			if(strcmp(res[k].name,myName)==0)
			{
				dec2bin(locctr,binary);
        		strcpy(myOut1[res[k].myOutLoc],res[k].myOutIns);
        		strcat(myOut1[res[k].myOutLoc],binary);
			}
        	k++;
		}
}

void file_ext(char *s,char * l)	{
	int z=0;
	while(s[z]!='.')
    {    
    	l[z]=s[z];
		z++;
	}
    strcat(l,".obj");
	printf("getout fileext");
}

int main()	{
////////////////File HAndling Var Declaration///////////////////////
	static const char filename[] = "data.txt";
	FILE *file = fopen ( filename, "r" );
	int i, j,lineCount=0;
	char line[128],header[50];
	char bin[10],psy[30];
	ob = fopen("out.obj","w");
	while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
	{
		mnem=0;op1=0;op2=0;im=0;re1=0;re2=0;seg=0;
		strcpy(temp1,"\0");
		strcpy(temp2,"\0");
		strcpy(temp3,"\0");	
		strcpy(d,"\0");
		strcpy(w1,"\0");
		strcpy(w2,"\0");
		strcpy(reg,"\0");
		strcpy(rim,"\0");
		strcpy(mo,"\0");
		strcpy(datal,"\0");
		strcpy(datah,"\0");
		strcpy(t,"\0");
	i=0;
	strcat(line,"\0");
	strcpy(s,line);
	strcpy(line,"\0");
	strcpy(psy,s);
 	printFlag = 1;
        while(s[i]!='\0')
        {	if (printFlag ==1)
		{
		
                while(s[i]==' ')
                i++;
                j=0;
                while(s[i]!=' '&& s[i]!=':')
                 temp1[j++]=s[i++];
                 temp1[j]='\0';
                while(s[i]==' ')
                i++;
        	if(s[i]==':')
		{
		printFlag = 0;
		
		resolve();
		}       
		else
                {
                  while(s[i]==' ')
                  i++;
                  j=0;
                  while(s[i]!=','&& s[i]!='\0')
                temp2[j++]=s[i++];
                temp2[j]='\0';
                if(temp2!='\0')
                 op1=1;
                tp=fopen("temper","w");
                fprintf(tp,"%s",temp2);
                fclose(tp);
                parse();

                if(s[i]=='\0')
                {
		op2=0;
                tp=fopen("temper","w");
                fprintf(tp,"%s",temp1);
                fclose(tp);
                 parse();
                }
                else if(s[i]==',' && s[++i]=='\0')
                error=1;
                else
                {
                  j=0;
                  while(s[i]!='\0')
                  temp3[j++]=s[i++];
                   temp3[j]='\0';
                  if(temp3!='\0')
                  op2=1;
		   	
                  tp=fopen("temper","w");
                   fprintf(tp,"%s",temp3);
                  fclose(tp);
                  parse();
		  if(im==1)
		 {
		   n=atoi(datal);	
		   dec2bin(n,binbufferl);
		   n=atoi(datah);
		   dec2bin(n,binbufferh);		
			}	
		
                  tp=fopen("temper","w");
                 fprintf(tp,"%s",temp1);
                 fclose(tp);
                 mnem=1;
		 parse();
                }
                }
		}
		else
		s[i]='\0';
        }
	if(printFlag == 1){
	if(strcmp(t,"\0")!=0)
	{
	myOutLoc[myOutRef]=locctr;
	strcat(myOut[myOutRef]," ");
	strcat(myOut1[myOutRef],t);
	strcat(myOut[myOutRef]," ");
	strcat(myOut[myOutRef],psy);
	myOutRef++;
        locctr+=strlen(t)/8;
	}
	}
}
	strcpy(header,"LocationCounter  ObjectCode  AssemblyCode     \n");
	fprintf(ob," %s",header);
	for(i=0;i<myOutRef;i++)
	{
		 myOutDec[i] = bin2dec(myOut1[i]);
	}
	for(i=0;i<myOutRef;i++)
	{
		fprintf(ob,"   0x%04X       ",myOutLoc[i]);
		fprintf(ob,"   0x%04X   ",myOutDec[i]);
		fprintf(ob," %s",myOut[i]);
	}
	fclose(ob);
}

More

  • Implementation of Handwritten lexer and parser

    Overview β - parse language is the beta version of parsing of our own language. It involves the best possibilities that we have seen from the classic language ‘C’. Our language is purely a subset of the higher level language - ’C’. It takes... - 15 months ago

  • C for TCS placement

    1. Which of the following is invalid (a) a+=b (b) a*=b (c) a>>=b (d) a**=b 2. What is y value of the code if input x=10 y=5; if (x==10) else if(x==9) else y=8; (a)9 ... - 17 months ago

  • Toughest Interview Questions

    1. Talk about yourself. 2. What was your last salary? 3. Have you ever laid off or fired a person? 4. How do you react when you realize that you have made a mistake? 5. Are you willing to lower your salary... - 17 months ago

  • simulation of solar system using OpenGL

    #include&lt;GL/glut.h&gt; #include&lt;stdio.h&gt; #include&lt;math.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #define XCOORDINATE 0 #define YCOORDINATE 1 #define ZCOORDINATE 2 #define ROT_ANGLE 2 #define SUN 0 #define MERCURY... - 17 months ago

  • some C problems for placement

    1. what is the error in the following sequence of program.           int i1;     switch(i1)     {         printf("The value of I1 is :");         case 1: printf("%d",i1);             break;         case... - 17 months ago

  • tricky pacement c language questions

    1:    Why doesn't the code "a[i] = i++;" work?   A:      The variable i is both referenced and modified in the same         expression.   2:    Under my compiler, the code "int i = 7; ... - 17 months ago

  • best aptitude questions for placement

    Aptitude Questions 1.If 2x-y=4 then 6x-3y=? (a)15 (b)12 (c)18 (d)10 Ans. (b) 2.If x=y=2z and xyz=256 then what is the value of x? (a)12 (b)8 (c)16 (d)6 Ans. (b) 3. (1/10)18 - (1/10)20 = ? ... - 17 months ago

  • C# ,NET assembles

    Each of the applications developed in this book’s first ten chapters were along the lines of traditional “stand-alone” applications, given that all of your custom programming logic was contained within a single executable file (*.exe).... - 17 months ago

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    • No HTML is allowed in comments, but URLs will be hyperlinked
    • Comments are not for promoting your Hubs or other sites

    Please wait working