From 7eb498875be2fd330c0bb9c6ca41ac5d246a2511 Mon Sep 17 00:00:00 2001 From: ramya shree Date: Mon, 17 Oct 2016 03:00:44 +0100 Subject: [PATCH 1/6] Addq and cmpq --- cpu_assignment_2.c | 123 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 114 insertions(+), 9 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 042f9fb..cc15b98 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -62,7 +62,14 @@ int mul(int inti, int src); int division(int inti, int src); int mod(int inti, int src); int rem =0; - +int addq(int src,int dest); +int cmpq(int src,int dest); +int sete(); +int sets(); +int setns(); +int setl(); +int setle(); +int setg(); /***************Function pointers*******/ @@ -137,7 +144,7 @@ bool cond_codes(){ printf("3. Format for SETL operation: SETL dest Eg: SETL A\n"); printf("4. Format for SETLE operation: SETLE dest Eg: SETLE A\n"); printf("5. Format for SETG operation: SETG dest Eg: SETG A\n"); - printf("6. Format for SETE operation: SETE dest Eg: SETE A\n"); + printf("6. Format for SETE operation: SETE dest Eg: SETE A\n"); char instruction[20]; int len; char *p2 = NULL; @@ -266,6 +273,100 @@ int setle(){ return 0; } } +/******************************************************************************************** +Conditional codes with two operands. +/********************************************************************************************/ +bool condcode_two(){ +printf("1. Format for ADDQ operation: ADDQ src,dest Eg: ADDQ A,B\n"); +printf("2. Format for CMPQ operation: CMPQ src,dest Eg: CMPQ A,B\n"); +char instruction[20]; + int len; + char *p2 = NULL; + fgets(instruction,20,stdin); + len = strlen(instruction); + instruction[len-1] = '\0'; + p1 = strtok(instruction, " "); + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Source register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Destination register should be from Register A-P \n"); + return false; + } + printf("Dest = %s\t Src reg = %s\n",destreg,srcreg); + /*****************************ADDITION**************************/ + if (strcasecmp(p1, "ADDQ")==0) { + reg[dest] = addq(reg[src],reg[dest]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "CMPQ")==0) { + reg[dest] = cmpq(reg[src],reg[dest]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else + { + return false; + } +} +/**************************************ADDQ*******************************************************/ +int addq(int src, int dest){ + temp_reg[9] = src; + temp_reg[10] = dest; + temp_reg[11] = add(temp_reg[9],temp_reg[10]); + if(temp_reg[11] == 0) + { + printf("Zero Flag is set\n"); + flags[1] = 1; + } + if(temp_reg[11] < 0) + { + printf("Sign Flag is set\n"); + flags[2] = 1; + } + if((temp_reg[9]>0 && temp_reg[10]>0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]<0) && temp_reg[10]>=0) + { + printf("Overflow Flag is set\n"); + flags[3] = 1; + } + return temp_reg[11]; +} + +/**************************************CMPQ*******************************************************/ +int cmpq(int src, int dest){ + temp_reg[9] = src; + temp_reg[10] = dest; + temp_reg[11] = sub(temp_reg[9],temp_reg[10]); + if(temp_reg[9] == temp_reg[10]) + { + printf("Zero Flag is set\n"); + flags[1] =1; + } + if(temp_reg[11] < 0) + { + printf("Sign Flag is set\n"); + flags[2] = 1; + } + if((temp_reg[9]>0 && temp_reg[10]<0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]>0) && temp_reg[10]>0) + { + printf("Overflow Flag is set"); + flags[3] = 1; + } + return temp_reg[11]; +} + /********************************************************************************************* Simple Function which calls the respective instruction. A function pointer is implemented to point the functions like add, sub, mul, division and mod. @@ -364,10 +465,6 @@ operand instruction. Also, flags like sign flag, carry flag, overflow flag and z /**************************************************************************************************/ int add(int inti, int src){ - //opcode = memory[pc]; - //dest = memory[pc+1]; - //inti = memory[pc+2]; - //src = memory[pc+3]; int carry; temp_reg[0] = src; temp_reg[1] = inti; @@ -806,8 +903,9 @@ int main(){ printf("****************Instructions Menu****************\n"); printf("1. Load/Store Instruction\n"); printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); - printf("3. Condition Codes - setne,sete,setle,setg,sets,setns,compq\n"); - printf("4. EXIT\n"); + printf("3. Condition Codes - setne,sete,setle,setg,sets,setns,compq,addq\n"); + printf("4. Condition Codes - compq,addq\n"); + printf("5. EXIT\n"); fgets(char_option,5,stdin); sscanf(char_option,"%d",&option); //option = option - '0'; @@ -826,7 +924,7 @@ int main(){ printf("Wrong Instruction\n"); exit(-1); } - break; + break; case 3: res = cond_codes(); if(res == false){ @@ -835,6 +933,13 @@ int main(){ } break; case 4: + res = condcode_two(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + case 5: res = false; break; } From 11e5b0df375f52a32540160ddc4c9f7c8de26548 Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Sun, 30 Oct 2016 04:23:52 -0700 Subject: [PATCH 2/6] Resursion Binary Search --- cpu_assignment_2.c | 180 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 169 insertions(+), 11 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 5287cb3..e0bc43a 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -21,6 +21,22 @@ #define STORE_OPCODE 3 /* Opcode for STORE instruction */ #define LOAD_OPCODE 4 /* Opcode for LOAD instruction */ +#define ADD_OPCODE 5 +#define SUB_OPCODE 6 +#define DIV_OPCODE 7 +#define MUL_OPCODE 8 +#define MOD_OPCODE 9 +#define CMPQ_OPCODE 10 +#define ADDQ_OPCODE 11 +#define SETE_OPCODE 12 +#define SETS_OPCODE 13 +#define SETNS_OPCODE 14 +#define SETL_OPCODE 15 +#define SETLE_OPCODE 16 +#define SETG_OPCODE 17 +#define JMP_OPCODE 18 +#define PUSH_OPCODE 19 +#define POP_OPCODE 20 /******add opcodes*****************************************************************************************************/ @@ -45,13 +61,16 @@ bool flags[4]={ false }; /* flag register for setting various flags*/ int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */ /* Stack Pointer */ - int sp = MEMORY_SIZE-1; +int sp = MEMORY_SIZE-1; char *p1 = NULL; int address,part_address,result; /******Function Declarations*********/ bool load_and_store(); bool alu_operations(); +bool cond_codes(); +bool jump_instructions(); +bool binary_search(); void init_memory(); int execute_load(); int execute_store(); @@ -61,6 +80,7 @@ int sub(int inti, int src); int mul(int inti, int src); int division(int inti, int src); int mod(int inti, int src); +int recursive_binary_search(int, int, int); int rem =0; int addq(int src,int dest); int cmpq(int src,int dest); @@ -134,6 +154,127 @@ int print_values(){ printf("\n\n\n"); return 0; } + +/******************************************************************************************** + + +**********************************************************************************************/ +bool jump_instructions(){ + +} + +/******************************************************************************************** + + +**********************************************************************************************/ +bool binary_search(){ + printf("Enter number of elements for binary search\n"); + char element_char[5]; + int element_int,element; + fgets(element_char,5,stdin); + sscanf(element_char,"%d",&element_int); + int c; + int key,index; + int mem_binary_search = 1900; + printf("Enter elements in ascending order\n"); + scanf("%d",&element); + memory[mem_binary_search] = element; + mem_binary_search = mem_binary_search + 1; + for(c=1;cmemory[mem_binary_search]){ + printf("Elements entered are not in ascending order\n"); + exit(0); + } + mem_binary_search = mem_binary_search + 1; + } + + mem_binary_search = 1900; + printf("The Elements are \n"); + for(c=0;c=%d\n",end,start); + pc = pc+4; + //printf("PC before IF= %d\n",pc); + if(flags[1] == 1 || flags[2] == 1){ + int mid; + mid = sub(end,start); + //printf("Mid after sub = %d\n",mid); + pc = pc+4; + //printf("PC = %d\n",pc); + mid = division(mid,2); + //printf("Mid after division = %d\n",mid); + pc = pc+4; + //printf("PC = %d\n",pc); + mid = add(start,mid); + //printf("Mid after add = %d\n",mid); + pc = pc+4; + //printf("PC = %d\n",pc); + cmpq(memory[mid],key); + pc = pc+4; + //printf("PC after cmpq= %d\n",pc); + if (flags[1]==1){ + // printf("%d==%d\n",memory[mid],key); + return mid; + } + else if(flags[2]==1){ + // printf("%d<%d\n",memory[mid],key); + pc = 500; + return recursive_binary_search(mid+1,end,key); + } + else{ + // printf("%d>%d\n",memory[mid],key); + pc = 500; + return recursive_binary_search(start,mid-1,key); + } + + } + return -1; +} + + + /******************************************************************************************** @@ -351,17 +492,17 @@ int cmpq(int src, int dest){ temp_reg[11] = sub(temp_reg[9],temp_reg[10]); if(temp_reg[9] == temp_reg[10]) { - printf("Zero Flag is set\n"); + //printf("Zero Flag is set\n"); flags[1] =1; } if(temp_reg[11] < 0) { - printf("Sign Flag is set\n"); + //printf("Sign Flag is set\n"); flags[2] = 1; } if((temp_reg[9]>0 && temp_reg[10]<0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]>0) && temp_reg[10]>0) { - printf("Overflow Flag is set"); + //printf("Overflow Flag is set"); flags[3] = 1; } return temp_reg[11]; @@ -509,9 +650,9 @@ destination register. Thus making it a 3 operand instruction. Basically it perfo of the negative number and calls the addition function. /******************************************************************************************************/ int sub(int inti, int src){ - printf("Performing Subtraction \n"); + //printf("Performing Subtraction \n"); temp_reg[4] = src; - temp_reg[5] = inti; //2'S complement + temp_reg[5] = inti; temp_reg[4] = (*fun_ptr_add)(~temp_reg[4], 1); temp_reg[4] = (*fun_ptr_add)(temp_reg[4], temp_reg[5]); return temp_reg[4]; @@ -525,7 +666,7 @@ destination register. Thus making it a 3 operand instruction. Primarily, with th multiplication instruction is carried out. /*********************************************************************************************************/ int mul(int inti, int src){ - printf("Performing Multiplication Operation\n"); + //printf("Performing Multiplication Operation\n"); int result = 0; temp_reg[2] = src; temp_reg[3] = inti; @@ -563,11 +704,11 @@ int division(int inti, int src){ } if (temp_reg[6]!= 0 ) { - printf("dividing"); + // printf("dividing"); while (temp_reg[5]>=temp_reg[6]) { temp_reg[5] = (*fun_ptr_sub)(temp_reg[5],temp_reg[6]); - printf("temp_reg[5] afteR sub %d",temp_reg[5]); +// printf("temp_reg[5] afteR sub %d",temp_reg[5]); result++; } } @@ -589,7 +730,7 @@ int division(int inti, int src){ else{ flags[1] = false; } - printf(" rem = %d \n",temp_reg[0]); + // printf(" rem = %d \n",temp_reg[0]); return result; } @@ -906,7 +1047,9 @@ int main(){ printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); printf("3. Condition Codes - setne,sete,setle,setg,sets,setns,compq,addq\n"); printf("4. Condition Codes - compq,addq\n"); - printf("5. EXIT\n"); + printf("5. JUMP Instructions\n"); + printf("6. Perform Recursive Binary Search\n"); + printf("7. EXIT\n"); fgets(char_option,5,stdin); sscanf(char_option,"%d",&option); //option = option - '0'; @@ -941,6 +1084,21 @@ int main(){ } break; case 5: + res = jump_instructions(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + case 6: + res = binary_search(); + + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + + case 7: res = false; break; } From 80b674c9e458cc951691edffa54b257d0ae04bab Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Wed, 2 Nov 2016 23:02:44 -0700 Subject: [PATCH 3/6] Added all functions for assignment 3 --- cpu_assignment_2.c | 519 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 414 insertions(+), 105 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index e0bc43a..4b53ae8 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -1,9 +1,10 @@ /* ****************************************************************************************************** - PROGRAM NAME: cpu_setup.c + PROGRAM NAME: cpu_3.c OBJECTIVE: Develop Best CPU. - DESCRIPTION: Setup CPU architecture with LOAD and STORE data and ALU operations and FLAGS. + DESCRIPTION: Setup CPU architecture with LOAD, STORE,LEA and ALU operations, Condition Codes, Jump, FLAGS + and Recursive Binary Search with Stack implementations. TEAM MEMBERS: Sanket Dhami, Karthik Sadanand, Ramyashree, Neha Rege - DATE: 8th Oct, 2016 + DATE: 2nd Nov, 2016 ****************************************************************************************************** */ #include @@ -18,7 +19,8 @@ #define DATA_MEMORY_BASE_ADD 1024 /* Base Address of Data Memory = 1024 */ #define INSTR_MEMORY_BASE_ADD 256 /* Base Address of Instruction Memory = 256 */ #define BOOT_MEMORY_BASE_ADD 0 /* Base Address of BIOS = 0 */ - +#define location_recursive_binary_search 500 +#define LOOP_ADDRESS 900 /* Looping function */ #define STORE_OPCODE 3 /* Opcode for STORE instruction */ #define LOAD_OPCODE 4 /* Opcode for LOAD instruction */ #define ADD_OPCODE 5 @@ -37,20 +39,21 @@ #define JMP_OPCODE 18 #define PUSH_OPCODE 19 #define POP_OPCODE 20 + /******add opcodes*****************************************************************************************************/ int memory[MEMORY_SIZE]; /* Total Memory size = 2048 bytes */ int reg[REG_COUNT]; /* Number of General Purpose Registers = 16 */ int temp_reg[TEMP_REG_COUNT]; /* Number of Temporary Registers = 16 */ -enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P}; + enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P}; int src=0, dest=0,inti=0; /* Index for source and destination registers */ int opcode = 0; int offset=0; -int i = 0; + int i = 0; -char *destreg = NULL, *srcreg = NULL,*basereg = NULL, *indexreg = NULL,*intreg = NULL; -bool flags[4]={ false }; /* flag register for setting various flags*/ + char *destreg = NULL, *srcreg = NULL,*basereg = NULL, *indexreg = NULL,*intreg = NULL; + bool flags[4]={ false }; /* flag register for setting various flags*/ /*Carry Flag = flags[0] * Zero Flag = flags[1] * Sign Flag = flags[2] @@ -61,12 +64,13 @@ bool flags[4]={ false }; /* flag register for setting various flags*/ int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */ /* Stack Pointer */ -int sp = MEMORY_SIZE-1; -char *p1 = NULL; -int address,part_address,result; + int sp = MEMORY_SIZE-1; + char *p1 = NULL; + + int address,part_address,result; /******Function Declarations*********/ -bool load_and_store(); +bool load_store_and_lea(); bool alu_operations(); bool cond_codes(); bool jump_instructions(); @@ -74,6 +78,7 @@ bool binary_search(); void init_memory(); int execute_load(); int execute_store(); +int execute_lea(); int print_values(); int add(int inti, int src); int sub(int inti, int src); @@ -90,6 +95,12 @@ int setns(); int setl(); int setle(); int setg(); +void push(int); +int pop(); +int looping(int dest,int count,int max); +bool loops(); +int jump(int loc); +int loopingwhile(int dest,int max); /***************Function pointers*******/ @@ -100,7 +111,7 @@ int (*fun_ptr_division)(int, int) = &division; int (*fun_ptr_mod)(int, int) = &mod; - +/************************Initialize Memory values to 0*******************/ void init_memory( ) { int i; @@ -154,20 +165,21 @@ int print_values(){ printf("\n\n\n"); return 0; } + /****************************************Jump Label*******************************************/ + int jump(int loc) + { + pc = loc; + return pc; + } -/******************************************************************************************** - - -**********************************************************************************************/ -bool jump_instructions(){ - -} /******************************************************************************************** - - +This is the main function for binary search where it asks user to enter elements in ascending +order and the element to be searched. Later it calls recursive_binary_search function which +returns the index for the searched element and displays the position og found element. **********************************************************************************************/ bool binary_search(){ + p1 = "CALL"; printf("Enter number of elements for binary search\n"); char element_char[5]; int element_int,element; @@ -175,98 +187,111 @@ bool binary_search(){ sscanf(element_char,"%d",&element_int); int c; int key,index; - int mem_binary_search = 1900; - printf("Enter elements in ascending order\n"); + temp_reg[12] = 1900; + printf("Enter elements in ascending order. Press enter after each element!!\n"); scanf("%d",&element); - memory[mem_binary_search] = element; - mem_binary_search = mem_binary_search + 1; + memory[temp_reg[12]] = element; + temp_reg[12] = temp_reg[12] + 1; for(c=1;cmemory[mem_binary_search]){ - printf("Elements entered are not in ascending order\n"); + memory[temp_reg[12]] = element; + if(memory[temp_reg[12]-1]>memory[temp_reg[12]]){ + printf("Elements entered are not in ascending order!!\n"); exit(0); } - mem_binary_search = mem_binary_search + 1; + temp_reg[12] = temp_reg[12] + 1; } - mem_binary_search = 1900; + temp_reg[12] = 1900; printf("The Elements are \n"); for(c=0;c=%d\n",end,start); + temp_reg[13] = start; + temp_reg[14] = end; + temp_reg[15] = key; + + cmpq(temp_reg[13],temp_reg[14]); //CMPQ TR13,TR14 + pc = pc+4; - //printf("PC before IF= %d\n",pc); - if(flags[1] == 1 || flags[2] == 1){ - int mid; - mid = sub(end,start); - //printf("Mid after sub = %d\n",mid); + + if(flags[1] == 1 || flags[2] == 1){ + + temp_reg[16] = sub(temp_reg[14],temp_reg[13]); //SUB TR16,TR14,TR13 pc = pc+4; - //printf("PC = %d\n",pc); - mid = division(mid,2); - //printf("Mid after division = %d\n",mid); + temp_reg[16] = division(temp_reg[16],2); //DIV TR16,TR16,2 pc = pc+4; - //printf("PC = %d\n",pc); - mid = add(start,mid); - //printf("Mid after add = %d\n",mid); + temp_reg[16] = add(temp_reg[13],temp_reg[16]); //ADD TR16,TR13,TR16 pc = pc+4; - //printf("PC = %d\n",pc); - cmpq(memory[mid],key); + cmpq(memory[temp_reg[16]],temp_reg[15]); //CMPQ TR16,TR15 pc = pc+4; - //printf("PC after cmpq= %d\n",pc); if (flags[1]==1){ - // printf("%d==%d\n",memory[mid],key); - return mid; + return temp_reg[16]; //RET } else if(flags[2]==1){ - // printf("%d<%d\n",memory[mid],key); - pc = 500; - return recursive_binary_search(mid+1,end,key); + temp_reg[13] = add(temp_reg[16],1); //ADD TR13,TR16,1 + pc = pc+4; + push(pc); //PUSH + jump(location_recursive_binary_search); //JUMP 500 + pc = pop(); //POP + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET + } else{ - // printf("%d>%d\n",memory[mid],key); - pc = 500; - return recursive_binary_search(start,mid-1,key); + temp_reg[14] = sub(temp_reg[16],1); //SUB TR14,TR16,1 + pc = pc +4; + push(pc); //PUSH + jump(location_recursive_binary_search); //JUMP 500 + pc = pop(); //POP + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET + } } @@ -276,8 +301,8 @@ int recursive_binary_search(int start,int end,int key){ /******************************************************************************************** - - +Below are set of condition codes where it sets the register to 1 based on the flag. +For Eg. SETS A will set the register to 1 if Signed Flag is 1. **********************************************************************************************/ bool cond_codes(){ printf("1. Format for SETS operation: SETS dest Eg: SETS A\n"); @@ -293,7 +318,6 @@ bool cond_codes(){ len = strlen(instruction); instruction[len-1] = '\0'; p1 = strtok(instruction, " "); - //printf("%s\n",p1); p2 = strtok(NULL," "); destreg = strtok(p2,","); dest = destreg[0]-'A'; @@ -301,7 +325,6 @@ bool cond_codes(){ printf("Destination register should be from Register A-P \n"); return false; } - //printf("Dest = %s\n",destreg); /**********************************************************************************************/ if (strcasecmp(p1, "SETE")==0) { reg[dest] = sete(); @@ -345,19 +368,12 @@ bool cond_codes(){ print_values(); return true; } -/* else if (strcasecmp(p1, "COMPQ")==0) { - reg[dest] = compq(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - }*/ else{ return false; } } - +/**************************SETE(Sets the destination register if Zero flag is set) ************/ int sete(){ if(flags[1] == true){ @@ -368,6 +384,7 @@ int sete(){ } } +/********************SETS(Sets the destination register if Sign flag is set)*****************/ int sets(){ printf("Sign Flag = %d\n",flags[2]); if(flags[2] == true){ @@ -377,7 +394,7 @@ int sets(){ return 0; } } - +/********************SETNS(Sets the destination register if signed flag is not set***********/ int setns(){ printf("Sign Flag = %d\n",flags[2]); if(flags[2] == false){ @@ -387,7 +404,7 @@ int setns(){ return 0; } } - +/****************SETL(Sets the destination register if (SF^OF))**************************/ int setl(){ if(flags[2]!=flags[3]){ return 1; @@ -396,7 +413,7 @@ int setl(){ return 0; } } - +/**************SETG(Sets the destination register if ~(SF^OF)&~ZF)***********************/ int setg(){ if((!(flags[2]!=flags[3]))&&(!flags[1])){ return 1; @@ -405,7 +422,7 @@ int setg(){ return 0; } } - +/****************SETLE(Sets the destination register if (SF^OF)|ZF)**************************/ int setle(){ if((flags[2]!=flags[3])||(flags[1])){ return 1; @@ -442,7 +459,7 @@ char instruction[20]; return false; } printf("Dest = %s\t Src reg = %s\n",destreg,srcreg); - /*****************************ADDITION**************************/ + if (strcasecmp(p1, "ADDQ")==0) { reg[dest] = addq(reg[src],reg[dest]); printf("Result %d\n", reg[dest]); @@ -598,6 +615,170 @@ bool alu_operations(){ return true; } +/*********************************************************************************************************** +Loops +For loop Increments till the maximum number is reached by the counter. +dowhile loop Increments if the source register is less than than the Comparing register or equal if it is +greater nothing is done. +whiledo loop increments if the source register is less than than the Comparing register if it is equal and +greater nothing is done. +*********************************************************************************************************/ +bool loops() +{ + printf("1. Format for For operation: to increment a number by one till the max is reached For Eg: for B,F\n"); + printf("2. Format for Do..While operation: it increments once without checking condition then it checks the condition to increment for Eg: dowhile B,F \n"); + printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,F\n"); + char instruction[20]; + int len; + char *p2 = NULL; + fgets(instruction,20,stdin); + len = strlen(instruction); + instruction[len-1] = '\0'; + p1 = strtok(instruction, " "); + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Source register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Destination register should be from Register A-P \n"); + return false; + } + printf("Dest = %s\t Src destreg = %s\n",destreg,srcreg); + if (strcasecmp(p1, "for")==0) { + int count = 0; + printf("dest register %d \n",reg[dest]); + cmpq(count,reg[dest]); + pc = pc + 4; + if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + looping(reg[src],count,reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[1]) + { + printf("Cannot increment as the value Max is equal to the count\n"); + print_values(); + return true; + } + } + else if (strcasecmp(p1, "dowhile") == 0) + { + cmpq(reg[src],reg[dest]); + pc = pc + 4; + if(flags[1]) + { + reg[src] = add(reg[src],1); //incrementing + pc = pc + 4; + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + loopingwhile(reg[src],reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if (!flags[1]) + { + reg[src] = add(reg[src],1); //incrementing + pc = pc + 4; + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + } + else if (strcasecmp(p1,"whiledo") == 0) + { + int count = 0; + cmpq(reg[src],reg[dest]); + pc = pc + 4; + if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + loopingwhile(reg[src],reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[1]) + { + printf("Values are equal so cannot increment \n"); + print_values(); + return true; + } + else if (!flags[1]) + { + printf("Cannot increment as the src is greater than the given number so cannot increment\n"); + print_values(); + return true; + } + + } + else { + return false; + } +} +/********************************************Looping********************************************* +Increments till the count reaches the max. this is for "for loop" +************************************************************************************************/ +int looping(int dest,int count,int max) +{ + temp_reg[16] = dest; + temp_reg[11] = 1; + temp_reg[12] = count; + temp_reg[13] = max; + + temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + temp_reg[12] = add(temp_reg[12],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + cmpq(temp_reg[12],temp_reg[13]); + pc = pc + 4; + if(flags[2]) + { + pc = pc + 4; + jump(900); + looping(temp_reg[16],temp_reg[12],temp_reg[13]); + } + reg[src] = temp_reg[16]; + return 0; +} +/********************************************LoopingWhile******************************************** +Increments till the source equal to the destination. this is for "do while" and "while do" +************************************************************************************************/ +int loopingwhile(int dest,int max) +{ + temp_reg[16] = dest; + temp_reg[11] = 1; + temp_reg[13] = max; + + temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + cmpq(temp_reg[16],temp_reg[13]); + pc = pc + 4; + if(flags[2]) //compare check sign + { + pc = pc + 4; + jump(900); + loopingwhile(temp_reg[16],temp_reg[13]); + } + reg[src] = temp_reg[16]; + return 0; +} + + /*****************************************ADDITION FUNCTION***************************************** This function adds the content of inti and src and returns the value. The returned value is stored in @@ -773,10 +954,11 @@ int c= 0 ,sign = 0; } -/*****************************************LOAD AND STORE FUNCTION*****************************************/ -bool load_and_store(){ +/*****************************************LOAD,STORE and LEA FUNCTION*****************************************/ +bool load_store_and_lea(){ printf("1. Format for LOAD operation: LOAD dest_reg,offset(src_reg) Eg: LOAD A,30(RB,RI,4) \n"); printf("2 .Format for STORE opcode: STORE dest_reg,offset(src_reg) Eg: STORE A,30(B) \n"); + printf("3. Format for LEA operation: LEA dest_reg,offset(src_reg) EG: LEA A,30(RB,RI,4) \n"); char *p2 = NULL,*p3 = NULL,*p4 = NULL; char instruction[20]; int s,base,index; @@ -877,6 +1059,104 @@ bool load_and_store(){ return false; } } + else if (strcasecmp(p1, "LEA")==0) { + p2 = strtok(NULL,""); + + destreg = strtok(p2,","); + + dest = destreg[0] - 'A'; + + if(dest > P) { + printf("Destination registers should be from Register A-P \n"); + return false; + } + + p2 = strtok(NULL,""); + + p2 = strtok(p2,"("); + + if(p2==NULL){ + offset = 0; + } + else{ + offset = atoi(p2); + } + + p3 = strtok(NULL,"("); + + if(p3 == NULL){ + if(p2==NULL){ + + } + else{ + p3=p2; + } + } + + p4 =strtok(p3,",)"); + + if(p4 == NULL){ + basereg = NULL; + } + else{ + basereg = p4; + base = basereg[0] - 'A'; /*Index for source register*/ + if(base > P){ + printf("Base registers should be from Register A-P \n"); + return false; + } + } + + p2 = strtok(NULL,""); + + indexreg = strtok(p2,",)"); + + if(indexreg == NULL){ + + } + else{ + index = indexreg[0] - 'A'; /*Index for source register*/ + if(index > P){ + printf("Index registers should be from Register A-P \n"); + return false; + } + } + p2 = strtok(NULL,")"); + + if(p2==NULL){ + s = 0; + } + else{ + s = atoi(strtok(p2,")")); + } + if(s==0 || s==1 || s==2 || s==4 || s==8){ + } + else{ + printf("S should be 1,2,4 or 8\n"); + exit(0); + } + + part_address = reg[base]+s*reg[index]; + + memory[INSTR_MEMORY_BASE_ADD + i] = opcode; + memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; + memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; + memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; + i = i+4; + + result = execute_lea(); + if (result !=0){ + printf("Error in executing Lea Instruction \n"); + return false; + } + result = print_values(); + if (result !=0){ + printf("Error in printing values \n"); + return false; + } + + } + else if (strcasecmp(p1, "STORE")==0) { opcode = STORE_OPCODE; p2 = strtok(NULL," "); @@ -990,7 +1270,7 @@ int execute_load(){ offset = memory[pc+2]; part_address = memory[pc+3]; address = part_address + offset; -// printf("part_address = %d\t full address = %d\n",part_address,address); + if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ reg[dest] = memory[address]; pc = pc + 4; @@ -1002,6 +1282,34 @@ int execute_load(){ } } +/****************Function for LEA Instruction *********************************************/ +/* On executing LEA destreg,offset(srcreg) instruction, the address or + value present in source register is added with offset which will result + into final address. The final address is then loaded directly into the + destination register.*/ +/*******************************************************************************************/ +int execute_lea() { + + opcode = memory[pc]; + dest = memory[pc+1]; + offset = memory[pc+2]; + + part_address = memory[pc+3]; + address = part_address + offset; + + if(address >= INSTR_MEMORY_BASE_ADD && address < MEMORY_SIZE){ + reg[dest] = address; + pc = pc+4; + printf("LEA Instruction executed successfully\n\n\n"); + return 0; +} +else{ + printf("Invalid location\n"); + } + +} + + /****************Function for STORE Instruction *********************************************/ /* On executing STORE destreg,offset(srcreg) instruction, the address or value present in source register is added with offset which will result @@ -1014,7 +1322,7 @@ int execute_store(){ offset = memory[pc+2]; part_address = memory[pc+3]; address = part_address + offset; - printf("part_address = %d\t full address = %d\n",part_address,address); + //printf("part_address = %d\t full address = %d\n",part_address,address); if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ memory[address] = reg[src]; pc = pc + 4; @@ -1026,7 +1334,7 @@ int execute_store(){ } } - +/******************************Main********************************/ int main(){ bool res = true; int option; @@ -1043,20 +1351,18 @@ int main(){ while(res == true){ printf("Enter instructions number from the menu\n"); printf("****************Instructions Menu****************\n"); - printf("1. Load/Store Instruction\n"); + printf("1. Load/Store/LEA Instruction\n"); printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); - printf("3. Condition Codes - setne,sete,setle,setg,sets,setns,compq,addq\n"); + printf("3. Condition Codes - setne,sete,setle,setg,sets,setns\n"); printf("4. Condition Codes - compq,addq\n"); - printf("5. JUMP Instructions\n"); - printf("6. Perform Recursive Binary Search\n"); + printf("5. Perform Recursive Binary Search with Stack operations\n"); + printf("6. Loops implementing Jump Instruction for, do..while, while..do\n"); printf("7. EXIT\n"); fgets(char_option,5,stdin); sscanf(char_option,"%d",&option); - //option = option - '0'; - //printf(" %d\n",option); switch(option){ case 1: - res = load_and_store(); + res = load_store_and_lea(); if(res == false){ printf("Wrong Instruction\n"); exit(-1); @@ -1083,23 +1389,26 @@ int main(){ exit(-1); } break; + case 5: - res = jump_instructions(); + res = binary_search(); if(res == false){ printf("Wrong Instruction\n"); exit(-1); } - break; + exit(0); case 6: - res = binary_search(); - + res = loops(); if(res == false){ printf("Wrong Instruction\n"); exit(-1); } + break; case 7: res = false; + exit(-1); + default: break; } From 118cb6067fa5b2e495e7968a75420ff2f3070410 Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Wed, 2 Nov 2016 23:05:36 -0700 Subject: [PATCH 4/6] Delete cpu_assignment_2.c --- cpu_assignment_2.c | 1417 -------------------------------------------- 1 file changed, 1417 deletions(-) delete mode 100644 cpu_assignment_2.c diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c deleted file mode 100644 index 4b53ae8..0000000 --- a/cpu_assignment_2.c +++ /dev/null @@ -1,1417 +0,0 @@ -/* ****************************************************************************************************** - PROGRAM NAME: cpu_3.c - OBJECTIVE: Develop Best CPU. - DESCRIPTION: Setup CPU architecture with LOAD, STORE,LEA and ALU operations, Condition Codes, Jump, FLAGS - and Recursive Binary Search with Stack implementations. - TEAM MEMBERS: Sanket Dhami, Karthik Sadanand, Ramyashree, Neha Rege - DATE: 2nd Nov, 2016 - ****************************************************************************************************** */ - -#include -#include -#include -#include -#include - -#define MEMORY_SIZE 2048 -#define REG_COUNT 16 -#define TEMP_REG_COUNT 16 -#define DATA_MEMORY_BASE_ADD 1024 /* Base Address of Data Memory = 1024 */ -#define INSTR_MEMORY_BASE_ADD 256 /* Base Address of Instruction Memory = 256 */ -#define BOOT_MEMORY_BASE_ADD 0 /* Base Address of BIOS = 0 */ -#define location_recursive_binary_search 500 -#define LOOP_ADDRESS 900 /* Looping function */ -#define STORE_OPCODE 3 /* Opcode for STORE instruction */ -#define LOAD_OPCODE 4 /* Opcode for LOAD instruction */ -#define ADD_OPCODE 5 -#define SUB_OPCODE 6 -#define DIV_OPCODE 7 -#define MUL_OPCODE 8 -#define MOD_OPCODE 9 -#define CMPQ_OPCODE 10 -#define ADDQ_OPCODE 11 -#define SETE_OPCODE 12 -#define SETS_OPCODE 13 -#define SETNS_OPCODE 14 -#define SETL_OPCODE 15 -#define SETLE_OPCODE 16 -#define SETG_OPCODE 17 -#define JMP_OPCODE 18 -#define PUSH_OPCODE 19 -#define POP_OPCODE 20 - -/******add opcodes*****************************************************************************************************/ - - - int memory[MEMORY_SIZE]; /* Total Memory size = 2048 bytes */ - int reg[REG_COUNT]; /* Number of General Purpose Registers = 16 */ - int temp_reg[TEMP_REG_COUNT]; /* Number of Temporary Registers = 16 */ - enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P}; - int src=0, dest=0,inti=0; /* Index for source and destination registers */ - int opcode = 0; - int offset=0; - int i = 0; - - char *destreg = NULL, *srcreg = NULL,*basereg = NULL, *indexreg = NULL,*intreg = NULL; - bool flags[4]={ false }; /* flag register for setting various flags*/ -/*Carry Flag = flags[0] - * Zero Flag = flags[1] - * Sign Flag = flags[2] - * Overflow flag = flags[3] - * */ - -/* Program Counter */ - int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */ - -/* Stack Pointer */ - int sp = MEMORY_SIZE-1; - char *p1 = NULL; - - int address,part_address,result; - -/******Function Declarations*********/ -bool load_store_and_lea(); -bool alu_operations(); -bool cond_codes(); -bool jump_instructions(); -bool binary_search(); -void init_memory(); -int execute_load(); -int execute_store(); -int execute_lea(); -int print_values(); -int add(int inti, int src); -int sub(int inti, int src); -int mul(int inti, int src); -int division(int inti, int src); -int mod(int inti, int src); -int recursive_binary_search(int, int, int); -int rem =0; -int addq(int src,int dest); -int cmpq(int src,int dest); -int sete(); -int sets(); -int setns(); -int setl(); -int setle(); -int setg(); -void push(int); -int pop(); -int looping(int dest,int count,int max); -bool loops(); -int jump(int loc); -int loopingwhile(int dest,int max); - -/***************Function pointers*******/ - -int (*fun_ptr_add)(int, int) = &add; -int (*fun_ptr_sub)(int, int) = ⊂ -int (*fun_ptr_mul)(int, int) = &mul; -int (*fun_ptr_division)(int, int) = &division; -int (*fun_ptr_mod)(int, int) = &mod; - - -/************************Initialize Memory values to 0*******************/ -void init_memory( ) -{ - int i; - for(i=0;imemory[temp_reg[12]]){ - printf("Elements entered are not in ascending order!!\n"); - exit(0); - } - temp_reg[12] = temp_reg[12] + 1; - } - - temp_reg[12] = 1900; - printf("The Elements are \n"); - for(c=0;c P){ - printf("Destination register should be from Register A-P \n"); - return false; - } -/**********************************************************************************************/ - if (strcasecmp(p1, "SETE")==0) { - reg[dest] = sete(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETS")==0) { - reg[dest] = sets(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETNS")==0) { - reg[dest] = setns(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETL")==0) { - reg[dest] = setl(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETLE")==0) { - reg[dest] = setle(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETG")==0) { - reg[dest] = setg(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else{ - return false; - } -} - -/**************************SETE(Sets the destination register if Zero flag is set) ************/ -int sete(){ - if(flags[1] == true){ - - return 1; - } - else{ - return 0; - } -} - -/********************SETS(Sets the destination register if Sign flag is set)*****************/ -int sets(){ - printf("Sign Flag = %d\n",flags[2]); - if(flags[2] == true){ - return 1; - } - else{ - return 0; - } -} -/********************SETNS(Sets the destination register if signed flag is not set***********/ -int setns(){ - printf("Sign Flag = %d\n",flags[2]); - if(flags[2] == false){ - return 1; - } - else{ - return 0; - } -} -/****************SETL(Sets the destination register if (SF^OF))**************************/ -int setl(){ - if(flags[2]!=flags[3]){ - return 1; - } - else{ - return 0; - } -} -/**************SETG(Sets the destination register if ~(SF^OF)&~ZF)***********************/ -int setg(){ - if((!(flags[2]!=flags[3]))&&(!flags[1])){ - return 1; - } - else{ - return 0; - } -} -/****************SETLE(Sets the destination register if (SF^OF)|ZF)**************************/ -int setle(){ - if((flags[2]!=flags[3])||(flags[1])){ - return 1; - } - else{ - return 0; - } -} -/******************************************************************************************** -Conditional codes with two operands. -/********************************************************************************************/ -bool condcode_two(){ -printf("1. Format for ADDQ operation: ADDQ src,dest Eg: ADDQ A,B\n"); -printf("2. Format for CMPQ operation: CMPQ src,dest Eg: CMPQ A,B\n"); -char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\t Src reg = %s\n",destreg,srcreg); - - if (strcasecmp(p1, "ADDQ")==0) { - reg[dest] = addq(reg[src],reg[dest]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "CMPQ")==0) { - reg[dest] = cmpq(reg[src],reg[dest]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else - { - return false; - } -} -/**************************************ADDQ*******************************************************/ -int addq(int src, int dest){ - temp_reg[9] = src; - temp_reg[10] = dest; - temp_reg[11] = add(temp_reg[9],temp_reg[10]); - if(temp_reg[11] == 0) - { - printf("Zero Flag is set\n"); - flags[1] = 1; - } - if(temp_reg[11] < 0) - { - printf("Sign Flag is set\n"); - flags[2] = 1; - } - if((temp_reg[9]>0 && temp_reg[10]>0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]<0) && temp_reg[10]>=0) - { - printf("Overflow Flag is set\n"); - flags[3] = 1; - } - return temp_reg[11]; -} - -/**************************************CMPQ*******************************************************/ -int cmpq(int src, int dest){ - temp_reg[9] = src; - temp_reg[10] = dest; - temp_reg[11] = sub(temp_reg[9],temp_reg[10]); - if(temp_reg[9] == temp_reg[10]) - { - //printf("Zero Flag is set\n"); - flags[1] =1; - } - if(temp_reg[11] < 0) - { - //printf("Sign Flag is set\n"); - flags[2] = 1; - } - if((temp_reg[9]>0 && temp_reg[10]<0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]>0) && temp_reg[10]>0) - { - //printf("Overflow Flag is set"); - flags[3] = 1; - } - return temp_reg[11]; -} - -/********************************************************************************************* -Simple Function which calls the respective instruction. A function pointer is implemented -to point the functions like add, sub, mul, division and mod. -/********************************************************************************************/ - -bool alu_operations(){ - printf("1. Format for ADD operation: ADD dest_reg,src_reg1,src_reg2 Eg: ADD rd,r1,r2 \n"); - printf("2. Format for SUB operation: SUB dest_reg,src_reg1,src_reg2 Eg: SUB rd,r1,r2 \n"); - printf("3. Format for MUL operation: MUL dest_reg,src_reg1,src_reg2 Eg: MUL rd,r1,r2 \n"); - printf("4. Format for DIV operation: DIV dest_reg,src_reg1,src_reg2 Eg: DIV rd,r1,r2 \n"); - printf("5. Format for MOD operation: MOD dest_reg,src_reg1,src_reg2 Eg: MOD rd,r1,r2 \n"); - char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - intreg = strtok(p2,","); - inti = intreg[0]-'A'; - if(inti > P){ - printf("Intermediate register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\tIntr Reg = %s\tSrc reg = %s\n",destreg,intreg,srcreg); - /*****************************ADDITION**************************/ - if (strcasecmp(p1, "ADD")==0) { - reg[dest] = (*fun_ptr_add)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************SUBTRACTION***************************/ - else if(strcasecmp(p1, "SUB")==0) - { - reg[dest] = (*fun_ptr_sub)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************MULTIPLICATION***************************/ - else if(strcasecmp(p1, "MUL")==0) - { - reg[dest] = (*fun_ptr_mul)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************DIVISION***************************/ - else if(strcasecmp(p1, "DIV")==0) - { - reg[dest] = (*fun_ptr_division)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************MOD***************************/ - else if(strcasecmp(p1, "MOD")==0) - { - reg[dest] = (*fun_ptr_mod)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else{ - return false; - } - return true; -} - -/*********************************************************************************************************** -Loops -For loop Increments till the maximum number is reached by the counter. -dowhile loop Increments if the source register is less than than the Comparing register or equal if it is -greater nothing is done. -whiledo loop increments if the source register is less than than the Comparing register if it is equal and -greater nothing is done. -*********************************************************************************************************/ -bool loops() -{ - printf("1. Format for For operation: to increment a number by one till the max is reached For Eg: for B,F\n"); - printf("2. Format for Do..While operation: it increments once without checking condition then it checks the condition to increment for Eg: dowhile B,F \n"); - printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,F\n"); - char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\t Src destreg = %s\n",destreg,srcreg); - if (strcasecmp(p1, "for")==0) { - int count = 0; - printf("dest register %d \n",reg[dest]); - cmpq(count,reg[dest]); - pc = pc + 4; - if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - looping(reg[src],count,reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[1]) - { - printf("Cannot increment as the value Max is equal to the count\n"); - print_values(); - return true; - } - } - else if (strcasecmp(p1, "dowhile") == 0) - { - cmpq(reg[src],reg[dest]); - pc = pc + 4; - if(flags[1]) - { - reg[src] = add(reg[src],1); //incrementing - pc = pc + 4; - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - loopingwhile(reg[src],reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if (!flags[1]) - { - reg[src] = add(reg[src],1); //incrementing - pc = pc + 4; - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - } - else if (strcasecmp(p1,"whiledo") == 0) - { - int count = 0; - cmpq(reg[src],reg[dest]); - pc = pc + 4; - if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - loopingwhile(reg[src],reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[1]) - { - printf("Values are equal so cannot increment \n"); - print_values(); - return true; - } - else if (!flags[1]) - { - printf("Cannot increment as the src is greater than the given number so cannot increment\n"); - print_values(); - return true; - } - - } - else { - return false; - } -} -/********************************************Looping********************************************* -Increments till the count reaches the max. this is for "for loop" -************************************************************************************************/ -int looping(int dest,int count,int max) -{ - temp_reg[16] = dest; - temp_reg[11] = 1; - temp_reg[12] = count; - temp_reg[13] = max; - - temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - temp_reg[12] = add(temp_reg[12],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - cmpq(temp_reg[12],temp_reg[13]); - pc = pc + 4; - if(flags[2]) - { - pc = pc + 4; - jump(900); - looping(temp_reg[16],temp_reg[12],temp_reg[13]); - } - reg[src] = temp_reg[16]; - return 0; -} -/********************************************LoopingWhile******************************************** -Increments till the source equal to the destination. this is for "do while" and "while do" -************************************************************************************************/ -int loopingwhile(int dest,int max) -{ - temp_reg[16] = dest; - temp_reg[11] = 1; - temp_reg[13] = max; - - temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - cmpq(temp_reg[16],temp_reg[13]); - pc = pc + 4; - if(flags[2]) //compare check sign - { - pc = pc + 4; - jump(900); - loopingwhile(temp_reg[16],temp_reg[13]); - } - reg[src] = temp_reg[16]; - return 0; -} - - - -/*****************************************ADDITION FUNCTION***************************************** -This function adds the content of inti and src and returns the value. The returned value is stored in -destination register. All operations in the add function are bitwise operations.Thus making it a 3 -operand instruction. Also, flags like sign flag, carry flag, overflow flag and zero flag is implemented. -/**************************************************************************************************/ - -int add(int inti, int src){ - int carry; - temp_reg[0] = src; - temp_reg[1] = inti; - while(temp_reg[0]!=0){ - carry = temp_reg[1] & temp_reg[0]; - temp_reg[1] = temp_reg[1] ^ temp_reg[0]; - temp_reg[0] = carry << 1; - } - - if((src>=0 && inti>=0 && temp_reg[1]<0) || (src<0 && inti<0 && temp_reg[1]>=0)){ //Overflow criteria - flags[3] = true; - } - else{ - flags[3] = false; - } - if(carry == 1){ //Carry Criteria - flags[0] = true; - } - else{ - flags[0] = false; - } - if(temp_reg[1]<0){ //Sign Flag criteria - flags[2] = true; - } - else{ - flags[2] = false; - } - if(temp_reg[1]==0){ //Zero Flag criteria - flags[1] = true; - } - else{ - flags[1] = false; - } - return temp_reg[1]; -} - - - -/*****************************************SUBTRACTION FUNCTION***************************************** -This substracts the content of inti and src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. Basically it performs 2's complement -of the negative number and calls the addition function. -/******************************************************************************************************/ -int sub(int inti, int src){ - //printf("Performing Subtraction \n"); - temp_reg[4] = src; - temp_reg[5] = inti; - temp_reg[4] = (*fun_ptr_add)(~temp_reg[4], 1); - temp_reg[4] = (*fun_ptr_add)(temp_reg[4], temp_reg[5]); - return temp_reg[4]; -} - - - -/*****************************************MULTIPLICATION FUNCTION***************************************** -This function multiplies the content of inti and src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. Primarily, with the help of logical left shift, -multiplication instruction is carried out. -/*********************************************************************************************************/ -int mul(int inti, int src){ - //printf("Performing Multiplication Operation\n"); - int result = 0; - temp_reg[2] = src; - temp_reg[3] = inti; - while (temp_reg[2] != 0) - { - if ((temp_reg[2] & 01) != 0) - { - result = (*fun_ptr_add)(result,temp_reg[3]); - } - temp_reg[3] <<= 1; - temp_reg[2] >>= 1; - } - return result; -} - -/*****************************************DIVISION FUNCTION***************************************** -This function divides the content of inti with src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. - -/***************************************************************************************************/ -int division(int inti, int src){ - //printf("Performing Division"); - temp_reg[5] = inti; - temp_reg[6] = src; - int result = 0 ,sign = 0; - if (temp_reg[5]< 0 ) - { - temp_reg[5]=-temp_reg[5]; - sign ^= 1; - } - if (temp_reg[6]< 0 ) - { - temp_reg[6]=-temp_reg[6]; - sign ^= 1; - } - if (temp_reg[6]!= 0 ) - { - // printf("dividing"); - while (temp_reg[5]>=temp_reg[6]) - { - temp_reg[5] = (*fun_ptr_sub)(temp_reg[5],temp_reg[6]); -// printf("temp_reg[5] afteR sub %d",temp_reg[5]); - result++; - } - } - - if (sign) - { - result=-result; - - } - if(result < 0){ //Sign Flag criteria - flags[2] = true; - } - else{ - flags[2] = false; - } - if(result == 0){ //Zero Flag criteria - flags[1] = true; - } - else{ - flags[1] = false; - } - // printf(" rem = %d \n",temp_reg[0]); - return result; -} - -/*****************************************MOD FUNCTION***************************************** -This function performs modulus operation on the content of inti with src and returns the value. -The returned value is stored in destination register. Thus making it a 3 operand instruction. - -/**********************************************************************************************/ -int mod(int inti, int src){ -temp_reg[7] = inti; -temp_reg[8] = src; -int c= 0 ,sign = 0; - if (temp_reg[7]< 0 ) - { - temp_reg[7]=-temp_reg[7]; - sign ^= 1; - } - if (temp_reg[8]< 0 ) - { - temp_reg[8]=-temp_reg[8]; - sign ^= 1; - } - if (temp_reg[8]!= 0 ) - { - - while (temp_reg[7]>=temp_reg[8]) - { - temp_reg[7] = (*fun_ptr_sub)(temp_reg[7],temp_reg[8]); - - c++; - } - } - - if (sign) - { - c=-c; - - } - return temp_reg[7]; - } - - -/*****************************************LOAD,STORE and LEA FUNCTION*****************************************/ -bool load_store_and_lea(){ - printf("1. Format for LOAD operation: LOAD dest_reg,offset(src_reg) Eg: LOAD A,30(RB,RI,4) \n"); - printf("2 .Format for STORE opcode: STORE dest_reg,offset(src_reg) Eg: STORE A,30(B) \n"); - printf("3. Format for LEA operation: LEA dest_reg,offset(src_reg) EG: LEA A,30(RB,RI,4) \n"); - char *p2 = NULL,*p3 = NULL,*p4 = NULL; - char instruction[20]; - int s,base,index; - fgets(instruction,20,stdin); - p1 = strtok(instruction, " "); - if (strcasecmp(p1, "LOAD")==0) { - opcode = LOAD_OPCODE; - p2 = strtok(NULL," "); - - destreg = strtok(p2,","); - - dest = destreg[0] - 'A'; /*Index for Destination register*/ - if(dest > P){ - printf("Destination registers should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL,""); - - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - printf("%d\n",s); - if(s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - result = execute_load(); - if (result !=0){ - printf("Error in executing Load Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - } - else if (strcasecmp(p1, "LEA")==0) { - p2 = strtok(NULL,""); - - destreg = strtok(p2,","); - - dest = destreg[0] - 'A'; - - if(dest > P) { - printf("Destination registers should be from Register A-P \n"); - return false; - } - - p2 = strtok(NULL,""); - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - if(s==0 || s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - - result = execute_lea(); - if (result !=0){ - printf("Error in executing Lea Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - - } - - else if (strcasecmp(p1, "STORE")==0) { - opcode = STORE_OPCODE; - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0] - 'A'; /*Index for Destination register*/ - if(src > P){ - printf("Destination registers should be from Register A-Q \n"); - return false; - } - p2 = strtok(NULL,""); - - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - if(s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = src; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - result = execute_store(); - if (result !=0){ - printf("Error in executing Store Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - return true; - } - else { - printf("Enter valid instructions \n"); - return false; - } - return true; -} - -/****************Function for LOAD Instruction *********************************************/ -/* On executing LOAD destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The value present in final address is then loaded in - destination register.*/ -/*******************************************************************************************/ -int execute_load(){ - opcode = memory[pc]; - dest = memory[pc+1]; - offset = memory[pc+2]; - part_address = memory[pc+3]; - address = part_address + offset; - - if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - reg[dest] = memory[address]; - pc = pc + 4; - printf("LOAD Instruction executed successfully\n\n\n"); - return 0; - } - else{ - printf("Invalid location\n"); - } -} - -/****************Function for LEA Instruction *********************************************/ -/* On executing LEA destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The final address is then loaded directly into the - destination register.*/ -/*******************************************************************************************/ -int execute_lea() { - - opcode = memory[pc]; - dest = memory[pc+1]; - offset = memory[pc+2]; - - part_address = memory[pc+3]; - address = part_address + offset; - - if(address >= INSTR_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - reg[dest] = address; - pc = pc+4; - printf("LEA Instruction executed successfully\n\n\n"); - return 0; -} -else{ - printf("Invalid location\n"); - } - -} - - -/****************Function for STORE Instruction *********************************************/ -/* On executing STORE destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The value present in destination register is then - loaded in memory's final address.*/ -/*******************************************************************************************/ -int execute_store(){ - opcode = memory[pc]; - src = memory[pc+1]; - offset = memory[pc+2]; - part_address = memory[pc+3]; - address = part_address + offset; - //printf("part_address = %d\t full address = %d\n",part_address,address); - if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - memory[address] = reg[src]; - pc = pc + 4; - printf("STORE Instruction executed successfully\n\n\n"); - return 0; - } - else{ - printf("Invalid location\n"); - } -} - -/******************************Main********************************/ -int main(){ - bool res = true; - int option; - char char_option[5]; - init_memory(); - reg[0] = 1024; /*Initialized Reg A = 1024 */ - reg[1] = 30; /*Initialized Reg B = 1024 */ - reg[2] =-103; - reg[3] =-69; - reg[4] = 2147483647; - reg[5] = 100; - result = print_values(); - while(pc < MEMORY_SIZE){ - while(res == true){ - printf("Enter instructions number from the menu\n"); - printf("****************Instructions Menu****************\n"); - printf("1. Load/Store/LEA Instruction\n"); - printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); - printf("3. Condition Codes - setne,sete,setle,setg,sets,setns\n"); - printf("4. Condition Codes - compq,addq\n"); - printf("5. Perform Recursive Binary Search with Stack operations\n"); - printf("6. Loops implementing Jump Instruction for, do..while, while..do\n"); - printf("7. EXIT\n"); - fgets(char_option,5,stdin); - sscanf(char_option,"%d",&option); - switch(option){ - case 1: - res = load_store_and_lea(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 2: - res = alu_operations(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 3: - res = cond_codes(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 4: - res = condcode_two(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - - case 5: - res = binary_search(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - exit(0); - case 6: - res = loops(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - - case 7: - res = false; - exit(-1); - default: - break; - } - - } - } -} From 12dc5b4d47159384e69cec0ace42bc946469839b Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Wed, 2 Nov 2016 23:08:13 -0700 Subject: [PATCH 5/6] Assignment 3 --- cpu_assignment_2.c | 1418 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1418 insertions(+) create mode 100644 cpu_assignment_2.c diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c new file mode 100644 index 0000000..cfe22c1 --- /dev/null +++ b/cpu_assignment_2.c @@ -0,0 +1,1418 @@ +/* ****************************************************************************************************** + PROGRAM NAME: cpu_3.c + OBJECTIVE: Develop Best CPU. + DESCRIPTION: Setup CPU architecture with LOAD, STORE,LEA and ALU operations, Condition Codes, Jump, FLAGS + and Recursive Binary Search with Stack implementations. + TEAM MEMBERS: Sanket Dhami, Karthik Sadanand, Ramyashree, Neha Rege + DATE: 2nd Nov, 2016 + ****************************************************************************************************** */ + +#include +#include +#include +#include +#include + +#define MEMORY_SIZE 2048 +#define REG_COUNT 16 +#define TEMP_REG_COUNT 16 +#define DATA_MEMORY_BASE_ADD 1024 /* Base Address of Data Memory = 1024 */ +#define INSTR_MEMORY_BASE_ADD 256 /* Base Address of Instruction Memory = 256 */ +#define BOOT_MEMORY_BASE_ADD 0 /* Base Address of BIOS = 0 */ +#define location_recursive_binary_search 500 +#define LOOP_ADDRESS 900 /* Looping function */ +#define STORE_OPCODE 3 /* Opcode for STORE instruction */ +#define LOAD_OPCODE 4 /* Opcode for LOAD instruction */ +#define ADD_OPCODE 5 +#define SUB_OPCODE 6 +#define DIV_OPCODE 7 +#define MUL_OPCODE 8 +#define MOD_OPCODE 9 +#define CMPQ_OPCODE 10 +#define ADDQ_OPCODE 11 +#define SETE_OPCODE 12 +#define SETS_OPCODE 13 +#define SETNS_OPCODE 14 +#define SETL_OPCODE 15 +#define SETLE_OPCODE 16 +#define SETG_OPCODE 17 +#define JMP_OPCODE 18 +#define PUSH_OPCODE 19 +#define POP_OPCODE 20 + +/******add opcodes*****************************************************************************************************/ + + + int memory[MEMORY_SIZE]; /* Total Memory size = 2048 bytes */ + int reg[REG_COUNT]; /* Number of General Purpose Registers = 16 */ + int temp_reg[TEMP_REG_COUNT]; /* Number of Temporary Registers = 16 */ + enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P}; + int src=0, dest=0,inti=0; /* Index for source and destination registers */ + int opcode = 0; + int offset=0; + int i = 0; + + char *destreg = NULL, *srcreg = NULL,*basereg = NULL, *indexreg = NULL,*intreg = NULL; + bool flags[4]={ false }; /* flag register for setting various flags*/ +/*Carry Flag = flags[0] + * Zero Flag = flags[1] + * Sign Flag = flags[2] + * Overflow flag = flags[3] + * */ + +/* Program Counter */ + int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */ + +/* Stack Pointer */ + int sp = MEMORY_SIZE-1; + char *p1 = NULL; + + int address,part_address,result; + +/******Function Declarations*********/ +bool load_store_and_lea(); +bool alu_operations(); +bool cond_codes(); +bool jump_instructions(); +bool binary_search(); +void init_memory(); +int execute_load(); +int execute_store(); +int execute_lea(); +int print_values(); +int add(int inti, int src); +int sub(int inti, int src); +int mul(int inti, int src); +int division(int inti, int src); +int mod(int inti, int src); +int recursive_binary_search(int, int, int); +int rem =0; +int addq(int src,int dest); +int cmpq(int src,int dest); +int sete(); +int sets(); +int setns(); +int setl(); +int setle(); +int setg(); +void push(int); +int pop(); +int looping(int dest,int count,int max); +bool loops(); +int jump(int loc); +int loopingwhile(int dest,int max); + +/***************Function pointers*******/ + +int (*fun_ptr_add)(int, int) = &add; +int (*fun_ptr_sub)(int, int) = ⊂ +int (*fun_ptr_mul)(int, int) = &mul; +int (*fun_ptr_division)(int, int) = &division; +int (*fun_ptr_mod)(int, int) = &mod; + + +/************************Initialize Memory values to 0*******************/ +void init_memory( ) +{ + int i; + for(i=0;imemory[temp_reg[12]]){ + printf("Elements entered are not in ascending order!!\n"); + exit(0); + } + temp_reg[12] = temp_reg[12] + 1; + } + + temp_reg[12] = 1900; + printf("The Elements are \n"); + for(c=0;c P){ + printf("Destination register should be from Register A-P \n"); + return false; + } +/**********************************************************************************************/ + if (strcasecmp(p1, "SETE")==0) { + reg[dest] = sete(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "SETS")==0) { + reg[dest] = sets(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "SETNS")==0) { + reg[dest] = setns(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "SETL")==0) { + reg[dest] = setl(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "SETLE")==0) { + reg[dest] = setle(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "SETG")==0) { + reg[dest] = setg(); + //printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else{ + return false; + } +} + +/**************************SETE(Sets the destination register if Zero flag is set) ************/ +int sete(){ + if(flags[1] == true){ + + return 1; + } + else{ + return 0; + } +} + +/********************SETS(Sets the destination register if Sign flag is set)*****************/ +int sets(){ + printf("Sign Flag = %d\n",flags[2]); + if(flags[2] == true){ + return 1; + } + else{ + return 0; + } +} +/********************SETNS(Sets the destination register if signed flag is not set***********/ +int setns(){ + printf("Sign Flag = %d\n",flags[2]); + if(flags[2] == false){ + return 1; + } + else{ + return 0; + } +} +/****************SETL(Sets the destination register if (SF^OF))**************************/ +int setl(){ + if(flags[2]!=flags[3]){ + return 1; + } + else{ + return 0; + } +} +/**************SETG(Sets the destination register if ~(SF^OF)&~ZF)***********************/ +int setg(){ + if((!(flags[2]!=flags[3]))&&(!flags[1])){ + return 1; + } + else{ + return 0; + } +} +/****************SETLE(Sets the destination register if (SF^OF)|ZF)**************************/ +int setle(){ + if((flags[2]!=flags[3])||(flags[1])){ + return 1; + } + else{ + return 0; + } +} +/******************************************************************************************** +Conditional codes with two operands. +/********************************************************************************************/ +bool condcode_two(){ +printf("1. Format for ADDQ operation: ADDQ src,dest Eg: ADDQ A,B\n"); +printf("2. Format for CMPQ operation: CMPQ src,dest Eg: CMPQ A,B\n"); +char instruction[20]; + int len; + char *p2 = NULL; + fgets(instruction,20,stdin); + len = strlen(instruction); + instruction[len-1] = '\0'; + p1 = strtok(instruction, " "); + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Source register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Destination register should be from Register A-P \n"); + return false; + } + printf("Dest = %s\t Src reg = %s\n",destreg,srcreg); + + if (strcasecmp(p1, "ADDQ")==0) { + reg[dest] = addq(reg[src],reg[dest]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else if (strcasecmp(p1, "CMPQ")==0) { + reg[dest] = cmpq(reg[src],reg[dest]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else + { + return false; + } +} +/**************************************ADDQ*******************************************************/ +int addq(int src, int dest){ + temp_reg[9] = src; + temp_reg[10] = dest; + temp_reg[11] = add(temp_reg[9],temp_reg[10]); + if(temp_reg[11] == 0) + { + printf("Zero Flag is set\n"); + flags[1] = 1; + } + if(temp_reg[11] < 0) + { + printf("Sign Flag is set\n"); + flags[2] = 1; + } + if((temp_reg[9]>0 && temp_reg[10]>0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]<0) && temp_reg[10]>=0) + { + printf("Overflow Flag is set\n"); + flags[3] = 1; + } + return temp_reg[11]; +} + +/**************************************CMPQ*******************************************************/ +int cmpq(int src, int dest){ + temp_reg[9] = src; + temp_reg[10] = dest; + temp_reg[11] = sub(temp_reg[9],temp_reg[10]); + if(temp_reg[9] == temp_reg[10]) + { + //printf("Zero Flag is set\n"); + flags[1] =1; + } + if(temp_reg[11] < 0) + { + //printf("Sign Flag is set\n"); + flags[2] = 1; + } + if((temp_reg[9]>0 && temp_reg[10]<0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]>0) && temp_reg[10]>0) + { + //printf("Overflow Flag is set"); + flags[3] = 1; + } + return temp_reg[11]; +} + +/********************************************************************************************* +Simple Function which calls the respective instruction. A function pointer is implemented +to point the functions like add, sub, mul, division and mod. +/********************************************************************************************/ + +bool alu_operations(){ + printf("1. Format for ADD operation: ADD dest_reg,src_reg1,src_reg2 Eg: ADD rd,r1,r2 \n"); + printf("2. Format for SUB operation: SUB dest_reg,src_reg1,src_reg2 Eg: SUB rd,r1,r2 \n"); + printf("3. Format for MUL operation: MUL dest_reg,src_reg1,src_reg2 Eg: MUL rd,r1,r2 \n"); + printf("4. Format for DIV operation: DIV dest_reg,src_reg1,src_reg2 Eg: DIV rd,r1,r2 \n"); + printf("5. Format for MOD operation: MOD dest_reg,src_reg1,src_reg2 Eg: MOD rd,r1,r2 \n"); + char instruction[20]; + int len; + char *p2 = NULL; + fgets(instruction,20,stdin); + len = strlen(instruction); + instruction[len-1] = '\0'; + p1 = strtok(instruction, " "); + p2 = strtok(NULL," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Destination register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + intreg = strtok(p2,","); + inti = intreg[0]-'A'; + if(inti > P){ + printf("Intermediate register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Source register should be from Register A-P \n"); + return false; + } + printf("Dest = %s\tIntr Reg = %s\tSrc reg = %s\n",destreg,intreg,srcreg); + /*****************************ADDITION**************************/ + if (strcasecmp(p1, "ADD")==0) { + reg[dest] = (*fun_ptr_add)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + /************************SUBTRACTION***************************/ + else if(strcasecmp(p1, "SUB")==0) + { + reg[dest] = (*fun_ptr_sub)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + /************************MULTIPLICATION***************************/ + else if(strcasecmp(p1, "MUL")==0) + { + reg[dest] = (*fun_ptr_mul)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + /************************DIVISION***************************/ + else if(strcasecmp(p1, "DIV")==0) + { + reg[dest] = (*fun_ptr_division)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + /************************MOD***************************/ + else if(strcasecmp(p1, "MOD")==0) + { + reg[dest] = (*fun_ptr_mod)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + pc = pc + 4; + print_values(); + return true; + } + else{ + return false; + } + return true; +} + +/*********************************************************************************************************** +Loops +For loop Increments till the maximum number is reached by the counter. +dowhile loop Increments if the source register is less than than the Comparing register or equal if it is +greater nothing is done. +whiledo loop increments if the source register is less than than the Comparing register if it is equal and +greater nothing is done. +*********************************************************************************************************/ +bool loops() +{ + printf("1. Format for For operation: to increment a number by one till the max is reached For Eg: for B,F\n"); + printf("2. Format for Do..While operation: it increments once without checking condition then it checks the condition to increment for Eg: dowhile B,F \n"); + printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,F\n"); + char instruction[20]; + int len; + char *p2 = NULL; + fgets(instruction,20,stdin); + len = strlen(instruction); + instruction[len-1] = '\0'; + p1 = strtok(instruction, " "); + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Source register should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Destination register should be from Register A-P \n"); + return false; + } + printf("Dest = %s\t Src destreg = %s\n",destreg,srcreg); + if (strcasecmp(p1, "for")==0) { + int count = 0; + printf("dest register %d \n",reg[dest]); + cmpq(count,reg[dest]); + pc = pc + 4; + if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + looping(reg[src],count,reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[1]) + { + printf("Cannot increment as the value Max is equal to the count\n"); + print_values(); + return true; + } + } + else if (strcasecmp(p1, "dowhile") == 0) + { + cmpq(reg[src],reg[dest]); + pc = pc + 4; + if(flags[1]) + { + reg[src] = add(reg[src],1); //incrementing + pc = pc + 4; + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + loopingwhile(reg[src],reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if (!flags[1]) + { + reg[src] = add(reg[src],1); //incrementing + pc = pc + 4; + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + } + else if (strcasecmp(p1,"whiledo") == 0) + { + int count = 0; + cmpq(reg[src],reg[dest]); + pc = pc + 4; + if(flags[2]) + { + jump(LOOP_ADDRESS); //jumps to particular location + loopingwhile(reg[src],reg[dest]); + printf("Result %d\n", reg[src]); + print_values(); + return true; + } + else if(flags[1]) + { + printf("Values are equal so cannot increment \n"); + print_values(); + return true; + } + else if (!flags[1]) + { + printf("Cannot increment as the src is greater than the given number so cannot increment\n"); + print_values(); + return true; + } + + } + else { + return false; + } +} +/********************************************Looping********************************************* +Increments till the count reaches the max. this is for "for loop" +************************************************************************************************/ +int looping(int dest,int count,int max) +{ + temp_reg[16] = dest; + temp_reg[11] = 1; + temp_reg[12] = count; + temp_reg[13] = max; + + temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + temp_reg[12] = add(temp_reg[12],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + cmpq(temp_reg[12],temp_reg[13]); + pc = pc + 4; + if(flags[2]) + { + pc = pc + 4; + jump(900); + looping(temp_reg[16],temp_reg[12],temp_reg[13]); + } + reg[src] = temp_reg[16]; + return 0; +} +/********************************************LoopingWhile******************************************** +Increments till the source equal to the destination. this is for "do while" and "while do" +************************************************************************************************/ +int loopingwhile(int dest,int max) +{ + temp_reg[16] = dest; + temp_reg[11] = 1; + temp_reg[13] = max; + + temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 + pc = pc + 4; + cmpq(temp_reg[16],temp_reg[13]); + pc = pc + 4; + if(flags[2]) //compare check sign + { + pc = pc + 4; + jump(900); + loopingwhile(temp_reg[16],temp_reg[13]); + } + reg[src] = temp_reg[16]; + return 0; +} + + + +/*****************************************ADDITION FUNCTION***************************************** +This function adds the content of inti and src and returns the value. The returned value is stored in +destination register. All operations in the add function are bitwise operations.Thus making it a 3 +operand instruction. Also, flags like sign flag, carry flag, overflow flag and zero flag is implemented. +/**************************************************************************************************/ + +int add(int inti, int src){ + int carry; + temp_reg[0] = src; + temp_reg[1] = inti; + while(temp_reg[0]!=0){ + carry = temp_reg[1] & temp_reg[0]; + temp_reg[1] = temp_reg[1] ^ temp_reg[0]; + temp_reg[0] = carry << 1; + } + + if((src>=0 && inti>=0 && temp_reg[1]<0) || (src<0 && inti<0 && temp_reg[1]>=0)){ //Overflow criteria + flags[3] = true; + } + else{ + flags[3] = false; + } + if(carry == 1){ //Carry Criteria + flags[0] = true; + } + else{ + flags[0] = false; + } + if(temp_reg[1]<0){ //Sign Flag criteria + flags[2] = true; + } + else{ + flags[2] = false; + } + if(temp_reg[1]==0){ //Zero Flag criteria + flags[1] = true; + } + else{ + flags[1] = false; + } + return temp_reg[1]; +} + + + +/*****************************************SUBTRACTION FUNCTION***************************************** +This substracts the content of inti and src and returns the value. The returned value is stored in +destination register. Thus making it a 3 operand instruction. Basically it performs 2's complement +of the negative number and calls the addition function. +/******************************************************************************************************/ +int sub(int inti, int src){ + //printf("Performing Subtraction \n"); + temp_reg[4] = src; + temp_reg[5] = inti; + temp_reg[4] = (*fun_ptr_add)(~temp_reg[4], 1); + temp_reg[4] = (*fun_ptr_add)(temp_reg[4], temp_reg[5]); + return temp_reg[4]; +} + + + +/*****************************************MULTIPLICATION FUNCTION***************************************** +This function multiplies the content of inti and src and returns the value. The returned value is stored in +destination register. Thus making it a 3 operand instruction. Primarily, with the help of logical left shift, +multiplication instruction is carried out. +/*********************************************************************************************************/ +int mul(int inti, int src){ + //printf("Performing Multiplication Operation\n"); + int result = 0; + temp_reg[2] = src; + temp_reg[3] = inti; + while (temp_reg[2] != 0) + { + if ((temp_reg[2] & 01) != 0) + { + result = (*fun_ptr_add)(result,temp_reg[3]); + } + temp_reg[3] <<= 1; + temp_reg[2] >>= 1; + } + return result; +} + +/*****************************************DIVISION FUNCTION***************************************** +This function divides the content of inti with src and returns the value. The returned value is stored in +destination register. Thus making it a 3 operand instruction. + +/***************************************************************************************************/ +int division(int inti, int src){ + //printf("Performing Division"); + temp_reg[5] = inti; + temp_reg[6] = src; + int result = 0 ,sign = 0; + if (temp_reg[5]< 0 ) + { + temp_reg[5]=-temp_reg[5]; + sign ^= 1; + } + if (temp_reg[6]< 0 ) + { + temp_reg[6]=-temp_reg[6]; + sign ^= 1; + } + if (temp_reg[6]!= 0 ) + { + // printf("dividing"); + while (temp_reg[5]>=temp_reg[6]) + { + temp_reg[5] = (*fun_ptr_sub)(temp_reg[5],temp_reg[6]); +// printf("temp_reg[5] afteR sub %d",temp_reg[5]); + result++; + } + } + + if (sign) + { + result=-result; + + } + if(result < 0){ //Sign Flag criteria + flags[2] = true; + } + else{ + flags[2] = false; + } + if(result == 0){ //Zero Flag criteria + flags[1] = true; + } + else{ + flags[1] = false; + } + // printf(" rem = %d \n",temp_reg[0]); + return result; +} + +/*****************************************MOD FUNCTION***************************************** +This function performs modulus operation on the content of inti with src and returns the value. +The returned value is stored in destination register. Thus making it a 3 operand instruction. + +/**********************************************************************************************/ +int mod(int inti, int src){ +temp_reg[7] = inti; +temp_reg[8] = src; +int c= 0 ,sign = 0; + if (temp_reg[7]< 0 ) + { + temp_reg[7]=-temp_reg[7]; + sign ^= 1; + } + if (temp_reg[8]< 0 ) + { + temp_reg[8]=-temp_reg[8]; + sign ^= 1; + } + if (temp_reg[8]!= 0 ) + { + + while (temp_reg[7]>=temp_reg[8]) + { + temp_reg[7] = (*fun_ptr_sub)(temp_reg[7],temp_reg[8]); + + c++; + } + } + + if (sign) + { + c=-c; + + } + return temp_reg[7]; + } + + +/*****************************************LOAD,STORE and LEA FUNCTION*****************************************/ +bool load_store_and_lea(){ + printf("1. Format for LOAD operation: LOAD dest_reg,offset(src_reg) Eg: LOAD A,30(RB,RI,4) \n"); + printf("2 .Format for STORE opcode: STORE dest_reg,offset(src_reg) Eg: STORE A,30(B) \n"); + printf("3. Format for LEA operation: LEA dest_reg,offset(src_reg) EG: LEA A,30(RB,RI,4) \n"); + char *p2 = NULL,*p3 = NULL,*p4 = NULL; + char instruction[20]; + int s,base,index; + fgets(instruction,20,stdin); + p1 = strtok(instruction, " "); + if (strcasecmp(p1, "LOAD")==0) { + opcode = LOAD_OPCODE; + p2 = strtok(NULL," "); + + destreg = strtok(p2,","); + + dest = destreg[0] - 'A'; /*Index for Destination register*/ + if(dest > P){ + printf("Destination registers should be from Register A-P \n"); + return false; + } + p2 = strtok(NULL,""); + + + p2 = strtok(p2,"("); + + if(p2==NULL){ + offset = 0; + } + else{ + offset = atoi(p2); + } + + + p3 = strtok(NULL,"("); + + if(p3 == NULL){ + if(p2==NULL){ + + } + else{ + p3=p2; + } + } + + p4 =strtok(p3,",)"); + + if(p4 == NULL){ + basereg = NULL; + } + else{ + basereg = p4; + base = basereg[0] - 'A'; /*Index for source register*/ + if(base > P){ + printf("Base registers should be from Register A-P \n"); + return false; + } + } + + p2 = strtok(NULL,""); + + indexreg = strtok(p2,",)"); + if(indexreg == NULL){ + + } + else{ + index = indexreg[0] - 'A'; /*Index for source register*/ + if(index > P){ + printf("Index registers should be from Register A-P \n"); + return false; + } + } + p2 = strtok(NULL,")"); + + if(p2==NULL){ + s = 0; + } + else{ + s = atoi(strtok(p2,")")); + } + printf("%d\n",s); + if(s==1 || s==2 || s==4 || s==8){ + } + else{ + printf("S should be 1,2,4 or 8\n"); + exit(0); + } + + part_address = reg[base]+s*reg[index]; + memory[INSTR_MEMORY_BASE_ADD + i] = opcode; + memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; + memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; + memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; + i = i+4; + result = execute_load(); + if (result !=0){ + printf("Error in executing Load Instruction \n"); + return false; + } + result = print_values(); + if (result !=0){ + printf("Error in printing values \n"); + return false; + } + } + else if (strcasecmp(p1, "LEA")==0) { + p2 = strtok(NULL,""); + + destreg = strtok(p2,","); + + dest = destreg[0] - 'A'; + + if(dest > P) { + printf("Destination registers should be from Register A-P \n"); + return false; + } + + p2 = strtok(NULL,""); + + p2 = strtok(p2,"("); + + if(p2==NULL){ + offset = 0; + } + else{ + offset = atoi(p2); + } + + p3 = strtok(NULL,"("); + + if(p3 == NULL){ + if(p2==NULL){ + + } + else{ + p3=p2; + } + } + + p4 =strtok(p3,",)"); + + if(p4 == NULL){ + basereg = NULL; + } + else{ + basereg = p4; + base = basereg[0] - 'A'; /*Index for source register*/ + if(base > P){ + printf("Base registers should be from Register A-P \n"); + return false; + } + } + + p2 = strtok(NULL,""); + + indexreg = strtok(p2,",)"); + + if(indexreg == NULL){ + + } + else{ + index = indexreg[0] - 'A'; /*Index for source register*/ + if(index > P){ + printf("Index registers should be from Register A-P \n"); + return false; + } + } + p2 = strtok(NULL,")"); + + if(p2==NULL){ + s = 0; + } + else{ + s = atoi(strtok(p2,")")); + } + if(s==0 || s==1 || s==2 || s==4 || s==8){ + } + else{ + printf("S should be 1,2,4 or 8\n"); + exit(0); + } + + part_address = reg[base]+s*reg[index]; + + memory[INSTR_MEMORY_BASE_ADD + i] = opcode; + memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; + memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; + memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; + i = i+4; + + result = execute_lea(); + if (result !=0){ + printf("Error in executing Lea Instruction \n"); + return false; + } + result = print_values(); + if (result !=0){ + printf("Error in printing values \n"); + return false; + } + + } + + else if (strcasecmp(p1, "STORE")==0) { + opcode = STORE_OPCODE; + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0] - 'A'; /*Index for Destination register*/ + if(src > P){ + printf("Destination registers should be from Register A-Q \n"); + return false; + } + p2 = strtok(NULL,""); + + + p2 = strtok(p2,"("); + + if(p2==NULL){ + offset = 0; + } + else{ + offset = atoi(p2); + } + + + p3 = strtok(NULL,"("); + + if(p3 == NULL){ + if(p2==NULL){ + + } + else{ + p3=p2; + } + } + + p4 =strtok(p3,",)"); + + if(p4 == NULL){ + basereg = NULL; + } + else{ + basereg = p4; + base = basereg[0] - 'A'; /*Index for source register*/ + if(base > P){ + printf("Base registers should be from Register A-P \n"); + return false; + } + } + + p2 = strtok(NULL,""); + + indexreg = strtok(p2,",)"); + + if(indexreg == NULL){ + + } + else{ + index = indexreg[0] - 'A'; /*Index for source register*/ + if(index > P){ + printf("Index registers should be from Register A-P \n"); + return false; + } + } + p2 = strtok(NULL,")"); + + if(p2==NULL){ + s = 0; + } + else{ + s = atoi(strtok(p2,")")); + } + if(s==1 || s==2 || s==4 || s==8){ + } + else{ + printf("S should be 1,2,4 or 8\n"); + exit(0); + } + + part_address = reg[base]+s*reg[index]; + memory[INSTR_MEMORY_BASE_ADD + i] = opcode; + memory[INSTR_MEMORY_BASE_ADD + i+1] = src; + memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; + memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; + i = i+4; + result = execute_store(); + if (result !=0){ + printf("Error in executing Store Instruction \n"); + return false; + } + result = print_values(); + if (result !=0){ + printf("Error in printing values \n"); + return false; + } + return true; + } + else { + printf("Enter valid instructions \n"); + return false; + } + return true; +} + +/****************Function for LOAD Instruction *********************************************/ +/* On executing LOAD destreg,offset(srcreg) instruction, the address or + value present in source register is added with offset which will result + into final address. The value present in final address is then loaded in + destination register.*/ +/*******************************************************************************************/ +int execute_load(){ + opcode = memory[pc]; + dest = memory[pc+1]; + offset = memory[pc+2]; + part_address = memory[pc+3]; + address = part_address + offset; + + if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ + reg[dest] = memory[address]; + pc = pc + 4; + printf("LOAD Instruction executed successfully\n\n\n"); + return 0; + } + else{ + printf("Invalid location\n"); + } +} + +/****************Function for LEA Instruction *********************************************/ +/* On executing LEA destreg,offset(srcreg) instruction, the address or + value present in source register is added with offset which will result + into final address. The final address is then loaded directly into the + destination register.*/ +/*******************************************************************************************/ +int execute_lea() { + + opcode = memory[pc]; + dest = memory[pc+1]; + offset = memory[pc+2]; + + part_address = memory[pc+3]; + address = part_address + offset; + + if(address >= INSTR_MEMORY_BASE_ADD && address < MEMORY_SIZE){ + reg[dest] = address; + pc = pc+4; + printf("LEA Instruction executed successfully\n\n\n"); + return 0; +} +else{ + printf("Invalid location\n"); + } + +} + + +/****************Function for STORE Instruction *********************************************/ +/* On executing STORE destreg,offset(srcreg) instruction, the address or + value present in source register is added with offset which will result + into final address. The value present in destination register is then + loaded in memory's final address.*/ +/*******************************************************************************************/ +int execute_store(){ + opcode = memory[pc]; + src = memory[pc+1]; + offset = memory[pc+2]; + part_address = memory[pc+3]; + address = part_address + offset; + //printf("part_address = %d\t full address = %d\n",part_address,address); + if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ + memory[address] = reg[src]; + pc = pc + 4; + printf("STORE Instruction executed successfully\n\n\n"); + return 0; + } + else{ + printf("Invalid location\n"); + } +} + +/******************************Main********************************/ +int main(){ + bool res = true; + int option; + char char_option[5]; + init_memory(); + reg[0] = 1024; /*Initialized Reg A = 1024 */ + reg[1] = 30; /*Initialized Reg B = 1024 */ + reg[2] =-103; + reg[3] =-69; + reg[4] = 2147483647; + reg[5] = 100; + result = print_values(); + while(pc < MEMORY_SIZE){ + while(res == true){ + printf("Enter instructions number from the menu\n"); + printf("****************Instructions Menu****************\n"); + printf("1. Load/Store/LEA Instruction\n"); + printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); + printf("3. Condition Codes - setne,sete,setle,setg,sets,setns\n"); + printf("4. Condition Codes - compq,addq\n"); + printf("5. Perform Recursive Binary Search with Stack operations\n"); + printf("6. Loops implementing Jump Instruction for, do..while, while..do\n"); + printf("7. EXIT\n"); + fgets(char_option,5,stdin); + sscanf(char_option,"%d",&option); + switch(option){ + case 1: + res = load_store_and_lea(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + case 2: + res = alu_operations(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + case 3: + res = cond_codes(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + case 4: + res = condcode_two(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + + case 5: + res = binary_search(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + exit(0); + case 6: + res = loops(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + break; + + case 7: + res = false; + exit(-1); + default: + break; + } + + } + } +} + From 4363499f9790b5d0d138c6e9f82674cdd93b1e37 Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Wed, 2 Nov 2016 23:09:13 -0700 Subject: [PATCH 6/6] Delete cpu_assignment_2.c --- cpu_assignment_2.c | 1418 -------------------------------------------- 1 file changed, 1418 deletions(-) delete mode 100644 cpu_assignment_2.c diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c deleted file mode 100644 index cfe22c1..0000000 --- a/cpu_assignment_2.c +++ /dev/null @@ -1,1418 +0,0 @@ -/* ****************************************************************************************************** - PROGRAM NAME: cpu_3.c - OBJECTIVE: Develop Best CPU. - DESCRIPTION: Setup CPU architecture with LOAD, STORE,LEA and ALU operations, Condition Codes, Jump, FLAGS - and Recursive Binary Search with Stack implementations. - TEAM MEMBERS: Sanket Dhami, Karthik Sadanand, Ramyashree, Neha Rege - DATE: 2nd Nov, 2016 - ****************************************************************************************************** */ - -#include -#include -#include -#include -#include - -#define MEMORY_SIZE 2048 -#define REG_COUNT 16 -#define TEMP_REG_COUNT 16 -#define DATA_MEMORY_BASE_ADD 1024 /* Base Address of Data Memory = 1024 */ -#define INSTR_MEMORY_BASE_ADD 256 /* Base Address of Instruction Memory = 256 */ -#define BOOT_MEMORY_BASE_ADD 0 /* Base Address of BIOS = 0 */ -#define location_recursive_binary_search 500 -#define LOOP_ADDRESS 900 /* Looping function */ -#define STORE_OPCODE 3 /* Opcode for STORE instruction */ -#define LOAD_OPCODE 4 /* Opcode for LOAD instruction */ -#define ADD_OPCODE 5 -#define SUB_OPCODE 6 -#define DIV_OPCODE 7 -#define MUL_OPCODE 8 -#define MOD_OPCODE 9 -#define CMPQ_OPCODE 10 -#define ADDQ_OPCODE 11 -#define SETE_OPCODE 12 -#define SETS_OPCODE 13 -#define SETNS_OPCODE 14 -#define SETL_OPCODE 15 -#define SETLE_OPCODE 16 -#define SETG_OPCODE 17 -#define JMP_OPCODE 18 -#define PUSH_OPCODE 19 -#define POP_OPCODE 20 - -/******add opcodes*****************************************************************************************************/ - - - int memory[MEMORY_SIZE]; /* Total Memory size = 2048 bytes */ - int reg[REG_COUNT]; /* Number of General Purpose Registers = 16 */ - int temp_reg[TEMP_REG_COUNT]; /* Number of Temporary Registers = 16 */ - enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P}; - int src=0, dest=0,inti=0; /* Index for source and destination registers */ - int opcode = 0; - int offset=0; - int i = 0; - - char *destreg = NULL, *srcreg = NULL,*basereg = NULL, *indexreg = NULL,*intreg = NULL; - bool flags[4]={ false }; /* flag register for setting various flags*/ -/*Carry Flag = flags[0] - * Zero Flag = flags[1] - * Sign Flag = flags[2] - * Overflow flag = flags[3] - * */ - -/* Program Counter */ - int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */ - -/* Stack Pointer */ - int sp = MEMORY_SIZE-1; - char *p1 = NULL; - - int address,part_address,result; - -/******Function Declarations*********/ -bool load_store_and_lea(); -bool alu_operations(); -bool cond_codes(); -bool jump_instructions(); -bool binary_search(); -void init_memory(); -int execute_load(); -int execute_store(); -int execute_lea(); -int print_values(); -int add(int inti, int src); -int sub(int inti, int src); -int mul(int inti, int src); -int division(int inti, int src); -int mod(int inti, int src); -int recursive_binary_search(int, int, int); -int rem =0; -int addq(int src,int dest); -int cmpq(int src,int dest); -int sete(); -int sets(); -int setns(); -int setl(); -int setle(); -int setg(); -void push(int); -int pop(); -int looping(int dest,int count,int max); -bool loops(); -int jump(int loc); -int loopingwhile(int dest,int max); - -/***************Function pointers*******/ - -int (*fun_ptr_add)(int, int) = &add; -int (*fun_ptr_sub)(int, int) = ⊂ -int (*fun_ptr_mul)(int, int) = &mul; -int (*fun_ptr_division)(int, int) = &division; -int (*fun_ptr_mod)(int, int) = &mod; - - -/************************Initialize Memory values to 0*******************/ -void init_memory( ) -{ - int i; - for(i=0;imemory[temp_reg[12]]){ - printf("Elements entered are not in ascending order!!\n"); - exit(0); - } - temp_reg[12] = temp_reg[12] + 1; - } - - temp_reg[12] = 1900; - printf("The Elements are \n"); - for(c=0;c P){ - printf("Destination register should be from Register A-P \n"); - return false; - } -/**********************************************************************************************/ - if (strcasecmp(p1, "SETE")==0) { - reg[dest] = sete(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETS")==0) { - reg[dest] = sets(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETNS")==0) { - reg[dest] = setns(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETL")==0) { - reg[dest] = setl(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETLE")==0) { - reg[dest] = setle(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "SETG")==0) { - reg[dest] = setg(); - //printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else{ - return false; - } -} - -/**************************SETE(Sets the destination register if Zero flag is set) ************/ -int sete(){ - if(flags[1] == true){ - - return 1; - } - else{ - return 0; - } -} - -/********************SETS(Sets the destination register if Sign flag is set)*****************/ -int sets(){ - printf("Sign Flag = %d\n",flags[2]); - if(flags[2] == true){ - return 1; - } - else{ - return 0; - } -} -/********************SETNS(Sets the destination register if signed flag is not set***********/ -int setns(){ - printf("Sign Flag = %d\n",flags[2]); - if(flags[2] == false){ - return 1; - } - else{ - return 0; - } -} -/****************SETL(Sets the destination register if (SF^OF))**************************/ -int setl(){ - if(flags[2]!=flags[3]){ - return 1; - } - else{ - return 0; - } -} -/**************SETG(Sets the destination register if ~(SF^OF)&~ZF)***********************/ -int setg(){ - if((!(flags[2]!=flags[3]))&&(!flags[1])){ - return 1; - } - else{ - return 0; - } -} -/****************SETLE(Sets the destination register if (SF^OF)|ZF)**************************/ -int setle(){ - if((flags[2]!=flags[3])||(flags[1])){ - return 1; - } - else{ - return 0; - } -} -/******************************************************************************************** -Conditional codes with two operands. -/********************************************************************************************/ -bool condcode_two(){ -printf("1. Format for ADDQ operation: ADDQ src,dest Eg: ADDQ A,B\n"); -printf("2. Format for CMPQ operation: CMPQ src,dest Eg: CMPQ A,B\n"); -char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\t Src reg = %s\n",destreg,srcreg); - - if (strcasecmp(p1, "ADDQ")==0) { - reg[dest] = addq(reg[src],reg[dest]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else if (strcasecmp(p1, "CMPQ")==0) { - reg[dest] = cmpq(reg[src],reg[dest]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else - { - return false; - } -} -/**************************************ADDQ*******************************************************/ -int addq(int src, int dest){ - temp_reg[9] = src; - temp_reg[10] = dest; - temp_reg[11] = add(temp_reg[9],temp_reg[10]); - if(temp_reg[11] == 0) - { - printf("Zero Flag is set\n"); - flags[1] = 1; - } - if(temp_reg[11] < 0) - { - printf("Sign Flag is set\n"); - flags[2] = 1; - } - if((temp_reg[9]>0 && temp_reg[10]>0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]<0) && temp_reg[10]>=0) - { - printf("Overflow Flag is set\n"); - flags[3] = 1; - } - return temp_reg[11]; -} - -/**************************************CMPQ*******************************************************/ -int cmpq(int src, int dest){ - temp_reg[9] = src; - temp_reg[10] = dest; - temp_reg[11] = sub(temp_reg[9],temp_reg[10]); - if(temp_reg[9] == temp_reg[10]) - { - //printf("Zero Flag is set\n"); - flags[1] =1; - } - if(temp_reg[11] < 0) - { - //printf("Sign Flag is set\n"); - flags[2] = 1; - } - if((temp_reg[9]>0 && temp_reg[10]<0 && temp_reg[11]<0)||(temp_reg[9]<0 && temp_reg[10]>0) && temp_reg[10]>0) - { - //printf("Overflow Flag is set"); - flags[3] = 1; - } - return temp_reg[11]; -} - -/********************************************************************************************* -Simple Function which calls the respective instruction. A function pointer is implemented -to point the functions like add, sub, mul, division and mod. -/********************************************************************************************/ - -bool alu_operations(){ - printf("1. Format for ADD operation: ADD dest_reg,src_reg1,src_reg2 Eg: ADD rd,r1,r2 \n"); - printf("2. Format for SUB operation: SUB dest_reg,src_reg1,src_reg2 Eg: SUB rd,r1,r2 \n"); - printf("3. Format for MUL operation: MUL dest_reg,src_reg1,src_reg2 Eg: MUL rd,r1,r2 \n"); - printf("4. Format for DIV operation: DIV dest_reg,src_reg1,src_reg2 Eg: DIV rd,r1,r2 \n"); - printf("5. Format for MOD operation: MOD dest_reg,src_reg1,src_reg2 Eg: MOD rd,r1,r2 \n"); - char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - intreg = strtok(p2,","); - inti = intreg[0]-'A'; - if(inti > P){ - printf("Intermediate register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\tIntr Reg = %s\tSrc reg = %s\n",destreg,intreg,srcreg); - /*****************************ADDITION**************************/ - if (strcasecmp(p1, "ADD")==0) { - reg[dest] = (*fun_ptr_add)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************SUBTRACTION***************************/ - else if(strcasecmp(p1, "SUB")==0) - { - reg[dest] = (*fun_ptr_sub)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************MULTIPLICATION***************************/ - else if(strcasecmp(p1, "MUL")==0) - { - reg[dest] = (*fun_ptr_mul)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************DIVISION***************************/ - else if(strcasecmp(p1, "DIV")==0) - { - reg[dest] = (*fun_ptr_division)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - /************************MOD***************************/ - else if(strcasecmp(p1, "MOD")==0) - { - reg[dest] = (*fun_ptr_mod)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - pc = pc + 4; - print_values(); - return true; - } - else{ - return false; - } - return true; -} - -/*********************************************************************************************************** -Loops -For loop Increments till the maximum number is reached by the counter. -dowhile loop Increments if the source register is less than than the Comparing register or equal if it is -greater nothing is done. -whiledo loop increments if the source register is less than than the Comparing register if it is equal and -greater nothing is done. -*********************************************************************************************************/ -bool loops() -{ - printf("1. Format for For operation: to increment a number by one till the max is reached For Eg: for B,F\n"); - printf("2. Format for Do..While operation: it increments once without checking condition then it checks the condition to increment for Eg: dowhile B,F \n"); - printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,F\n"); - char instruction[20]; - int len; - char *p2 = NULL; - fgets(instruction,20,stdin); - len = strlen(instruction); - instruction[len-1] = '\0'; - p1 = strtok(instruction, " "); - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Source register should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Destination register should be from Register A-P \n"); - return false; - } - printf("Dest = %s\t Src destreg = %s\n",destreg,srcreg); - if (strcasecmp(p1, "for")==0) { - int count = 0; - printf("dest register %d \n",reg[dest]); - cmpq(count,reg[dest]); - pc = pc + 4; - if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - looping(reg[src],count,reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[1]) - { - printf("Cannot increment as the value Max is equal to the count\n"); - print_values(); - return true; - } - } - else if (strcasecmp(p1, "dowhile") == 0) - { - cmpq(reg[src],reg[dest]); - pc = pc + 4; - if(flags[1]) - { - reg[src] = add(reg[src],1); //incrementing - pc = pc + 4; - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - loopingwhile(reg[src],reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if (!flags[1]) - { - reg[src] = add(reg[src],1); //incrementing - pc = pc + 4; - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - } - else if (strcasecmp(p1,"whiledo") == 0) - { - int count = 0; - cmpq(reg[src],reg[dest]); - pc = pc + 4; - if(flags[2]) - { - jump(LOOP_ADDRESS); //jumps to particular location - loopingwhile(reg[src],reg[dest]); - printf("Result %d\n", reg[src]); - print_values(); - return true; - } - else if(flags[1]) - { - printf("Values are equal so cannot increment \n"); - print_values(); - return true; - } - else if (!flags[1]) - { - printf("Cannot increment as the src is greater than the given number so cannot increment\n"); - print_values(); - return true; - } - - } - else { - return false; - } -} -/********************************************Looping********************************************* -Increments till the count reaches the max. this is for "for loop" -************************************************************************************************/ -int looping(int dest,int count,int max) -{ - temp_reg[16] = dest; - temp_reg[11] = 1; - temp_reg[12] = count; - temp_reg[13] = max; - - temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - temp_reg[12] = add(temp_reg[12],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - cmpq(temp_reg[12],temp_reg[13]); - pc = pc + 4; - if(flags[2]) - { - pc = pc + 4; - jump(900); - looping(temp_reg[16],temp_reg[12],temp_reg[13]); - } - reg[src] = temp_reg[16]; - return 0; -} -/********************************************LoopingWhile******************************************** -Increments till the source equal to the destination. this is for "do while" and "while do" -************************************************************************************************/ -int loopingwhile(int dest,int max) -{ - temp_reg[16] = dest; - temp_reg[11] = 1; - temp_reg[13] = max; - - temp_reg[16]= add(temp_reg[16],temp_reg[11]); // ADD R3,R1,R2 - pc = pc + 4; - cmpq(temp_reg[16],temp_reg[13]); - pc = pc + 4; - if(flags[2]) //compare check sign - { - pc = pc + 4; - jump(900); - loopingwhile(temp_reg[16],temp_reg[13]); - } - reg[src] = temp_reg[16]; - return 0; -} - - - -/*****************************************ADDITION FUNCTION***************************************** -This function adds the content of inti and src and returns the value. The returned value is stored in -destination register. All operations in the add function are bitwise operations.Thus making it a 3 -operand instruction. Also, flags like sign flag, carry flag, overflow flag and zero flag is implemented. -/**************************************************************************************************/ - -int add(int inti, int src){ - int carry; - temp_reg[0] = src; - temp_reg[1] = inti; - while(temp_reg[0]!=0){ - carry = temp_reg[1] & temp_reg[0]; - temp_reg[1] = temp_reg[1] ^ temp_reg[0]; - temp_reg[0] = carry << 1; - } - - if((src>=0 && inti>=0 && temp_reg[1]<0) || (src<0 && inti<0 && temp_reg[1]>=0)){ //Overflow criteria - flags[3] = true; - } - else{ - flags[3] = false; - } - if(carry == 1){ //Carry Criteria - flags[0] = true; - } - else{ - flags[0] = false; - } - if(temp_reg[1]<0){ //Sign Flag criteria - flags[2] = true; - } - else{ - flags[2] = false; - } - if(temp_reg[1]==0){ //Zero Flag criteria - flags[1] = true; - } - else{ - flags[1] = false; - } - return temp_reg[1]; -} - - - -/*****************************************SUBTRACTION FUNCTION***************************************** -This substracts the content of inti and src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. Basically it performs 2's complement -of the negative number and calls the addition function. -/******************************************************************************************************/ -int sub(int inti, int src){ - //printf("Performing Subtraction \n"); - temp_reg[4] = src; - temp_reg[5] = inti; - temp_reg[4] = (*fun_ptr_add)(~temp_reg[4], 1); - temp_reg[4] = (*fun_ptr_add)(temp_reg[4], temp_reg[5]); - return temp_reg[4]; -} - - - -/*****************************************MULTIPLICATION FUNCTION***************************************** -This function multiplies the content of inti and src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. Primarily, with the help of logical left shift, -multiplication instruction is carried out. -/*********************************************************************************************************/ -int mul(int inti, int src){ - //printf("Performing Multiplication Operation\n"); - int result = 0; - temp_reg[2] = src; - temp_reg[3] = inti; - while (temp_reg[2] != 0) - { - if ((temp_reg[2] & 01) != 0) - { - result = (*fun_ptr_add)(result,temp_reg[3]); - } - temp_reg[3] <<= 1; - temp_reg[2] >>= 1; - } - return result; -} - -/*****************************************DIVISION FUNCTION***************************************** -This function divides the content of inti with src and returns the value. The returned value is stored in -destination register. Thus making it a 3 operand instruction. - -/***************************************************************************************************/ -int division(int inti, int src){ - //printf("Performing Division"); - temp_reg[5] = inti; - temp_reg[6] = src; - int result = 0 ,sign = 0; - if (temp_reg[5]< 0 ) - { - temp_reg[5]=-temp_reg[5]; - sign ^= 1; - } - if (temp_reg[6]< 0 ) - { - temp_reg[6]=-temp_reg[6]; - sign ^= 1; - } - if (temp_reg[6]!= 0 ) - { - // printf("dividing"); - while (temp_reg[5]>=temp_reg[6]) - { - temp_reg[5] = (*fun_ptr_sub)(temp_reg[5],temp_reg[6]); -// printf("temp_reg[5] afteR sub %d",temp_reg[5]); - result++; - } - } - - if (sign) - { - result=-result; - - } - if(result < 0){ //Sign Flag criteria - flags[2] = true; - } - else{ - flags[2] = false; - } - if(result == 0){ //Zero Flag criteria - flags[1] = true; - } - else{ - flags[1] = false; - } - // printf(" rem = %d \n",temp_reg[0]); - return result; -} - -/*****************************************MOD FUNCTION***************************************** -This function performs modulus operation on the content of inti with src and returns the value. -The returned value is stored in destination register. Thus making it a 3 operand instruction. - -/**********************************************************************************************/ -int mod(int inti, int src){ -temp_reg[7] = inti; -temp_reg[8] = src; -int c= 0 ,sign = 0; - if (temp_reg[7]< 0 ) - { - temp_reg[7]=-temp_reg[7]; - sign ^= 1; - } - if (temp_reg[8]< 0 ) - { - temp_reg[8]=-temp_reg[8]; - sign ^= 1; - } - if (temp_reg[8]!= 0 ) - { - - while (temp_reg[7]>=temp_reg[8]) - { - temp_reg[7] = (*fun_ptr_sub)(temp_reg[7],temp_reg[8]); - - c++; - } - } - - if (sign) - { - c=-c; - - } - return temp_reg[7]; - } - - -/*****************************************LOAD,STORE and LEA FUNCTION*****************************************/ -bool load_store_and_lea(){ - printf("1. Format for LOAD operation: LOAD dest_reg,offset(src_reg) Eg: LOAD A,30(RB,RI,4) \n"); - printf("2 .Format for STORE opcode: STORE dest_reg,offset(src_reg) Eg: STORE A,30(B) \n"); - printf("3. Format for LEA operation: LEA dest_reg,offset(src_reg) EG: LEA A,30(RB,RI,4) \n"); - char *p2 = NULL,*p3 = NULL,*p4 = NULL; - char instruction[20]; - int s,base,index; - fgets(instruction,20,stdin); - p1 = strtok(instruction, " "); - if (strcasecmp(p1, "LOAD")==0) { - opcode = LOAD_OPCODE; - p2 = strtok(NULL," "); - - destreg = strtok(p2,","); - - dest = destreg[0] - 'A'; /*Index for Destination register*/ - if(dest > P){ - printf("Destination registers should be from Register A-P \n"); - return false; - } - p2 = strtok(NULL,""); - - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - printf("%d\n",s); - if(s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - result = execute_load(); - if (result !=0){ - printf("Error in executing Load Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - } - else if (strcasecmp(p1, "LEA")==0) { - p2 = strtok(NULL,""); - - destreg = strtok(p2,","); - - dest = destreg[0] - 'A'; - - if(dest > P) { - printf("Destination registers should be from Register A-P \n"); - return false; - } - - p2 = strtok(NULL,""); - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - if(s==0 || s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = dest; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - - result = execute_lea(); - if (result !=0){ - printf("Error in executing Lea Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - - } - - else if (strcasecmp(p1, "STORE")==0) { - opcode = STORE_OPCODE; - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0] - 'A'; /*Index for Destination register*/ - if(src > P){ - printf("Destination registers should be from Register A-Q \n"); - return false; - } - p2 = strtok(NULL,""); - - - p2 = strtok(p2,"("); - - if(p2==NULL){ - offset = 0; - } - else{ - offset = atoi(p2); - } - - - p3 = strtok(NULL,"("); - - if(p3 == NULL){ - if(p2==NULL){ - - } - else{ - p3=p2; - } - } - - p4 =strtok(p3,",)"); - - if(p4 == NULL){ - basereg = NULL; - } - else{ - basereg = p4; - base = basereg[0] - 'A'; /*Index for source register*/ - if(base > P){ - printf("Base registers should be from Register A-P \n"); - return false; - } - } - - p2 = strtok(NULL,""); - - indexreg = strtok(p2,",)"); - - if(indexreg == NULL){ - - } - else{ - index = indexreg[0] - 'A'; /*Index for source register*/ - if(index > P){ - printf("Index registers should be from Register A-P \n"); - return false; - } - } - p2 = strtok(NULL,")"); - - if(p2==NULL){ - s = 0; - } - else{ - s = atoi(strtok(p2,")")); - } - if(s==1 || s==2 || s==4 || s==8){ - } - else{ - printf("S should be 1,2,4 or 8\n"); - exit(0); - } - - part_address = reg[base]+s*reg[index]; - memory[INSTR_MEMORY_BASE_ADD + i] = opcode; - memory[INSTR_MEMORY_BASE_ADD + i+1] = src; - memory[INSTR_MEMORY_BASE_ADD + i+2] = offset; - memory[INSTR_MEMORY_BASE_ADD + i+3] = part_address; - i = i+4; - result = execute_store(); - if (result !=0){ - printf("Error in executing Store Instruction \n"); - return false; - } - result = print_values(); - if (result !=0){ - printf("Error in printing values \n"); - return false; - } - return true; - } - else { - printf("Enter valid instructions \n"); - return false; - } - return true; -} - -/****************Function for LOAD Instruction *********************************************/ -/* On executing LOAD destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The value present in final address is then loaded in - destination register.*/ -/*******************************************************************************************/ -int execute_load(){ - opcode = memory[pc]; - dest = memory[pc+1]; - offset = memory[pc+2]; - part_address = memory[pc+3]; - address = part_address + offset; - - if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - reg[dest] = memory[address]; - pc = pc + 4; - printf("LOAD Instruction executed successfully\n\n\n"); - return 0; - } - else{ - printf("Invalid location\n"); - } -} - -/****************Function for LEA Instruction *********************************************/ -/* On executing LEA destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The final address is then loaded directly into the - destination register.*/ -/*******************************************************************************************/ -int execute_lea() { - - opcode = memory[pc]; - dest = memory[pc+1]; - offset = memory[pc+2]; - - part_address = memory[pc+3]; - address = part_address + offset; - - if(address >= INSTR_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - reg[dest] = address; - pc = pc+4; - printf("LEA Instruction executed successfully\n\n\n"); - return 0; -} -else{ - printf("Invalid location\n"); - } - -} - - -/****************Function for STORE Instruction *********************************************/ -/* On executing STORE destreg,offset(srcreg) instruction, the address or - value present in source register is added with offset which will result - into final address. The value present in destination register is then - loaded in memory's final address.*/ -/*******************************************************************************************/ -int execute_store(){ - opcode = memory[pc]; - src = memory[pc+1]; - offset = memory[pc+2]; - part_address = memory[pc+3]; - address = part_address + offset; - //printf("part_address = %d\t full address = %d\n",part_address,address); - if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){ - memory[address] = reg[src]; - pc = pc + 4; - printf("STORE Instruction executed successfully\n\n\n"); - return 0; - } - else{ - printf("Invalid location\n"); - } -} - -/******************************Main********************************/ -int main(){ - bool res = true; - int option; - char char_option[5]; - init_memory(); - reg[0] = 1024; /*Initialized Reg A = 1024 */ - reg[1] = 30; /*Initialized Reg B = 1024 */ - reg[2] =-103; - reg[3] =-69; - reg[4] = 2147483647; - reg[5] = 100; - result = print_values(); - while(pc < MEMORY_SIZE){ - while(res == true){ - printf("Enter instructions number from the menu\n"); - printf("****************Instructions Menu****************\n"); - printf("1. Load/Store/LEA Instruction\n"); - printf("2. ALU operations - ADD/SUB/MUL/DIV/MOD\n"); - printf("3. Condition Codes - setne,sete,setle,setg,sets,setns\n"); - printf("4. Condition Codes - compq,addq\n"); - printf("5. Perform Recursive Binary Search with Stack operations\n"); - printf("6. Loops implementing Jump Instruction for, do..while, while..do\n"); - printf("7. EXIT\n"); - fgets(char_option,5,stdin); - sscanf(char_option,"%d",&option); - switch(option){ - case 1: - res = load_store_and_lea(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 2: - res = alu_operations(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 3: - res = cond_codes(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - case 4: - res = condcode_two(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - - case 5: - res = binary_search(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - exit(0); - case 6: - res = loops(); - if(res == false){ - printf("Wrong Instruction\n"); - exit(-1); - } - break; - - case 7: - res = false; - exit(-1); - default: - break; - } - - } - } -} -