1 Attachment(s)
building an assembler in C++
hello
i'm trying to build an assembler for an SIC machine.
I wrote all my functions including pass1 and pass2 .
the user is supposed to write in a file the source code then the program can convert it into object code.
now my problem is that there r no errors but the program is not working and i do not see where the problem is..can u please help?
the program is written in c++ using visual c++.
i will attach the program so u can see my code
plz answer me as soon a possible
thank u in advance
1 Attachment(s)
confused,tired an depressed in need of urgent help
hi
as i said last time i'm trying to build an assembler for an sic machine using VC++.
I'm having problems running my program and everytime i refine my program other problems pop out i don't know from where!!
now i know that u will realize that this is a homework and may be think that i'm asking u to to do it for me..so this is no the case AT ALL. it's just that i've been working on it for a long time and i can't get it to run corrcetly which making me really depressed:(
i will attach the new file and if u can plz take a look at it and if u didn't know what i mean by some of the code let me know and i will asnswer u right back..
there is an error that i didn't know how to handle..i tried to check the braces to see if i missed one but all seems fine..
thank you
Re: confused,tired an depressed in need of urgent help
Quote:
Originally posted by Lolita
it's just that i've been working on it for a long time and i can't get it to run corrcetly which making me really depressed:(
Get stubborn instead of depressed. Be stubborn about making it work.
If you want someone to help you, though, and if it is necessary to execute the program, then some data to execute it with would help.
program is running BUT...
ok now my program is compiling and is running and it produces an output but this output is not as expected.
what i do is this :
ofstream output;
output.open("myfile.dat");
output<<"H"<<array2[0].label;
output.close();
this is supposed to open a file and write to my file H then whatever is found in array2[0].label. Something like H001000. Now the problem is i find grabage characters before and after the the line i'm writing which i have absolutely no idea where the came from.
do u know what the problem is .
i found this way of writing and reading from files in a book so i don't know why the file is in .dat extension. i tried other ways of using files and this is the only one that worked with me.
i used it before and it run correctly without any grabage characters but i don't know why it it not working now..
any ideas?
thnx
Re: program is running BUT...
Quote:
Originally posted by Lolita
ok now my program is compiling and is running and it produces an output but this output is not as expected.
what i do is this :
ofstream output;
output.open("myfile.dat");
output<<"H"<<array2[0].label;
output.close();
May I suggest changing your inter_file structure to use std::string? You're coding in C++, there is little reason not to use what C++ provides (why don't they teach this to C++ students??)
Code:
#include <string>
struct inter_file
{
std::string locctr;//store the locctr during pass 1
std::string label;
std::string opcode;
std::string operand;
};
Then you don't need to worry about allocating, deallocating, memory overwrites, and the error you are currently getting now. As a matter of fact, you're including <string> already -- why not use it? Also, why are you stuffing two or more statements on a line? This code not only makes it harder to read, but also harder to debug. Stick to one statement per line.
You are doing too many "new"'s and absolutely no "delete's" in your program. This is a memory leak. I'll take an example of some of your functions:
Code:
struct operation_code // operation code table
{
std::string op;//operation code name
std::string x;//operation code
};
//---------------------
operation_code optab[26];
//-------------------------
void insert_optab()// insert into the operation table (standard)
{
optab[0]. op = "ADD";
optab[0].x = "18";
// etc.
}
//--------------------------
struct symbol_table //symbol table structure
{
std::string label;
std::string add;//in hexa;
};
//...
// In your main() program
for(; ;)
{
// There are no need for these lines if using std::string
/*
line[i-1].label=new char; // What??
line[i-1].opcode=new char; // Where are the "deletes"?
line[i-1].operand=new char; // Same here.
*/
// Here is another usage of std::string in your main() function
// Look at the difference.
input>>line[i-1].label>>line[i-1].opcode>>line[i-1].operand;
{
line[i-1].operand = line[i-1].opcode;
line[i-1].opcode = line[i-1].label;
line[i-1].label = "";
}
if (line[i-1].opcode == "END")
break;
else
i++;
}
Now which code looks easier to handle? Please revisit your code, and take a look at how much you can actually eliminate (yes, eliminate) and clean up.
Regards,
Paul McKenzie