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

  • SEARCH
  • Home
  • 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 7668853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:27:45+00:00 2026-05-31T15:27:45+00:00

I’m trying to read lines from a SIC/XE program using a C program that

  • 0

I’m trying to read lines from a SIC/XE program using a C program that I wrote (it’s supposed to act like pass one of the SICASM assembler). However, the program will “hang” (won’t finish executing) if the last line of SIC/XE code doesn’t have an operand after the mneumonic. I would just like to get rid of the hanging, the assembly will follow from there.

Here is the piece of my program that gets the input:

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

#define SIZE 51
#define LABEL_SIZE 9
#define MNEUMONIC_SIZE 8
#define OPERAND_SIZE 10

void getData(FILE* fpIn);
int hashFunc(char label[]);
unsigned long hex2int(char *a, unsigned int len);
bool checkMneumonic(char* mneumonic, int* bytes);

int main(int argc, char* argv[])
{
   FILE* fpIn = fopen(argv[1], "r");   
   if(fpIn != NULL)
      printf("\nFile open.");
   else
   {
      printf("\nError: Main needs an input file.\n");
      exit(EXIT_FAILURE);
   }

   getData(fpIn);

   fclose(fpIn);
   printf("\nFileClosed");

   printf("\n\n");
   return 0;
}

void getData(FILE* fpIn)
{
//debug printf("\nIn \"getData\" ");

   char label[LABEL_SIZE];
   char mneumonic[MNEUMONIC_SIZE];
   char operand[OPERAND_SIZE];
   int startAddr, currentAddr, PC = 0;
   bool gotValidMneu = true;
   int bytes;

   //open file for writing
   FILE* fpOut = fopen("output", "w");
   if(fpOut != NULL)
      printf("\n Ready to write.");

   fprintf(fpOut,"\n\tHash\tLabel\tAddress");

   char string1[10];
   char string2[10];
   int size;
   while( fscanf(fpIn, "%s", string1) != EOF)
   {         
      gotValidMneu = true;

      if(strcmp(string1, "LTORG") == 0)
         strcpy(string2, " ");
      else if(strcmp(string1, "RSUB") == 0)
         strcpy(string2, " ");
      else if(!feof(fpIn) && fgetc(fpIn) != '\n')
         fscanf(fpIn,"%s", string2);

      if(checkMneumonic(string1, &size) && !feof(fpIn))
      {
         strcpy(mneumonic, string1);
         strcpy(operand, string2);
         strcpy(label, " ");
      }
      else if(checkMneumonic(string2, &size) && !feof(fpIn))
      {
         strcpy(label, string1);
         strcpy(mneumonic, string2);
         fscanf(fpIn, "%s[\n]", operand);
      }
      else if( string1[0] == '.')
      {
         char junk[80];
         fgets(junk, 80, fpIn);
         gotValidMneu = false;
      }
      else
      {
         printf("\nError: got invalid mneumonic");
         gotValidMneu = false;
      }

      if(gotValidMneu)
      {
         //adjust the current address
         currentAddr = PC;

         //get the starting address
         if(strcmp(mneumonic, "START") == 0)
         {
            startAddr = hex2int(operand, strlen(operand) );
            PC = startAddr;
         }

         //mneumonic is "BASE"
         else if(strcmp(mneumonic, "BASE") == 0);

         //mneumonic is "RESW"
         else if(strcmp(mneumonic, "RESW") == 0)
         {
            PC += (3 * atoi(operand) );
         }

         //mneumonic is "BYTE"
         else if(strcmp(mneumonic, "BYTE") == 0 )
         {
            PC += atoi(operand);
         }

         //mneumonic is "END"
         else if(strcmp(mneumonic, "END") == 0);

         //check if the mneumonic is valid
         else if(checkMneumonic(mneumonic, &bytes) )
         {
            PC += bytes;
         }

         //got a bad mneumonic
         else
         {
            gotValidMneu = false;
            printf("\nError: \"%s\" is not a valid mneumonic.", mneumonic);
            fprintf(fpOut,"\nError: \"%s\" is not a valid mneumonic.", 
                    mneumonic);
         }

         if(gotValidMneu)
         {
            if(strcmp(label, " ") != 0 )
               fprintf(fpOut,"\n\t%d\t%s\t%x", hashFunc(label), label, 
                       currentAddr);
            printf("\n%x\t%s\t%s\t%s", currentAddr, label, mneumonic, operand);
         }

      }

      if(gotValidMneu)
      //flush the comments, if any
      while( '\n' != getc(fpIn) )
         getc(fpIn);

   } //end while
   fprintf(fpOut,"\n");
   fclose(fpOut);
   printf("\n Symbol table sent to file: \"output\".");
}

int hashFunc(char label[])
{
   int i, sum, hashVal;
   sum = 0;

   for(i = 0; i < strlen(label); i++)
      sum += label[i];

   hashVal = sum % SIZE;
//   printf("\nHashed Index: %d", hashVal);

   return hashVal;
}

unsigned long hex2int(char *a, unsigned int len)
{
    int i;
    unsigned long val = 0;

    for(i=0;i<len;i++)
       if(a[i] <= 57)
        val += (a[i]-48)*(1<<(4*(len-1-i)));
       else
        val += (a[i]-55)*(1<<(4*(len-1-i)));
    return val;
}

bool checkMneumonic(char mneumonic[], int* bytes)
{
   bool valid = false;
   char validMneumonic[MNEUMONIC_SIZE];

   FILE* fpIn = fopen("sicOps", "r");

   while( (fscanf(fpIn, "%s %d", validMneumonic, bytes) != EOF) )
   {
      if(strcmp(mneumonic, validMneumonic) == 0 )
      {
         valid = true;
         break;
      }
   }

   fclose(fpIn);
   return valid;
}

And here is the file that I’m using to test it:

ADDRES    START    100
. tests pc forward addressing
. tests base addressing
. tests pc backward addressing
NOW      +LDB     #BEGIN              load base register
XXX       BASE     BEGIN              tell assembler
YYY       LDA      BE                 A <- (m..m+2)
EN        RESW     4
BE        WORD     2
BEGIN     RESW     1
JUNK      RESW     2200
THERE     RESW     1
ZZZ       LDA      JUNK
MMM       STA      THERE
          END 

EDIT: There is a lot of new code. I trimmed down the original program, compiled and tested it and it hangs.

  • 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-05-31T15:27:47+00:00Added an answer on May 31, 2026 at 3:27 pm

    I don’t see how the code that you posted could hang, either by waiting for input from a file or by looping, so it seems that the actual problem is elsewhere in your program. Perhaps in a combination with the posted snippet.

    But I see that you use feof, which is almost always wrong in a C program, since it will return true only after you have hit the end of the file. Use the return values from the various input calls instead.

    EDIT:

    I actually tried your code, by adding the minimal code needed to make it a complete, compilable program, and tried it on your example input. It didn’t hang. You should always try to post a minimal, working program that still exhibits the problem. Now you removed parts that seem to have been relevant.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker

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.