Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:51:24+00:00 2026-06-15T00:51:24+00:00

I have a tournament branch predictor simulation as an assignment. This program simulates the

  • 0

I have a tournament branch predictor simulation as an assignment. This program simulates the tournament branch predictor in the microprocessors. I guess I did everything right. I cannot trace the problem. I am stuck in segmentation fault.

/* 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:51:26+00:00Added an answer on June 15, 2026 at 12:51 am

    You have seq_in as

    char * seq_in;
    

    To be used later in a fgets call. Your variable must have its memory allocated properly or you program will raise a segmentation fault. You can solve this issue:

    char seq_in[200];  /* or the size you want */
    

    Or

    char * seq_in = malloc (200, sizeof(char));  /* or the size you want */
    

    Same issue happens with your fout variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this structure models class Tournament < ActiveRecord::Base AGES = [5u, 6u, 7u,
I have an application that runs through the rounds in a tournament, and I
have written this little class, which generates a UUID every time an object of
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I'm making a tournament manager in CakePHP 1.3 and I have a tournament controller
I have an applications that stores players ratings for each tournament. So I have
I have the following set of code for a Sub program. It's inserting a
I have an array of 16 (example below). Each match of the tournament involves
I have this piece of code and it's causing NullPointerException private List<Player> playerList; private
I really hope someone can help on this because I'm learning cocoa and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.