Code runs perfectly. I can enter the parameters and quit the program but when I try to calculate the number of cycles and print out the instructions it crashes. I looked over the nested for-loops and changed the condition (whether it should be < or <= ) and it still crashes. All I know is that the problem is within the for-loop and I can't figure out what it is.

# include<stdio.h>
# include<stdlib.h>

char instr_string[8];

struct node {
	int dest;
	int src1;
	int src2;
	int delay;
} *set = NULL;
typedef struct node instr;

/* Option 1: Enter instrutions */
void enter_params() {
	/* Declare local vars */
	int n;
	int i;
	
	/* Prompt number of instruction: n */
	printf("Enter the total number of instructions: ");
	scanf("%d", &n);
	printf("\n");
	
	set = (instr*) malloc ((n + 1) *sizeof (instr)); 
	
	/* Prompt for each instruction */
	/* For-loop: i = 1 to n */
	for(i = 1; i <= n; i++) {
		printf("%d) ", i);
		scanf("%s", &instr_string);
		set[i].dest = instr_string[1] - '0';
		set[i].src1 = instr_string[4] - '0';
		set[i].src2 = instr_string[7] - '0';
	}
	set[0].dest = -1;
} /* End option 1 */

/* Option 2: Pipelined */
void calc_pipe() {
	/* Declare local vars */
	int i;
	int n;
	int k;
	int dep = 0;
	int delay = 0;	
	int prev_delay = 0;
	int total_delay = 0;
	
	/* Check for dependencies */
	/* For-loop: i = 2 to n */
	for(i = 2; i <= n; i++) {
		/* Initialize local vars */
		delay = 0;
		total_delay = 0;
		
		/* For-loop: k = i - 2 to i - 1 */
		for(k = i - 2; k <= i - 1; k++) {
			/* Initialize local vars */
			dep = 0;
			prev_delay = 0;
			
			if((set[k].dest == set[i].src1) || (set[k].dest == set[i].src2)) { /* RAW dependency */				
				dep = 1;
				
				if(i - k == 2) {
					if(prev_delay == 1) {
						delay = 0;
						prev_delay = 0;
					}
					else{
						delay = 1;
						prev_delay = 1;
					}
				}
				else{
					delay = 2;
					prev_delay = 1;
				}
			}
			else { /* No RAW dependency */
				if(dep == 0)
					prev_delay = 0;
				else
					dep = 0;
			}
		} /* End of k-loop */
		set[i].delay = delay + 1 + set[i - 1].delay;
		total_delay += delay + 1;
	} /* End of i-loop */
	
	/* Print number of delays */
	printf("Total number of cycles: %d", &total_delay);
	printf("\n");
	
	/* Print instructions */
	for(i = 1; i <= n; i++) {
		for(k = 1; k < set[i].delay; k++) {
			printf("\t");
		}
		printf("FI\tDI\tFO\tEI\tWO\n");
	}
}

/* Option 3 */
void calc_super() {
	/* Declare local vars */
	int i;
	int n;
	int k;
	int dep = 0;
	int delay = 0;	
	int prev_delay = 0;
	int total_delay = 0;

	
	/* Check for dependencies */
	/* For-loop: i = 2 to n */
	for(i = 2; i <= n; i++) {
		/* Initialize local vars */
		delay = 0;
		total_delay = 0;
		
		/* For-loop: k = i - 2 to i - 1 */
		for(k = i - 5; k <= i - 1; k++) {
			/* Initialize local vars */
			dep = 0;
			prev_delay = 0;
			
			if(k >= 0) { /* RAW dependency */	
				dep = 1;
				
				if(i - k > 1) {
					if(prev_delay == 1) {
						delay = 0;
						prev_delay = 0;
					}
					else{
						delay = 1;
						prev_delay = 1;
					}
				}
				else{
					delay = 2;
					prev_delay = 1;
				}
			}
			else{ /* No RAW dependency */
				if(dep == 0)
					prev_delay = 0;
				else
					dep = 0;
			}
		} /* End of k-loop */
		set[i].delay = delay + 1 + set[i - 1].delay;
		total_delay += delay + 1;
	} /* End of i-loop */
	
	/* Print number of delays */
	printf("Total number of cycles: %d", &total_delay);
	printf("\n");
	
	/* Print instructions */
	for(i = 1; i <= n; i++) {
		for(k = 1; k <= set[i].delay; k++) {
			printf("\t");
		}
		printf("FI\tDI\tFO\tEI\tWO\n");
	}
}

/* Option 4 */
void quit_prog() {
	if(set != NULL) {
		free(set);
	}
}

void main() {
	int selection;
	
	while(selection != 4) {
		printf("PIPELINED INSTRUCTION PERFORMANCE:\n");
		printf("1) Enter instructions\n");
		printf("2) Calculate total cycle count on a 6-stage pipelined architecture\n");
		printf("3) Calculate total cycle count on a 6-stage superscalar architecture\n");
		printf("4) Quit program\n\n");
		
		printf("Enter selection: ");
		scanf("%d", &selection);
		printf("\n");
		
		switch(selection) {
			case 1: enter_params();
				break;
									
			case 2: calc_pipe();
				break;
				
			case 3: calc_super();
				break;
		
			case 4: quit_prog();
				break;
						
			default: printf("\nInvalid selection!\n\n");
		}
	}
}

Recommended Answers

All 5 Replies

I guess there's something wrong with my jGrasp or something because I tried running on the school's jGrasp and it runs without crashing but the total number of delay expected was way off by 10 digits and the print statement isn't working. The problem is definitely within the nested for-loop.

In line 31 .... The way you are using the scanf is wrong

char arr[10] ={};
scanf("%s",arr);                 // This is the correct way of using scanf

FYI scanf should be avoided as much as possible as it has many gotcha's. Its better to shift to fgets

scanf("%s", &instr_string);

What's wrong with it?

Okay ignore that, I understand why. It'll make some giant number.

So I managed figure out why it kept crashing... int n; is the number of instructions I input. I set it as a local variable but it suppose to be a global variable because it's suppose to apply to the entire code.

It doesn't crash anymore BUT it goes blank... I'm using jGrasp and run the program on I/O in MSDOS Windows

# include<stdio.h>
# include<stdlib.h>

char instr_string[8];
int n;

struct node {
   int dest;
   int src1;
   int src2;
   int delay;
} *set = NULL;
typedef struct node instr;

/* Option 1: Enter instrutions */
void enter_params() {
   /* Declare local vars */
   int i;
  
   /* Prompt number of instruction: n */
   printf("Enter the total number of instructions: ");
   scanf("%d", &n);
   printf("\n");
  
   set = (instr*) malloc ((n + 1) *sizeof (instr));
  
   /* Prompt for each instruction */
   /* For-loop: i = 1 to n */
   for(i = 1; i <= n; i++) {
      printf("%d) ", i);
      scanf("%s", &instr_string);
      set[i].dest = instr_string[1] - '0';
      set[i].src1 = instr_string[4] - '0';
      set[i].src2 = instr_string[7] - '0';
   }
	printf("\n");
	
   set[0].dest = -1;
} /* End option 1 */

/* Option 2: Pipelined */
void calc_pipe() {
   /* Declare local vars */
   int i;
   int k;
   int dep = 0;
   int delay = 0;
   int prev_delay = 0;
   int total_delay = 0;
  
   /* Check for dependencies */
   /* For-loop: i = 2 to n */
   for(i = 2; i <= n; i++) {
      /* Initialize local vars */
      delay = 0;
      total_delay = 0;
     
      /* For-loop: k = i - 2 to i - 1 */
      for(k = i - 2; k <= i - 1; k++) {
         /* Initialize local vars */
         dep = 0;
         prev_delay = 0;
        
         if((set[k].dest == set[i].src1) || (set[k].dest == set[i].src2)) { /* RAW dependency */           
            dep = 1;
           
           if(i - k == 2) {
               if(prev_delay == 1) {
                  delay = 0;
                  prev_delay = 0;
               }
               else{
                  delay = 1;
                  prev_delay = 1;
               }
            }
            else{
               delay = 2;
               prev_delay = 1;
            }
         }
         else { /* No RAW dependency */
            if(dep == 0)
               prev_delay = 0;
            else
               dep = 0;
         }
      } /* End of k-loop */
      set[i].delay = delay + 1 + set[i - 1].delay;
      total_delay += delay + 1;
   } /* End of i-loop */
  
   /* Print number of delays */
   printf("Total number of cycles: %d", total_delay);
   printf("\n");
   printf("\n");
  
   /* Print instructions */
   for(i = 1; i <= n; i++) {
      for(k = 1; k < set[i].delay; k++) {
         printf("\t");
      }
      printf("FI\tDI\tFO\tEI\tWO\n");
   }
}

/* Option 3 */
void calc_super() {
   /* Declare local vars */
   int i;
   int k;
   int dep = 0;
   int delay = 0;
   int prev_delay = 0;
   int total_delay = 0;

  
   /* Check for dependencies */
   /* For-loop: i = 2 to n */
   for(i = 2; i <= n; i++) {
      /* Initialize local vars */
      delay = 0;
      total_delay = 0;
     
      /* For-loop: k = i - 2 to i - 1 */
      for(k = i - 5; k <= i - 1; k++) {
         /* Initialize local vars */
         dep = 0;
         prev_delay = 0;
        
         if(k >= 0) { /* RAW dependency */  
            dep = 1;
           
            if(i - k > 1) {
               if(prev_delay == 1) {
                  delay = 0;
                  prev_delay = 0;
               }
               else{
                  delay = 1;
                  prev_delay = 1;
               }
            }
            else{
               delay = 2;
               prev_delay = 1;
            }
         }
         else{ /* No RAW dependency */
            if(dep == 0)
               prev_delay = 0;
            else
               dep = 0;
         }
      } /* End of k-loop */
      set[i].delay = delay + 1 + set[i - 1].delay;
      total_delay += delay + 1;
   } /* End of i-loop */
  
   /* Print number of delays */
   printf("Total number of cycles: %d", total_delay);
   printf("\n");
   printf("\n");
  
   /* Print instructions */
   for(i = 1; i <= n; i++) {
      for(k = 1; k <= set[i].delay; k++) {
         printf("\t");
      }
      printf("FI\tDI\tFO\tEI\tWO\n");
   }
}

/* Option 4 */
void quit_prog() {
   if(set != NULL) {
      free(set);
  }
}

int main() {
   int selection;
 
   while(selection != 4) {
      printf("PIPELINED INSTRUCTION PERFORMANCE:\n");
      printf("1) Enter instructions\n");
      printf("2) Calculate total cycle count on a 6-stage pipelined architecture\n");
      printf("3) Calculate total cycle count on a 6-stage superscalar architecture\n");
      printf("4) Quit program\n\n");
     
      printf("Enter selection: ");
      scanf("%d", &selection);
      printf("\n");
     
      switch(selection) {
         case 1: enter_params();
            break;
                          
         case 2: calc_pipe();
            break;
           
         case 3: calc_super();
            break;
     
         case 4: quit_prog();
            break;
                 
         default: printf("\nInvalid selection!\n\n");
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.