An opcode is an instruction. An operand is information used by the opcode. Not all opcodes require operands.
Each mnemonic maps directly to a machine instruction code, known as an opcode. Some mnemonics map to more than one opcode, however the instruction's operand types will determine which specific opcode will be generated.
#include<stdio.h> #include<conio.h> #include<string.h> struct symbol { char sn[35]; int add; }s[35]; void main() { int symerr=0,oper=0,i=1,j=1,lc,k,sa,p1,f,op,l,q,d,x=1; char opcode[50],lb[25],operand[25]; char optab[10][10]={"LDA","LDB","STA","ADD","COMP","J","JEQ","SUB","STA"}; char value[10][10]={"00","b8","0c","18","28","3C","30","lc","14","78"}; FILE *fp1,*fp2; clrscr(); fp1=fopen("input1.txt","r"); fp2=fopen("inter.txt","w"); fscanf(fp1,"%s %s",&lb,&opcode); if(strcmp(opcode,"START")==0) { fscanf(fp1,"%d",&lc); sa=lc; fprintf(fp2,"%d\t %s\t %s\t %d\n",lc,lb,opcode,lc); fscanf(fp1,"%s %s",&lb,&opcode); } while(!feof(fp1)) { d=lc; if(strcmp(".",lb)!=0) { if(strcmp("-",lb)!=0) { for(i=1;i<=j;i++) { if(strcmp(s[i].sn,lb)==0) symerr=1; break; } if(symerr==0) { strcpy(s[j].sn,lb); s[j].add=lc; j++; } } for(k=0;k<10;k++) for(l=0;l<10;l++) if(optab[k][l]==opcode) { fscanf(fp1,"%s",&operand); lc=lc+3; x=0; } if(strcmp(opcode,"RESW")==0) { fscanf(fp1,"%s",&operand); op=atoi(operand); lc=lc+(op*3); } else if(strcmp(opcode,"RESB")==0) { fscanf(fp1,"%s",&operand); op=atoi(operand); lc=lc+op; } else if(strcmp(opcode,"BYTE")==0) { fscanf(fp1,"%s",&operand); f=strlen(operand); lc=lc+(f-3); } else if(strcmp(opcode,"WORD")==0) { fscanf(fp1,"%s",&operand); lc=lc+3; } else { if(x==1) { fscanf(fp1,"%s",operand); lc=lc+3; } } } fprintf(fp2,"\n%d\t%s\t%s\t%s\n",d,lb,opcode,operand); if(symerr==1) fprintf(fp2,"\n**DUPLICATE SYMBOL**\n"); if(oper==1) fprintf(fp2,"\n**INVALID OPERATION CODE**\n"); fscanf(fp1,"%s\t%s",&lb,&opcode); symerr=0; oper=0; } p1=lc-sa; fprintf(fp2,"\nThe program length is %d",p1); printf("\n symbol table\tlabeL address\n"); for(q=1;q<j;q++) printf("\n%s\t%d",s[q].sn,s[q].add); fcloseall(); getch(); }
#include<stdio.h> #include<string.h> #include<stdlib.h> void chk_label(); void chk_opcode(); void READ_LINE(); struct optab { char code[10],objcode[10]; }myoptab[3]={ {"LDA","00"}, {"JMP","01"}, {"STA","02"} }; struct symtab{ char symbol[10]; int addr; }mysymtab[10]; int startaddr,locctr,symcount=0,length; char line[20],label[8],opcode[8],operand[8],programname[10]; void PASS1() { FILE *input,*inter; input=fopen("input.txt","r"); inter=fopen("inter.txt","w"); printf("LOCATION LABEL\tOPERAND\tOPCODE\n"); printf("_____________________________________"); fgets(line,20,input); READ_LINE(); if(!strcmp(opcode,"START")) { startaddr=atoi(operand); locctr=startaddr; strcpy(programname,label); fprintf(inter,"%s",line); fgets(line,20,input); } else { programname[0]='\0'; startaddr=0; locctr=0; } printf("\n %d\t %s\t%s\t %s",locctr,label,opcode,operand); while(strcmp(line,"END")!=0) { READ_LINE(); printf("\n %d\t %s \t%s\t %s",locctr,label,opcode,operand); if(label[0]!='\0') chk_label(); chk_opcode(); fprintf(inter,"%s %s %s\n",label,opcode,operand); fgets(line,20,input); } printf("\n %d\t\t%s",locctr,line); fprintf(inter,"%s",line); fclose(inter); fclose(input); } void READ_LINE() { char buff[8],word1[8],word2[8],word3[8]; int i,j=0,count=0; label[0]=opcode[0]=operand[0]=word1[0]=word2[0]=word3[0]='\0'; for(i=0;line[i]!='\0';i++) { if(line[i]!=' ') buff[j++]=line[i]; else { buff[j]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); j=0; count++; } } buff[j-1]='\0'; strcpy(word3,word2); strcpy(word2,word1); strcpy(word1,buff); switch(count) { case 0:strcpy(opcode,word1); break; case 1:strcpy(opcode,word2); strcpy(operand,word1); break; case 2:strcpy(label,word3); strcpy(opcode,word2); strcpy(operand,word1); break; } } void chk_label() { int k,dupsym=0; for(k=0;k<symcount;k++) if(!strcmp(label,mysymtab[k].symbol)) { mysymtab[k].addr=-1; dupsym=1; break; } if(!dupsym) { strcpy(mysymtab[symcount].symbol,label); mysymtab[symcount++].addr=locctr; } } void chk_opcode() { int k=0,found=0; for(k=0;k<3;k++) if(!strcmp(opcode,myoptab[k].code)) { locctr+=3; found=1; break; } if(!found) { if(!strcmp( opcode,"WORD")) locctr+=3; else if (!strcmp(opcode,"RESW")) locctr+=(3*atoi(operand)); else if(!strcmp(opcode,"RESB")) locctr+=atoi(operand); } } int main() { PASS1(); length=locctr-startaddr; }
Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not. int a = 1; int b = ++a; // both a and b are now equal to 2 int a = 1; int b = a++; // a is equal to 2 and b is equal to 1
popfd