From 939e313f865fadf896e23996c3ce99583514e2a1 Mon Sep 17 00:00:00 2001 From: karthiksadanand Date: Thu, 27 Oct 2016 03:43:38 -0700 Subject: [PATCH 1/5] jump and cmp statements --- cpu_assignment_2.c | 82 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 3143aca..b6a072c 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -52,6 +52,8 @@ int address,part_address,result; /******Function Declarations*********/ bool load_and_store(); bool alu_operations(); +bool unconditional(); +int cmp(); void init_memory(); int execute_load(); int execute_store(); @@ -73,7 +75,6 @@ int (*fun_ptr_division)(int, int) = &division; int (*fun_ptr_mod)(int, int) = &mod; - void init_memory( ) { int i; @@ -534,6 +535,75 @@ int c= 0 ,sign = 0; return temp_reg[7]; } +/*****************************************UNCONDTIONAL JUMP*****************************************/ +bool unconditional(){ + + printf("Enter the instruction: jmp\n"); + char instruction[20]; + fgets(instruction,20,stdin); + + //remove newline from the input + int len = strlen(instruction); + instruction[len-1]='\0'; + + p1 = strtok(instruction," "); + + printf("Value of Program Counter: %xH \n", pc); + //PC = PC + (int) temp_str; + pc = pc + 500; + printf("Value of Program Counter: %xH \n", pc); + + return true; +} + +int cmp(){ + printf("Enter the instruction: cmp 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," "); + destreg = strtok(p2,","); + dest = destreg[0]-'A'; + if(dest > P){ + printf("Register should be from Register A-P \n"); + return false; + } + + p2 = strtok(NULL," "); + srcreg = strtok(p2,","); + src = srcreg[0]-'A'; + if(src > P){ + printf("Register should be from Register A-P \n"); + return false; + } + printf("Register1 = %s\tRegister2 = %s\n",destreg,srcreg); + printf("Value of flag registers before execution of the instruction\n"); + printf("Value of program counter %xH\n", pc); + + //COMPARING BY SUBSTRACTION. bANKING HEAVILY ON THE FLAG UPDATION IN SUB. + reg[dest] = (*fun_ptr_sub)(reg[inti],reg[src]); + printf("Result %d\n", reg[dest]); + jump_decision(); + + print_values(); + return 0; +} + +int jump_decision(){ + + if(flags[1]==true){ + //Jump equal + } + else if(flags[1]==false){ + //Jump not equal + } + return 0; + +} /*****************************************LOAD AND STORE FUNCTION*****************************************/ bool load_and_store(){ @@ -808,7 +878,9 @@ int main(){ 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("4. Unconditional Jump\n"); + printf("5. Conditional Jump\n"); + printf("6. EXIT\n"); fgets(char_option,5,stdin); sscanf(char_option,"%d",&option); //option = option - '0'; @@ -836,6 +908,12 @@ int main(){ } break; case 4: + res = unconditional(); + break; + case 5: + res = cmp(); + break; + case 6: res = false; break; } From ad632232957235a472cd68fabacbbddcc4ac7e60 Mon Sep 17 00:00:00 2001 From: karthiksadanand Date: Tue, 1 Nov 2016 22:00:06 -0700 Subject: [PATCH 2/5] adding condi codes and recursive binary --- cpu_assignment_2.c | 391 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 300 insertions(+), 91 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index b6a072c..594d923 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -18,9 +18,27 @@ #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 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,15 +63,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 unconditional(); -int cmp(); +bool cond_codes(); +bool jump_instructions(); +bool binary_search(); void init_memory(); int execute_load(); int execute_store(); @@ -63,8 +82,18 @@ 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(); /***************Function pointers*******/ @@ -75,6 +104,7 @@ int (*fun_ptr_division)(int, int) = &division; int (*fun_ptr_mod)(int, int) = &mod; + void init_memory( ) { int i; @@ -128,6 +158,153 @@ int print_values(){ printf("\n\n\n"); return 0; } + /****************************************Jump Label*******************************************/ + int jump(int loc) + { + pc = loc; + return pc; + } + + +/******************************************************************************************** + + +**********************************************************************************************/ +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; + temp_reg[12] = 1900; + printf("Enter elements in ascending order. Press enter after each element!!\n"); + scanf("%d",&element); + memory[temp_reg[12]] = element; + temp_reg[12] = temp_reg[12] + 1; + for(c=1;cmemory[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=%d\n",temp_reg[14],temp_reg[13]); + pc = pc+4; + //printf("PC before IF= %d\n",pc); + if(flags[1] == 1 || flags[2] == 1){ + int temp_reg[16]; + temp_reg[16] = sub(temp_reg[14],temp_reg[13]); + //printf("temp_reg[16] after sub = %d\n",temp_reg[16]); + pc = pc+4; + //printf("PC = %d\n",pc); + temp_reg[16] = division(temp_reg[16],2); + //printf("temp_reg[16] after division = %d\n",temp_reg[16]); + pc = pc+4; + //printf("PC = %d\n",pc); + temp_reg[16] = add(temp_reg[13],temp_reg[16]); + //printf("temp_reg[16] after add = %d\n",temp_reg[16]); + pc = pc+4; + //printf("PC = %d\n",pc); + cmpq(memory[temp_reg[16]],temp_reg[15]); + pc = pc+4; + //printf("PC after cmpq= %d\n",pc); + if (flags[1]==1){ + // printf("%d==%d\n",memory[temp_reg[16]],temp_reg[15]); + return temp_reg[16]; + } + else if(flags[2]==1){ + temp_reg[13] = add(temp_reg[16],1); + pc = pc+4; + // printf("%d<%d\n",memory[temp_reg[16]],temp_reg[15]); + printf("SP before push= %d\n",sp); + push(pc); + jump(location_recursive_binary_search); + pc = pop(); + printf("SP after pop= %d\n",sp); + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); + + } + else{ + // printf("%d>%d\n",memory[temp_reg[16]],temp_reg[15]); + temp_reg[14] = sub(temp_reg[16],1); + pc = pc +4; + push(pc); + jump(location_recursive_binary_search); + printf("SP before push= %d\n",sp); + pc = pop(); + printf("SP after pop= %d\n",sp); + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); + + } + + } + return -1; +} + + + /******************************************************************************************** @@ -138,7 +315,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; @@ -267,6 +444,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. @@ -365,10 +636,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; @@ -413,7 +680,7 @@ 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[4] = (*fun_ptr_add)(~temp_reg[4], 1); @@ -429,7 +696,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; @@ -467,11 +734,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++; } } @@ -493,7 +760,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; } @@ -535,75 +802,6 @@ int c= 0 ,sign = 0; return temp_reg[7]; } -/*****************************************UNCONDTIONAL JUMP*****************************************/ -bool unconditional(){ - - printf("Enter the instruction: jmp\n"); - char instruction[20]; - fgets(instruction,20,stdin); - - //remove newline from the input - int len = strlen(instruction); - instruction[len-1]='\0'; - - p1 = strtok(instruction," "); - - printf("Value of Program Counter: %xH \n", pc); - //PC = PC + (int) temp_str; - pc = pc + 500; - printf("Value of Program Counter: %xH \n", pc); - - return true; -} - -int cmp(){ - printf("Enter the instruction: cmp 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," "); - destreg = strtok(p2,","); - dest = destreg[0]-'A'; - if(dest > P){ - printf("Register should be from Register A-P \n"); - return false; - } - - p2 = strtok(NULL," "); - srcreg = strtok(p2,","); - src = srcreg[0]-'A'; - if(src > P){ - printf("Register should be from Register A-P \n"); - return false; - } - printf("Register1 = %s\tRegister2 = %s\n",destreg,srcreg); - printf("Value of flag registers before execution of the instruction\n"); - printf("Value of program counter %xH\n", pc); - - //COMPARING BY SUBSTRACTION. bANKING HEAVILY ON THE FLAG UPDATION IN SUB. - reg[dest] = (*fun_ptr_sub)(reg[inti],reg[src]); - printf("Result %d\n", reg[dest]); - jump_decision(); - - print_values(); - return 0; -} - -int jump_decision(){ - - if(flags[1]==true){ - //Jump equal - } - else if(flags[1]==false){ - //Jump not equal - } - return 0; - -} /*****************************************LOAD AND STORE FUNCTION*****************************************/ bool load_and_store(){ @@ -877,9 +1075,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. Unconditional Jump\n"); - printf("5. Conditional Jump\n"); + printf("3. Condition Codes - setne,sete,setle,setg,sets,setns,compq,addq\n"); + printf("4. Condition Codes - compq,addq\n"); + printf("5. Perform Recursive Binary Search\n"); printf("6. EXIT\n"); fgets(char_option,5,stdin); sscanf(char_option,"%d",&option); @@ -899,7 +1097,7 @@ int main(){ printf("Wrong Instruction\n"); exit(-1); } - break; + break; case 3: res = cond_codes(); if(res == false){ @@ -908,12 +1106,22 @@ int main(){ } break; case 4: - res = unconditional(); + res = condcode_two(); + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } break; + case 5: - res = cmp(); - break; - case 6: + res = binary_search(); + + if(res == false){ + printf("Wrong Instruction\n"); + exit(-1); + } + + case 7: res = false; break; } @@ -921,3 +1129,4 @@ int main(){ } } } + From fdd1981de9ad08808cda43811d5fd4de7ac5909a Mon Sep 17 00:00:00 2001 From: karthiksadanand Date: Tue, 1 Nov 2016 22:31:33 -0700 Subject: [PATCH 3/5] segmentation fault fix plus register usage in binary search --- cpu_assignment_2.c | 53 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 594d923..2546dfe 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -205,23 +205,23 @@ bool binary_search(){ printf("Enter the element to be searched\n"); scanf("%d",&key); - push(pc); + push(pc); //PUSH PC //pc = 500; //RECURSIVE BINARY SEARCH FUNCTION IS AT 500th LOCATION //temp_reg[12] = temp_reg[12] - 1; - jump(location_recursive_binary_search); - temp_reg[16] = recursive_binary_search(1900,temp_reg[12],key); - temp_reg[16] = temp_reg[16] -1900; + jump(location_recursive_binary_search); //JUMP 500 + temp_reg[16] = recursive_binary_search(1900,temp_reg[12],key); //CALL + temp_reg[16] = sub(temp_reg[16],1900); if(temp_reg[16]<0){ printf("****ELEMENT NOT FOUND****\n"); } else{ - reg[7] = add(temp_reg[16],1); + reg[7] = add(temp_reg[16],1); //ADD R7,TR16,1 pc = pc +4; printf("ELEMENT FOUND AT POSITION %d\n",reg[7]); } printf("\n\n"); //print_values(); - pc = pop(); + pc = pop(); //POP PC print_values(); return true; @@ -229,11 +229,11 @@ bool binary_search(){ void push(pc){ memory[sp] = pc+4; - sp = sub(sp,1); - printf("stack in push %d\n", sp); + sp = sub(sp,1); //SUB SP,SP,1 + printf("stack in push %d\n", sp); } int pop(){ - sp = add(sp,1); + sp = add(sp,1); //ADD SP,SP,1 printf("stack in pop %d\n", sp); return memory[sp]; @@ -249,25 +249,24 @@ int recursive_binary_search(int start,int end,int key){ temp_reg[14] = end; temp_reg[15] = key; //printf("Start PC = %d\n",pc); - cmpq(temp_reg[13],temp_reg[14]); + cmpq(temp_reg[13],temp_reg[14]); //CMPQ TR13,TR14 //printf("%d>=%d\n",temp_reg[14],temp_reg[13]); pc = pc+4; //printf("PC before IF= %d\n",pc); - if(flags[1] == 1 || flags[2] == 1){ - int temp_reg[16]; - temp_reg[16] = sub(temp_reg[14],temp_reg[13]); + if(flags[1] == 1 || flags[2] == 1){ + temp_reg[16] = sub(temp_reg[14],temp_reg[13]); //SUB TR16,TR14,TR13 //printf("temp_reg[16] after sub = %d\n",temp_reg[16]); pc = pc+4; //printf("PC = %d\n",pc); - temp_reg[16] = division(temp_reg[16],2); + temp_reg[16] = division(temp_reg[16],2); //DIV TR16,TR16,2 //printf("temp_reg[16] after division = %d\n",temp_reg[16]); pc = pc+4; //printf("PC = %d\n",pc); - temp_reg[16] = add(temp_reg[13],temp_reg[16]); + temp_reg[16] = add(temp_reg[13],temp_reg[16]); //ADD TR16,TR13,TR16 //printf("temp_reg[16] after add = %d\n",temp_reg[16]); pc = pc+4; //printf("PC = %d\n",pc); - cmpq(memory[temp_reg[16]],temp_reg[15]); + 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){ @@ -275,27 +274,27 @@ int recursive_binary_search(int start,int end,int key){ return temp_reg[16]; } else if(flags[2]==1){ - temp_reg[13] = add(temp_reg[16],1); + temp_reg[13] = add(temp_reg[16],1); //ADD TR16,1 pc = pc+4; // printf("%d<%d\n",memory[temp_reg[16]],temp_reg[15]); printf("SP before push= %d\n",sp); - push(pc); - jump(location_recursive_binary_search); - pc = pop(); + push(pc); //PUSH + jump(location_recursive_binary_search); //JUMP 500 + pc = pop(); //POP printf("SP after pop= %d\n",sp); - return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL } else{ // printf("%d>%d\n",memory[temp_reg[16]],temp_reg[15]); - temp_reg[14] = sub(temp_reg[16],1); + temp_reg[14] = sub(temp_reg[16],1); //SUB TR14,TR16,1 pc = pc +4; - push(pc); - jump(location_recursive_binary_search); + push(pc); //PUSH + jump(location_recursive_binary_search); //JUMP 500 printf("SP before push= %d\n",sp); - pc = pop(); + pc = pop(); //POP printf("SP after pop= %d\n",sp); - return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL } @@ -682,7 +681,7 @@ 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; //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]; From e60e05cf05970ff45f730ed2eabc0fcfacb0b892 Mon Sep 17 00:00:00 2001 From: karthiksadanand Date: Wed, 2 Nov 2016 01:45:47 -0700 Subject: [PATCH 4/5] integrating LEA, loops in code --- cpu_assignment_2.c | 366 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 339 insertions(+), 27 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 2546dfe..07971a4 100644 --- a/cpu_assignment_2.c +++ b/cpu_assignment_2.c @@ -19,7 +19,7 @@ #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 @@ -68,7 +68,7 @@ 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(); @@ -76,6 +76,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); @@ -94,6 +95,10 @@ 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*******/ @@ -167,8 +172,9 @@ int print_values(){ /******************************************************************************************** - - +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(){ printf("Enter number of elements for binary search\n"); @@ -205,23 +211,23 @@ bool binary_search(){ printf("Enter the element to be searched\n"); scanf("%d",&key); - push(pc); //PUSH PC + push(pc); //PUSH PC //pc = 500; //RECURSIVE BINARY SEARCH FUNCTION IS AT 500th LOCATION //temp_reg[12] = temp_reg[12] - 1; - jump(location_recursive_binary_search); //JUMP 500 - temp_reg[16] = recursive_binary_search(1900,temp_reg[12],key); //CALL + jump(location_recursive_binary_search); //JUMP 500 + temp_reg[16] = recursive_binary_search(1900,temp_reg[12],key); //CALL temp_reg[16] = sub(temp_reg[16],1900); if(temp_reg[16]<0){ printf("****ELEMENT NOT FOUND****\n"); } else{ - reg[7] = add(temp_reg[16],1); //ADD R7,TR16,1 - pc = pc +4; + reg[7] = add(temp_reg[16],1); //ADD R7,TR16,1 + pc = pc +4; printf("ELEMENT FOUND AT POSITION %d\n",reg[7]); - } + } printf("\n\n"); //print_values(); - pc = pop(); //POP PC + pc = pop(); //POP PC print_values(); return true; @@ -241,8 +247,12 @@ int pop(){ /******************************************************************************************** - - +Here we have implemented Recursive Binary Search along with Stack functions. +This function searches an element from a given set of elements stored in memory using binary +search and recursively calls the function till it finds an element. If found, it displays the +index of the element present in array of element. If not found then, it will display element not +found. +Stack operations are also performed like PUSH and POP functions. **********************************************************************************************/ int recursive_binary_search(int start,int end,int key){ temp_reg[13] = start; @@ -254,6 +264,7 @@ int recursive_binary_search(int start,int end,int key){ pc = pc+4; //printf("PC before IF= %d\n",pc); if(flags[1] == 1 || flags[2] == 1){ + //int temp_reg[16]; temp_reg[16] = sub(temp_reg[14],temp_reg[13]); //SUB TR16,TR14,TR13 //printf("temp_reg[16] after sub = %d\n",temp_reg[16]); pc = pc+4; @@ -271,10 +282,10 @@ int recursive_binary_search(int start,int end,int key){ //printf("PC after cmpq= %d\n",pc); if (flags[1]==1){ // printf("%d==%d\n",memory[temp_reg[16]],temp_reg[15]); - return temp_reg[16]; + return temp_reg[16]; //RET } else if(flags[2]==1){ - temp_reg[13] = add(temp_reg[16],1); //ADD TR16,1 + temp_reg[13] = add(temp_reg[16],1); //ADD TR13,TR16,1 pc = pc+4; // printf("%d<%d\n",memory[temp_reg[16]],temp_reg[15]); printf("SP before push= %d\n",sp); @@ -282,7 +293,7 @@ int recursive_binary_search(int start,int end,int key){ jump(location_recursive_binary_search); //JUMP 500 pc = pop(); //POP printf("SP after pop= %d\n",sp); - return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET } else{ @@ -294,7 +305,7 @@ int recursive_binary_search(int start,int end,int key){ printf("SP before push= %d\n",sp); pc = pop(); //POP printf("SP after pop= %d\n",sp); - return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL + return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET } @@ -305,8 +316,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"); @@ -627,6 +638,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,number\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,number \n"); + printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,number\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; + printf("max value %d",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; + printf("max value %d",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 @@ -802,10 +977,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; @@ -906,6 +1082,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," "); @@ -1031,6 +1305,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 @@ -1072,19 +1374,20 @@ 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,compq\n"); printf("4. Condition Codes - compq,addq\n"); - printf("5. Perform Recursive Binary Search\n"); - printf("6. EXIT\n"); + printf("5. Perform Recursive Binary Search with Stack operations\n"); + printf("6. Loops, for, do..while, while..do implemented using jump instructions\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); @@ -1114,14 +1417,23 @@ int main(){ 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 5a4491ac5779fc7355e9e68c00a20af1909befac Mon Sep 17 00:00:00 2001 From: Sanketdhami Date: Wed, 2 Nov 2016 23:11:26 -0700 Subject: [PATCH 5/5] Assignment 3 --- cpu_assignment_2.c | 106 +++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 66 deletions(-) diff --git a/cpu_assignment_2.c b/cpu_assignment_2.c index 07971a4..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 @@ -45,14 +46,14 @@ 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] @@ -63,9 +64,10 @@ 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_store_and_lea(); @@ -109,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; @@ -177,6 +179,7 @@ order and the element to be searched. Later it calls recursive_binary_search fun 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; @@ -206,19 +209,16 @@ bool binary_search(){ temp_reg[12] = temp_reg[12] + 1; } printf("\n"); - //printf("%d\n",temp_reg[12]); - printf("Enter the element to be searched\n"); scanf("%d",&key); push(pc); //PUSH PC - //pc = 500; //RECURSIVE BINARY SEARCH FUNCTION IS AT 500th LOCATION - //temp_reg[12] = temp_reg[12] - 1; jump(location_recursive_binary_search); //JUMP 500 temp_reg[16] = recursive_binary_search(1900,temp_reg[12],key); //CALL temp_reg[16] = sub(temp_reg[16],1900); if(temp_reg[16]<0){ printf("****ELEMENT NOT FOUND****\n"); + reg[7] = -1; } else{ reg[7] = add(temp_reg[16],1); //ADD R7,TR16,1 @@ -226,7 +226,6 @@ bool binary_search(){ printf("ELEMENT FOUND AT POSITION %d\n",reg[7]); } printf("\n\n"); - //print_values(); pc = pop(); //POP PC print_values(); return true; @@ -236,11 +235,11 @@ bool binary_search(){ void push(pc){ memory[sp] = pc+4; sp = sub(sp,1); //SUB SP,SP,1 - printf("stack in push %d\n", sp); + //printf("stack in push %d\n", sp); } int pop(){ sp = add(sp,1); //ADD SP,SP,1 - printf("stack in pop %d\n", sp); + //printf("stack in pop %d\n", sp); return memory[sp]; } @@ -258,53 +257,39 @@ int recursive_binary_search(int start,int end,int key){ temp_reg[13] = start; temp_reg[14] = end; temp_reg[15] = key; - //printf("Start PC = %d\n",pc); + cmpq(temp_reg[13],temp_reg[14]); //CMPQ TR13,TR14 - //printf("%d>=%d\n",temp_reg[14],temp_reg[13]); + pc = pc+4; - //printf("PC before IF= %d\n",pc); + if(flags[1] == 1 || flags[2] == 1){ - //int temp_reg[16]; + temp_reg[16] = sub(temp_reg[14],temp_reg[13]); //SUB TR16,TR14,TR13 - //printf("temp_reg[16] after sub = %d\n",temp_reg[16]); pc = pc+4; - //printf("PC = %d\n",pc); temp_reg[16] = division(temp_reg[16],2); //DIV TR16,TR16,2 - //printf("temp_reg[16] after division = %d\n",temp_reg[16]); pc = pc+4; - //printf("PC = %d\n",pc); temp_reg[16] = add(temp_reg[13],temp_reg[16]); //ADD TR16,TR13,TR16 - //printf("temp_reg[16] after add = %d\n",temp_reg[16]); pc = pc+4; - //printf("PC = %d\n",pc); 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[temp_reg[16]],temp_reg[15]); return temp_reg[16]; //RET } else if(flags[2]==1){ temp_reg[13] = add(temp_reg[16],1); //ADD TR13,TR16,1 pc = pc+4; - // printf("%d<%d\n",memory[temp_reg[16]],temp_reg[15]); - printf("SP before push= %d\n",sp); push(pc); //PUSH jump(location_recursive_binary_search); //JUMP 500 pc = pop(); //POP - printf("SP after pop= %d\n",sp); return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET } else{ - // printf("%d>%d\n",memory[temp_reg[16]],temp_reg[15]); 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 - printf("SP before push= %d\n",sp); pc = pop(); //POP - printf("SP after pop= %d\n",sp); return recursive_binary_search(temp_reg[13],temp_reg[14],temp_reg[15]); //CALL & RET } @@ -333,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'; @@ -341,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(); @@ -385,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){ @@ -408,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){ @@ -417,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){ @@ -427,7 +404,7 @@ int setns(){ return 0; } } - +/****************SETL(Sets the destination register if (SF^OF))**************************/ int setl(){ if(flags[2]!=flags[3]){ return 1; @@ -436,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; @@ -445,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; @@ -482,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]); @@ -648,9 +625,9 @@ 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,number\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,number \n"); - printf("3. Format for While..do operation: it increments after the condition is true for Eg: whiledo B,number\n"); + 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; @@ -762,7 +739,7 @@ int looping(int dest,int count,int max) temp_reg[11] = 1; temp_reg[12] = count; temp_reg[13] = max; - printf("max value %d",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 @@ -786,7 +763,7 @@ int loopingwhile(int dest,int max) temp_reg[16] = dest; temp_reg[11] = 1; temp_reg[13] = max; - printf("max value %d",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]); @@ -1094,9 +1071,9 @@ bool load_store_and_lea(){ return false; } - p2 = strtok(NULL,""); + p2 = strtok(NULL,""); - p2 = strtok(p2,"("); + p2 = strtok(p2,"("); if(p2==NULL){ offset = 0; @@ -1293,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; @@ -1345,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; @@ -1357,7 +1334,7 @@ int execute_store(){ } } - +/******************************Main********************************/ int main(){ bool res = true; int option; @@ -1376,15 +1353,13 @@ int main(){ 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,compq\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, for, do..while, while..do implemented using jump instructions\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_store_and_lea(); @@ -1440,4 +1415,3 @@ int main(){ } } } -