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");
}
}
}