我将锦标赛分支预测模拟作为一项任务。该程序模拟微处理器中的锦标赛分支预测器。我想我做的一切都是正确的。我无法追查问题。我陷入了分段错误。
/* Shreyas Kale Computer Architecture Homework 3 Problem 1 */
/*
* for global index, at first considered not taken, gi[0] --> MSB
* 0 --> not taken
* 1 --> taken
*
*/
#include<stdio.h>
#include<unistd.h>
#include<strings.h>
struct tables{
char msb;
char lsb;
}local[10],global[64],selector[10];
//6 bits = 64
struct global_index{
unsigned int i : 6; //bitfield, to index global predictor
}gi = {0};
int pc = 0, correct = 0;
//here I mean instruction address
char localdec, globaldec, selected;
void initialise()
{
int i,res;
for(i=0;i<10;i++)
{
local[i].lsb = 'n';
selector[i].lsb = 'n';
local[i].msb = 'n';
selector[i].msb = 'n';
}
for(i=0;i<64;i++)
{
global[i].lsb = 'n';
global[i].msb = 'n';
}
}
void main()
{
char *sbs, *sbo, *myfile;
FILE *fd_sbs, *fd_sbo, *fd_myfile;
char *seq_in, *fout; // for input and output to file
int g = 0;
int k = 0, res;
sbs = "sample_branch_sequence.txt"; //sample branch sequence filename
sbo = "sample_branch_output.txt"; //sample branch output filename
myfile = "myout.txt"; // my output filename
initialise();
fd_sbs = fopen(sbs,"r");
fd_sbo = fopen(sbo,"r");
fd_myfile = fopen(myfile,"a+");
/* Check whether files are opened */
if((fd_sbs == NULL) || (fd_sbo == NULL) || (fd_myfile == NULL))
{
printf("\n files are not opened properly, bye bye!");
return;
}
/*Actual algorithm*/
for(k=0;k<10000;k++)
{
fgets(seq_in,4,fd_sbs);
//printf("%s\n",seq_in);
pc = atoi(seq_in[0]);
//now branch
//look up local
if(local[pc].msb == 'n')
localdec = 'n';
else if(local[pc].msb == 't')
localdec = 't';
//lookup global
g = (int)gi.i;
if(global[g].msb == 'n')
globaldec = 'n';
else if(global[g].msb == 't')
globaldec = 't';
//look up selector
if(selector[pc].msb == 'n')
selected = 'g';
else if(selector[pc].msb == 't')
selected = 'l';
// final decision and program direction updation of output string
fout[0] = seq_in[0];
fout[1] = localdec;
fout[2] = globaldec;
fout[3] = selected;
fout[4] = ((selected == 'g') ? globaldec : localdec);
fout[5] = seq_in[1];
fout[6] = '\n';
fout[7] = '\0';
//put it in file
//calculate correct predictions
if(fout[4] == seq_in[1])
correct += 1;
res = fputs(fout,fd_myfile);
if(res == EOF){
printf("Something erroneous!!");
}
//update global counter
if(seq_in[1] == 't')
{
gi.i = gi.i << 1; //pust a 0 from left
gi.i += 1; //add 1 now so, lsb becomes 1
}
else if(seq_in[1] == 'n')
{
gi.i = gi.i <<1; //push a 0 from left
}
//NOW UPDATION OF THE PREDICTORS
if(fout[4] == seq_in[1])
{
//decision correct update selector accordingly
if(selected == 'g')
{
// decrement slector
if((selector[pc].lsb == 't')&&(selector[pc].msb == 't'))
selector[pc].lsb = 'n';
else if((selector[pc].lsb == 'n')&&(selector[pc].msb == 't'))
{
selector[pc].msb = 'n';
selector[pc].lsb = 't';
}
else if((selector[pc].lsb == 't')&&(selector[pc].msb == 'n'))
selector[pc].lsb = 'n';
else if((selector[pc].msb == 'n')&&(selector[pc].lsb == 'n')) //cant be deremented
return;
}
else if(selected == 'l')
{
//increment selector
if((selector[pc].lsb == 'n')&&(selector[pc].msb == 'n'))
selector[pc].lsb = 't';
else if((selector[pc].lsb == 't')&&(selector[pc].msb == 'n'))
{
selector[pc].msb = 't';
selector[pc].lsb = 'n';
}
else if((selector[pc].lsb == 'n')&&(selector[pc].msb == 't'))
selector[pc].lsb = 't';
else if((selector[pc].msb == 't')&&(selector[pc].lsb == 't')) //cant increment
return;
}
//now update other tables : local first increment
if((local[pc].lsb == 'n')&&(local[pc].msb == 'n'))
local[pc].lsb = 't';
else if((local[pc].lsb == 't')&&(local[pc].msb == 'n'))
{
local[pc].msb = 't';
local[pc].lsb = 'n';
}
else if((local[pc].lsb == 'n')&&(local[pc].msb == 't'))
local[pc].lsb = 't';
else if((local[pc].msb == 't')&&(local[pc].lsb == 't')) //cant increment
return;
//now update global : increment
if((global[gi.i].lsb == 'n')&&(global[gi.i].msb == 'n'))
global[gi.i].lsb = 't';
else if((global[gi.i].lsb == 't')&&(global[gi.i].msb == 'n'))
{
global[gi.i].msb = 't';
global[gi.i].lsb = 'n';
}
else if((global[gi.i].lsb == 'n')&&(global[gi.i].msb == 't'))
global[gi.i].lsb = 't';
else if((global[gi.i].msb == 't')&&(global[gi.i].lsb == 't')) //cant increment
return;
}
else if(fout[4] != seq_in[1]) //wrong decision
{
//decrement local and global
if((local[pc].lsb == 't')&&(local[pc].msb == 'n'))
local[pc].lsb = 'n';
else if((local[pc].lsb == 'n')&&(local[pc].msb == 't'))
{
local[pc].msb = 'n';
local[pc].lsb = 't';
}
else if((local[pc].lsb == 't')&&(local[pc].msb == 'n'))
local[pc].lsb = 'n';
else if((local[pc].msb == 'n')&&(local[pc].lsb == 'n')) //cant decremet
return;
//now update global : decrement
if((global[gi.i].lsb == 't')&&(global[gi.i].msb == 'n'))
global[gi.i].lsb = 'n';
else if((global[gi.i].lsb == 'n')&&(global[gi.i].msb == 't'))
{
global[gi.i].msb = 'n';
global[gi.i].lsb = 't';
}
else if((global[gi.i].lsb == 't')&&(global[gi.i].msb == 'n'))
global[gi.i].lsb = 'n';
else if((global[gi.i].msb == 'n')&&(global[gi.i].lsb == 'n')) //cant decrement
return;
}
}
}
</code></pre>
答案 0 :(得分:3)
你有seq_in as
char * seq_in;
稍后在fgets通话中使用。您的变量必须正确分配其内存,否则您的程序将引发分段错误。您可以解决此问题:
char seq_in[200]; /* or the size you want */
或者
char * seq_in = malloc (200, sizeof(char)); /* or the size you want */
你的fout变量会出现同样的问题。