CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2003
    Posts
    6

    Unhappy 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
    Attached Files Attached Files

  2. #2
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Can you be more specific about what is not working?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  3. #3
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40
    Homework, huh?

    Visual C++ as well as many other compilers have debugging tools, why don't you insert breakpoints or run it step by step so you can find what the problem is?

    If you did a good design of the program, then the error should be something like a typo instead of logical.
    int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
    o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

  4. #4
    Join Date
    Apr 2003
    Posts
    6

    Unhappy 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
    Attached Files Attached Files

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Can you be more specific about what is not working?

    You are much, much more likely to get help if you can describe the problem as best as possible so that the time we need to invest is as small as possible. Since you only provide your source code it is unlikely anyone will look at it. You should describe the problem as best as possible here and try to make it possible for us to help you without looking at the code. Then if we think we can help then having the source code can help us help you.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: confused,tired an depressed in need of urgent help

    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    Apr 2003
    Posts
    6

    PASS2 FUNCTION..

    hi
    first i have to thank you for replying to my post and trying to give me a bit of positive thinking..
    ok maybe i didn't explain my problem in a good way..
    my program is supposed to convert a file written in assembly language a simple example is something like:
    COPY START 1000
    - LDA FIVE
    - STA FIVE
    FIVE WORD 5
    - END -
    this is the input or the data for the program and the output should convert be the object code.
    The object code i can't get because the function that grenerates it which is pass2() is not working. but the output that i get from pass1 is the program along with the location counter:
    1000 COPY START 1000
    1003 - LDA FIVE
    1006 - STA FIVE
    1009 FIVE WORD 5
    - END -


    A problem i'm facing is this: as u see when i write my program in assembly i have to type a (-) instead of every space cuz whitespace is not taking into account . In other words,if i didn't type (-) when the program runs it will shift the operation code and the operand towards the empty space so the program will look like this:
    COPY START 1000
    LDA FIVE
    STA FIVE
    FIVE WORD 5
    END
    which will result in wrong calculations and wrong output.

    Now my real problem is in the pass2 function when i comment the pass2 function all other parts of the program work correctly but when it it active it gives me 2 error messages saying that i'm defining main() inside of pass 2!!so i said maybe i didn't close one of the braces so i checked them all( and there r a plenty of them!!)
    but the problem is still un solved.. and this function is the one that does all the real work.I checked it for mistakes but i didn't realize any and as i said there r no missing braces so why does this error say:
    error C2601: 'main' : local function definitions are illegal

    I hope i'm clearer this time..

    thank you

  8. #8
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752
    hi,

    i did not really looked into your code, but as for errors you're getting it's simple. the origin of error is that you've lost one closing bracket, so pass2 is trying to include also main() definition, which is denied in C++. so just add one closing bracket before the main():

    Code:
    int pass2(long int &programlength,inter_file array2[],int no_t[],int s,array1 arr[object_len])
    {  
    ......................
    		output<<"E";
    		output<<array2[0].locctr;
    
    		return 1;
    	}
    }
    //-------------------------------
    
    int main()
    ............
    secret "know how" : put cursor just before the opening/closing bracket and then press Ctrl-} ==> cursor will jump to correspondend closing/opening bracket. so you always may check that something's missing

    in general, i may advise you to format source code according to how Visual Studio offers you because it's much easier to understand what's written.
    Cheers,

    Alex
    Please rate this post if you find it helpful

  9. #9
    Join Date
    Apr 2003
    Posts
    6
    thank you i found my missing bracket

  10. #10
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Talking

    Lolita.......this name originates from a Russian novel about a man's lust for a young girl called Lolita.

    Fair maiden....I came too late. Sam pal and others have already solved your problem for you. Aye~.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by Lolita
    thank you i found my missing bracket
    I did not understand that the program did not compile, but with a missing bracket I assume it was not compiling. It will help a lot to be told that the program won't compile when it won't.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Apr 2003
    Posts
    6

    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

  13. #13
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    In order to help us help you it helps to also show the definition of inter_file, as in:
    Code:
    struct inter_file {
    char *locctr;//store the locctr during pass 1
    char *label;
    char *opcode;
    char *operand;
    };
    Notice that this structure consists entirely of pointers to strings. Thare are no strings in the structure, only pointers to strings. Each pointer must point to actuall memory where there is a string. Either the memory was not allocated or the pointer is not properly set to the address of the memory.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: program is running BUT...

    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
    Last edited by Paul McKenzie; May 6th, 2003 at 01:59 PM.

  15. #15
    Join Date
    Apr 2003
    Posts
    6

    problems again!

    thank you for answering my question ..
    i used string for declaration but the problem was not solved and i had even more problems..
    how can i compre two strings when i don't kow the size because it changes..
    i have three functions in my program:

    int search_opcode(takes the opcode which is a string)

    here i have to compare my opcode to the operation codes in the opcode table so how can i compare two strings?
    i checked MSDN but i didn't really understand what they mean there is this function compare() that takes two strings with the size but in my case the size is not fixed..isn't there another way to comare the strings?

    int check_indexing(takes the operand)
    int stringlen(takes the operand)
    in these two functions i have to start form a certain postion in the strin so when i had them as arrays in my origional code i could start at the index i need..
    to clarify, if i have an operand like X'F1'
    stringlen would have to count strting from F that is at index 2, and check_indexing would check if there is an X at index 0 from the array.
    can i do the same with strings as i can with arrrays? is there a function that does that..

    and finally, if i change my declaration to string would that solve my origional problem which was having garbage charachters in my file?

    thank you

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured