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 8533735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:02:17+00:00 2026-06-11T10:02:17+00:00

I’m attempting to go through a simple text file with assembly instruction in it

  • 0

I’m attempting to go through a simple text file with assembly instruction in it and it looks like this

TOP   NOP
VAL   INT 0
TAN   LA 2,1

This is just a small example so I can show you how it works. Basically I’m taking the first labels and placing them in label, then the second which are NOP, INT, and LA and placing them in opcode.

After that I’m taking the first argument (0 and 2) and placing them in arg1. However here is where my problem comes in. With the current code I have, the output I get when I place the arguments into the string is as such

TOP
0
2

Obviously I’d like to only get the last two to be the only ones but how do I make it so that TOP doesn’t get thrown in there with my first arguments?

#include <string>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])
{
// If no extra file is provided then exit the program with error message
if (argc <= 1)
{
    cout << "Correct Usage: " << argv[0] << " <Filename>" << endl;
    exit (1);
}

// Array to hold the registers and initialize them all to zero
int registers [] = {0,0,0,0,0,0,0,0};

string memory [16000];

string Symtablelab[1000];
int Symtablepos[1000];

string line;
string label;
string opcode;
string arg1;
string arg2;

// Open the file that was input on the command line
ifstream myFile;
myFile.open(argv[1]);

if (!myFile.is_open())
{
    cerr << "Cannot open the file." << endl;
}

int counter = 0;
int i = 0;
int j = 0;

while (getline(myFile, line, '\n'))
{
    if (line[0] == '#')
    {
        continue;
    }

    if (line.length() == 0)
    {
        continue;
    }

    if (line[0] != '\t' && line[0] != ' ')
    {
        string delimeters = "\t ";

        int current;
        int next = -1;

        current = next + 1;
        next = line.find_first_of( delimeters, current);
        label = line.substr( current, next - current );

        Symtablelab[i] = label;

        current = next + 1;
        next = line.find_first_of(delimeters, current);
        opcode = line.substr(current, next - current);

        if (opcode != "WORDS" && opcode != "INT")
        {
            counter += 3;
        }

        if (opcode == "INT")
        {
            counter++;
        }

        delimeters = ", \n\t";
        current = next + 1;
        next = line.find_first_of(delimeters, current);
        arg1 = line.substr(current, next-current);

        cout << arg1<<endl;

        i++;
    }
}
  • 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-11T10:02:18+00:00Added an answer on June 11, 2026 at 10:02 am

    The problem is looking for the start of each subsequent word: current = next + 1. You want to look for the first non-delimiter to be the start of the word and check if you’re at the end of the line before looking for arguments.

    Adding debug information, I see the following:

    >> label: start=0 end=3 value="TOP"
    >> opcode: start=4 end=4 value=""
    
    >> label: start=0 end=3 value="VAL"
    >> opcode: start=4 end=4 value=""
    
    >> label: start=0 end=3 value="TAN"
    >> opcode: start=4 end=4 value=""
    

    Which tells me each attempt at opcode is finding another delimiter.

    The problem is that you only increment one after the word and the next line.substr() catches the delimiter.

    In the lookups after the start, change:

    current = next + 1;
    

    to:

    current = line.find_first_not_of(delimeters, next + 1);
    

    This allows it to look for the start the next word after any and all delimiters.

    Also, you want to make the lookup of arguments conditional on line length remaining, so wrap it in if(next >0) { ... }.

    This gives me, with my debugging and your original output (made conditional):

    >> label: start=0 end=3 value="TOP"
    >> opcode: start=6 end=-1 value="NOP"
    >> label: start=0 end=3 value="VAL"
    >> opcode: start=6 end=9 value="INT"
    >> arg1: start=10 end=-1 value="0"
    0
    >> label: start=0 end=3 value="TAN"
    >> opcode: start=6 end=8 value="LA"
    >> arg1: start=9 end=10 value="2"
    2
    

    Re-factor your parsing/tokenizing from the main loop so you can focus on them. You might even want to get cppunit (or similar) to help you test your parsing function. In the absence of such, it helps you go to one place and insert debugging information like:

    cout << ">> " << whatIsBeingDebugged << ": " << start=" << current 
         << " end=" << next << " value= \"" << value << "\"" << endl;
    

    Making a robust lexical analyzer and parser is the subject of many libraries (lex and yacc, flex and bison, etc.), can be an application of others such as regular expressions and is even entire college courses. It is work. But, just be methodical, thorough, and test pieces in isolation such as with unit testing with cppunit (or similar).

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

Sidebar

Related Questions

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 have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.